86 lines
2.2 KiB
JavaScript
Raw Normal View History

'use strict';
2022-05-13 16:10:18 +02:00
const { FOLDER_MODEL_UID, FILE_MODEL_UID } = require('../constants');
2022-04-26 11:23:07 +02:00
2022-05-13 16:10:18 +02:00
const { getService } = require('../utils');
2022-08-08 23:33:39 +02:00
const getFolderPath = async (folderId) => {
if (!folderId) return '/';
2022-05-13 16:10:18 +02:00
const parentFolder = await strapi.entityService.findOne(FOLDER_MODEL_UID, folderId);
2022-04-12 16:32:05 +02:00
return parentFolder.path;
};
2022-04-26 11:23:07 +02:00
const deleteByIds = async (ids = []) => {
2022-05-13 16:10:18 +02:00
const filesToDelete = await strapi.db
.query(FILE_MODEL_UID)
.findMany({ where: { id: { $in: ids } } });
2022-04-26 11:23:07 +02:00
2022-08-08 23:33:39 +02:00
await Promise.all(filesToDelete.map((file) => getService('upload').remove(file)));
2022-04-26 11:23:07 +02:00
return filesToDelete;
};
2023-02-09 15:02:15 +01:00
const signFileUrl = async (fileIdentifier) => {
const { provider } = strapi.plugins.upload;
const { url } = await provider.getSignedUrl(fileIdentifier);
return url;
};
const signFileUrls = async (file) => {
const { provider } = strapi.plugins.upload;
// Check file provider and if provider is private
if (file.provider === provider.name && provider.isPrivate()) {
file.url = (await provider.getSignedUrl(file)).url;
// Sign each file format
if (file.formats) {
// File formats is an object with keys as format name and values the file object defintion
// We need to sign each file format
file.formats = await Promise.all(
Object.keys(file.formats).map(async (format) => {
const formatFile = file.formats[format];
const signedURL = await provider.getSignedUrl(formatFile);
formatFile.url = signedURL.url;
return formatFile;
})
);
}
}
return file;
};
const addSignedFileUrlsToAdmin = () => {
const { provider } = strapi.plugins.upload;
// Only if provider is private, we need to sign the file urls
if (provider.isPrivate()) {
strapi.container
.get('services')
.extend(`plugin::content-manager.entity-manager`, (entityManager) => {
const find = (opts, uid) => {
return entityManager.find(opts, uid).then((results) => {
return results;
});
};
return {
...entityManager,
find,
};
});
}
};
module.exports = {
2022-04-12 16:32:05 +02:00
getFolderPath,
2022-04-26 11:23:07 +02:00
deleteByIds,
2023-02-09 15:02:15 +01:00
signFileUrl,
signFileUrls,
addSignedFileUrlsToAdmin,
};