| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const _ = require('lodash'); | 
					
						
							| 
									
										
										
										
											2020-10-27 11:27:17 +01:00
										 |  |  | const { contentTypes: contentTypesUtils } = require('strapi-utils'); | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  | const validateSettings = require('../validation/settings'); | 
					
						
							|  |  |  | const validateUploadBody = require('../validation/upload'); | 
					
						
							| 
									
										
										
										
											2020-10-27 11:27:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-08 19:50:39 +02:00
										 |  |  | const { CREATED_BY_ATTRIBUTE } = contentTypesUtils.constants; | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | const ACTIONS = { | 
					
						
							|  |  |  |   read: 'plugins::upload.read', | 
					
						
							|  |  |  |   readSettings: 'plugins::upload.settings.read', | 
					
						
							|  |  |  |   create: 'plugins::upload.assets.create', | 
					
						
							|  |  |  |   update: 'plugins::upload.assets.update', | 
					
						
							|  |  |  |   download: 'plugins::upload.assets.download', | 
					
						
							|  |  |  |   copyLink: 'plugins::upload.assets.copy-link', | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const fileModel = 'plugins::upload.file'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |   async find(ctx) { | 
					
						
							|  |  |  |     const { | 
					
						
							|  |  |  |       state: { userAbility }, | 
					
						
							|  |  |  |     } = ctx; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-02 17:19:42 +01:00
										 |  |  |     const pm = strapi.admin.services.permission.createPermissionsManager({ | 
					
						
							|  |  |  |       ability: userAbility, | 
					
						
							|  |  |  |       action: ACTIONS.read, | 
					
						
							|  |  |  |       model: fileModel, | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!pm.isAllowed) { | 
					
						
							|  |  |  |       return ctx.forbidden(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const method = _.has(ctx.query, '_q') ? 'search' : 'fetchAll'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const query = pm.queryFrom(ctx.query); | 
					
						
							| 
									
										
										
										
											2021-06-02 17:07:00 +03:00
										 |  |  |     const files = await strapi.plugins.upload.services.upload[method](query, []); | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-01 17:47:08 +02:00
										 |  |  |     ctx.body = pm.sanitize(files, { withPrivate: false }); | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async findOne(ctx) { | 
					
						
							|  |  |  |     const { | 
					
						
							|  |  |  |       state: { userAbility }, | 
					
						
							|  |  |  |       params: { id }, | 
					
						
							|  |  |  |     } = ctx; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 16:25:25 +02:00
										 |  |  |     const { pm, file } = await findEntityAndCheckPermissions( | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  |       userAbility, | 
					
						
							|  |  |  |       ACTIONS.read, | 
					
						
							| 
									
										
										
										
											2020-07-06 16:25:25 +02:00
										 |  |  |       fileModel, | 
					
						
							|  |  |  |       id | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  |     ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-01 17:47:08 +02:00
										 |  |  |     ctx.body = pm.sanitize(file, { withPrivate: false }); | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async count(ctx) { | 
					
						
							| 
									
										
										
										
											2020-11-02 17:19:42 +01:00
										 |  |  |     const pm = strapi.admin.services.permission.createPermissionsManager({ | 
					
						
							|  |  |  |       ability: ctx.state.userAbility, | 
					
						
							|  |  |  |       action: ACTIONS.read, | 
					
						
							|  |  |  |       model: fileModel, | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!pm.isAllowed) { | 
					
						
							|  |  |  |       return ctx.forbidden(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const method = _.has(ctx.query, '_q') ? 'countSearch' : 'count'; | 
					
						
							|  |  |  |     const query = pm.queryFrom(ctx.query); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const count = await strapi.plugins.upload.services.upload[method](query); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx.body = { count }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async destroy(ctx) { | 
					
						
							|  |  |  |     const { | 
					
						
							|  |  |  |       state: { userAbility }, | 
					
						
							|  |  |  |       params: { id }, | 
					
						
							|  |  |  |     } = ctx; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 16:25:25 +02:00
										 |  |  |     const { pm, file } = await findEntityAndCheckPermissions( | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  |       userAbility, | 
					
						
							|  |  |  |       ACTIONS.update, | 
					
						
							| 
									
										
										
										
											2020-07-06 16:25:25 +02:00
										 |  |  |       fileModel, | 
					
						
							|  |  |  |       id | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  |     ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await strapi.plugins['upload'].services.upload.remove(file); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-01 17:47:08 +02:00
										 |  |  |     ctx.body = pm.sanitize(file, { action: ACTIONS.read, withPrivate: false }); | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async updateSettings(ctx) { | 
					
						
							|  |  |  |     const { | 
					
						
							|  |  |  |       request: { body }, | 
					
						
							|  |  |  |       state: { userAbility }, | 
					
						
							|  |  |  |     } = ctx; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 18:51:09 +02:00
										 |  |  |     if (userAbility.cannot(ACTIONS.readSettings, fileModel)) { | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  |       return ctx.forbidden(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const data = await validateSettings(body); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await strapi.plugins.upload.services.upload.setSettings(data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx.body = { data }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async getSettings(ctx) { | 
					
						
							|  |  |  |     const { | 
					
						
							|  |  |  |       state: { userAbility }, | 
					
						
							|  |  |  |     } = ctx; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 16:25:25 +02:00
										 |  |  |     if (userAbility.cannot(ACTIONS.readSettings, fileModel)) { | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  |       return ctx.forbidden(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const data = await strapi.plugins.upload.services.upload.getSettings(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx.body = { data }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async updateFileInfo(ctx) { | 
					
						
							|  |  |  |     const { | 
					
						
							|  |  |  |       state: { userAbility, user }, | 
					
						
							|  |  |  |       query: { id }, | 
					
						
							|  |  |  |       request: { body }, | 
					
						
							|  |  |  |     } = ctx; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const uploadService = strapi.plugins.upload.services.upload; | 
					
						
							| 
									
										
										
										
											2020-07-06 16:25:25 +02:00
										 |  |  |     const { pm } = await findEntityAndCheckPermissions(userAbility, ACTIONS.update, fileModel, id); | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const data = await validateUploadBody(body); | 
					
						
							| 
									
										
										
										
											2020-08-31 16:23:43 +02:00
										 |  |  |     const file = await uploadService.updateFileInfo(id, data.fileInfo, { user }); | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-01 17:47:08 +02:00
										 |  |  |     ctx.body = pm.sanitize(file, { action: ACTIONS.read, withPrivate: false }); | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async replaceFile(ctx) { | 
					
						
							|  |  |  |     const { | 
					
						
							|  |  |  |       state: { userAbility, user }, | 
					
						
							|  |  |  |       query: { id }, | 
					
						
							|  |  |  |       request: { body, files: { files } = {} }, | 
					
						
							|  |  |  |     } = ctx; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const uploadService = strapi.plugins.upload.services.upload; | 
					
						
							| 
									
										
										
										
											2020-07-06 16:25:25 +02:00
										 |  |  |     const { pm } = await findEntityAndCheckPermissions(userAbility, ACTIONS.update, fileModel, id); | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (Array.isArray(files)) { | 
					
						
							|  |  |  |       throw strapi.errors.badRequest(null, { | 
					
						
							|  |  |  |         errors: [ | 
					
						
							|  |  |  |           { id: 'Upload.replace.single', message: 'Cannot replace a file with multiple ones' }, | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const data = await validateUploadBody(body); | 
					
						
							| 
									
										
										
										
											2020-10-05 12:00:03 +02:00
										 |  |  |     const replacedFiles = await uploadService.replace(id, { data, file: files }, { user }); | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-01 17:47:08 +02:00
										 |  |  |     ctx.body = pm.sanitize(replacedFiles, { action: ACTIONS.read, withPrivate: false }); | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async uploadFiles(ctx) { | 
					
						
							|  |  |  |     const { | 
					
						
							|  |  |  |       state: { userAbility, user }, | 
					
						
							|  |  |  |       request: { body, files: { files } = {} }, | 
					
						
							| 
									
										
										
										
											2020-07-02 19:18:33 +02:00
										 |  |  |     } = ctx; | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const uploadService = strapi.plugins.upload.services.upload; | 
					
						
							| 
									
										
										
										
											2020-11-02 17:19:42 +01:00
										 |  |  |     const pm = strapi.admin.services.permission.createPermissionsManager({ | 
					
						
							|  |  |  |       ability: userAbility, | 
					
						
							|  |  |  |       action: ACTIONS.create, | 
					
						
							|  |  |  |       model: fileModel, | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!pm.isAllowed) { | 
					
						
							|  |  |  |       throw strapi.errors.forbidden(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const data = await validateUploadBody(body); | 
					
						
							| 
									
										
										
										
											2020-10-05 12:00:03 +02:00
										 |  |  |     const uploadedFiles = await uploadService.upload({ data, files }, { user }); | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-01 17:47:08 +02:00
										 |  |  |     ctx.body = pm.sanitize(uploadedFiles, { action: ACTIONS.read, withPrivate: false }); | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  |   }, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const findEntityAndCheckPermissions = async (ability, action, model, id) => { | 
					
						
							| 
									
										
										
										
											2021-06-02 17:07:00 +03:00
										 |  |  |   const file = await strapi.plugins.upload.services.upload.fetch({ id }, []); | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (_.isNil(file)) { | 
					
						
							|  |  |  |     throw strapi.errors.notFound(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-02 17:19:42 +01:00
										 |  |  |   const pm = strapi.admin.services.permission.createPermissionsManager({ ability, action, model }); | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-17 12:00:02 +02:00
										 |  |  |   const author = await strapi.admin.services.user.findOne({ id: file[CREATED_BY_ATTRIBUTE] }, [ | 
					
						
							|  |  |  |     'roles', | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const fileWithRoles = _.set(_.cloneDeep(file), 'created_by', author); | 
					
						
							| 
									
										
										
										
											2020-07-06 16:25:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (pm.ability.cannot(pm.action, pm.toSubject(fileWithRoles))) { | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  |     throw strapi.errors.forbidden(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 16:25:25 +02:00
										 |  |  |   return { pm, file }; | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  | }; |