2023-02-11 17:37:28 +01:00

63 lines
1.6 KiB
JavaScript

'use strict';
const { cloneDeep } = require('lodash/fp');
const { mapAsync } = require('@strapi/utils');
const { FOLDER_MODEL_UID, FILE_MODEL_UID } = require('../constants');
const { getService } = require('../utils');
const getFolderPath = async (folderId) => {
if (!folderId) return '/';
const parentFolder = await strapi.entityService.findOne(FOLDER_MODEL_UID, folderId);
return parentFolder.path;
};
const deleteByIds = async (ids = []) => {
const filesToDelete = await strapi.db
.query(FILE_MODEL_UID)
.findMany({ where: { id: { $in: ids } } });
await Promise.all(filesToDelete.map((file) => getService('upload').remove(file)));
return filesToDelete;
};
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');
// Check file provider and if provider is private
if (file.provider !== providerConfig || !provider.isPrivate()) {
return file;
}
const signUrl = async (file) => {
const signedUrl = await provider.getSignedUrl(file);
file.url = signedUrl.url;
};
const signedFile = cloneDeep(file);
// Sign each file format
await signUrl(signedFile);
if (file.formats) {
await mapAsync(Object.values(signedFile.formats), signUrl);
}
return signedFile;
};
module.exports = {
getFolderPath,
deleteByIds,
signFileUrl,
signFileUrls,
};