63 lines
1.6 KiB
JavaScript
Raw Normal View History

'use strict';
2023-02-11 17:37:28 +01:00
const { cloneDeep } = require('lodash/fp');
2023-02-10 09:44:49 +01:00
const { mapAsync } = require('@strapi/utils');
2022-05-13 16:10:18 +02:00
const { FOLDER_MODEL_UID, FILE_MODEL_UID } = require('../constants');
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
2023-02-10 09:44:49 +01:00
if (file.provider !== providerConfig || !provider.isPrivate()) {
2023-02-11 17:37:28 +01:00
return file;
2023-02-10 09:44:49 +01:00
}
const signUrl = async (file) => {
const signedUrl = await provider.getSignedUrl(file);
file.url = signedUrl.url;
};
2023-02-11 17:37:28 +01:00
const signedFile = cloneDeep(file);
2023-02-10 09:44:49 +01:00
// Sign each file format
2023-02-11 17:37:28 +01:00
await signUrl(signedFile);
2023-02-10 09:44:49 +01:00
if (file.formats) {
2023-02-11 17:37:28 +01:00
await mapAsync(Object.values(signedFile.formats), signUrl);
2023-02-09 15:02:15 +01:00
}
2023-02-11 17:37:28 +01:00
return signedFile;
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,
};