Thanos 524af8055c
fix(upload): media library image signedUrl cache busting issue (#18541)
* media library image signedUrl cache busting issue references #18443

* date query param typo

* add asset isUrlSigned property, used as asset url cache busting condition

* removed cacheBustUrl function, added inline comment to describe functionality
2023-10-31 15:28:52 +00:00

59 lines
1.5 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 signFileUrls = async (file) => {
const { provider } = strapi.plugins.upload;
const { provider: providerConfig } = strapi.config.get('plugin.upload');
const isPrivate = await provider.isPrivate();
file.isUrlSigned = false;
// Check file provider and if provider is private
if (file.provider !== providerConfig || !isPrivate) {
return file;
}
const signUrl = async (file) => {
const signedUrl = await provider.getSignedUrl(file);
file.url = signedUrl.url;
file.isUrlSigned = true;
};
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,
signFileUrls,
};