mirror of
https://github.com/strapi/strapi.git
synced 2025-07-21 07:57:45 +00:00
78 lines
1.9 KiB
JavaScript
78 lines
1.9 KiB
JavaScript
'use strict';
|
|
|
|
const { merge } = require('lodash/fp');
|
|
const { mapAsync } = require('@strapi/utils');
|
|
const { getService } = require('../utils');
|
|
const { ACTIONS, FILE_MODEL_UID } = require('../constants');
|
|
const { findEntityAndCheckPermissions } = require('./utils/find-entity-and-check-permissions');
|
|
|
|
module.exports = {
|
|
async find(ctx) {
|
|
const {
|
|
state: { userAbility },
|
|
} = ctx;
|
|
|
|
const defaultQuery = { populate: { folder: true } };
|
|
|
|
const pm = strapi.admin.services.permission.createPermissionsManager({
|
|
ability: userAbility,
|
|
action: ACTIONS.read,
|
|
model: FILE_MODEL_UID,
|
|
});
|
|
|
|
if (!pm.isAllowed) {
|
|
return ctx.forbidden();
|
|
}
|
|
|
|
const pmQuery = pm.addPermissionsQueryTo(merge(defaultQuery, ctx.query));
|
|
|
|
await pm.validateQuery(pmQuery);
|
|
const query = await pm.sanitizeQuery(pmQuery);
|
|
|
|
const { results: files, pagination } = await getService('upload').findPage(query);
|
|
|
|
// Sign file urls for private providers
|
|
const signedFiles = await mapAsync(files, getService('file').signFileUrls);
|
|
|
|
const sanitizedFiles = await pm.sanitizeOutput(signedFiles);
|
|
|
|
return { results: sanitizedFiles, pagination };
|
|
},
|
|
|
|
async findOne(ctx) {
|
|
const {
|
|
state: { userAbility },
|
|
params: { id },
|
|
} = ctx;
|
|
|
|
const { pm, file } = await findEntityAndCheckPermissions(
|
|
userAbility,
|
|
ACTIONS.read,
|
|
FILE_MODEL_UID,
|
|
id
|
|
);
|
|
|
|
const signedFile = await getService('file').signFileUrls(file);
|
|
ctx.body = await pm.sanitizeOutput(signedFile);
|
|
},
|
|
|
|
async destroy(ctx) {
|
|
const { id } = ctx.params;
|
|
const { userAbility } = ctx.state;
|
|
|
|
const { pm, file } = await findEntityAndCheckPermissions(
|
|
userAbility,
|
|
ACTIONS.update,
|
|
FILE_MODEL_UID,
|
|
id
|
|
);
|
|
|
|
const [body] = await Promise.all([
|
|
pm.sanitizeOutput(file, { action: ACTIONS.read }),
|
|
getService('upload').remove(file),
|
|
]);
|
|
|
|
ctx.body = body;
|
|
},
|
|
};
|