137 lines
3.7 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;
const { provider: providerConfig } = strapi.config.get('plugin.upload');
2023-02-09 15:02:15 +01:00
// Check file provider and if provider is private
if (file.provider === providerConfig && provider.isPrivate()) {
2023-02-09 15:02:15 +01:00
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;
};
/**
*
* Iterate through an entity manager result
* Check which modelAttributes are media and pre sign the image URLs
* if they are from the current upload provider
* @param {Object} entity
* @param {Object} modelAttributes
* @param {String} providerConfig
* @returns
*/
const signEntityMedia = async (entity, modelAttributes, providerConfig) => {
if (!modelAttributes) {
return entity;
}
for (const [key, value] of Object.entries(entity)) {
if (!value) continue;
const isMedia = modelAttributes[key]?.type === 'media';
if (!isMedia || value.provider !== providerConfig) continue;
await signFileUrls(value);
}
return entity;
};
2023-02-09 15:02:15 +01:00
const addSignedFileUrlsToAdmin = () => {
const { provider } = strapi.plugins.upload;
const { provider: providerConfig } = strapi.config.get('plugin.upload');
2023-02-09 15:02:15 +01:00
// We only need to sign the file urls if the provider is private
if (!provider.isPrivate()) {
return;
2023-02-09 15:02:15 +01:00
}
strapi.container
.get('services')
.extend(`plugin::content-manager.entity-manager`, (entityManager) => {
const findWithRelationCountsPage = async (opts, uid) => {
const entityManagerResults = await entityManager.findWithRelationCountsPage(opts, uid);
const attributes = strapi.getModel(uid)?.attributes;
await Promise.all(
entityManagerResults.results.map(async (entity) =>
signEntityMedia(entity, attributes, providerConfig)
)
);
return entityManagerResults;
};
const findOneWithCreatorRolesAndCount = async (id, uid) => {
const entityManagerResult = await entityManager.findOneWithCreatorRolesAndCount(id, uid);
await signEntityMedia(
entityManagerResult,
strapi.getModel(uid)?.attributes,
providerConfig
);
return entityManagerResult;
};
return {
...entityManager,
findOneWithCreatorRolesAndCount,
findWithRelationCountsPage,
};
});
2023-02-09 15:02:15 +01:00
};
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,
};