| 
									
										
										
										
											2022-03-22 18:19:46 +01:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const _ = require('lodash'); | 
					
						
							|  |  |  | const { ApplicationError } = require('@strapi/utils').errors; | 
					
						
							|  |  |  | const { getService } = require('../utils'); | 
					
						
							| 
									
										
										
										
											2022-05-13 16:10:18 +02:00
										 |  |  | const { ACTIONS, FILE_MODEL_UID } = require('../constants'); | 
					
						
							| 
									
										
										
										
											2022-04-06 12:02:47 +02:00
										 |  |  | const validateUploadBody = require('./validation/admin/upload'); | 
					
						
							| 
									
										
										
										
											2022-03-30 16:26:09 +02:00
										 |  |  | const { findEntityAndCheckPermissions } = require('./utils/find-entity-and-check-permissions'); | 
					
						
							| 
									
										
										
										
											2022-03-22 18:19:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |   async updateFileInfo(ctx) { | 
					
						
							|  |  |  |     const { | 
					
						
							|  |  |  |       state: { userAbility, user }, | 
					
						
							|  |  |  |       query: { id }, | 
					
						
							|  |  |  |       request: { body }, | 
					
						
							|  |  |  |     } = ctx; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const uploadService = getService('upload'); | 
					
						
							| 
									
										
										
										
											2022-05-13 16:10:18 +02:00
										 |  |  |     const { pm } = await findEntityAndCheckPermissions( | 
					
						
							|  |  |  |       userAbility, | 
					
						
							|  |  |  |       ACTIONS.update, | 
					
						
							|  |  |  |       FILE_MODEL_UID, | 
					
						
							|  |  |  |       id | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2022-03-22 18:19:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const data = await validateUploadBody(body); | 
					
						
							|  |  |  |     const file = await uploadService.updateFileInfo(id, data.fileInfo, { user }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx.body = await pm.sanitizeOutput(file, { action: ACTIONS.read }); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async replaceFile(ctx) { | 
					
						
							|  |  |  |     const { | 
					
						
							|  |  |  |       state: { userAbility, user }, | 
					
						
							|  |  |  |       query: { id }, | 
					
						
							|  |  |  |       request: { body, files: { files } = {} }, | 
					
						
							|  |  |  |     } = ctx; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const uploadService = getService('upload'); | 
					
						
							| 
									
										
										
										
											2022-05-13 16:10:18 +02:00
										 |  |  |     const { pm } = await findEntityAndCheckPermissions( | 
					
						
							|  |  |  |       userAbility, | 
					
						
							|  |  |  |       ACTIONS.update, | 
					
						
							|  |  |  |       FILE_MODEL_UID, | 
					
						
							|  |  |  |       id | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2022-03-22 18:19:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (Array.isArray(files)) { | 
					
						
							|  |  |  |       throw new ApplicationError('Cannot replace a file with multiple ones'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const data = await validateUploadBody(body); | 
					
						
							|  |  |  |     const replacedFiles = await uploadService.replace(id, { data, file: files }, { user }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx.body = await pm.sanitizeOutput(replacedFiles, { action: ACTIONS.read }); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async uploadFiles(ctx) { | 
					
						
							|  |  |  |     const { | 
					
						
							|  |  |  |       state: { userAbility, user }, | 
					
						
							|  |  |  |       request: { body, files: { files } = {} }, | 
					
						
							|  |  |  |     } = ctx; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const uploadService = getService('upload'); | 
					
						
							|  |  |  |     const pm = strapi.admin.services.permission.createPermissionsManager({ | 
					
						
							|  |  |  |       ability: userAbility, | 
					
						
							|  |  |  |       action: ACTIONS.create, | 
					
						
							| 
									
										
										
										
											2022-05-13 16:10:18 +02:00
										 |  |  |       model: FILE_MODEL_UID, | 
					
						
							| 
									
										
										
										
											2022-03-22 18:19:46 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!pm.isAllowed) { | 
					
						
							|  |  |  |       return ctx.forbidden(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const data = await validateUploadBody(body); | 
					
						
							|  |  |  |     const uploadedFiles = await uploadService.upload({ data, files }, { user }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx.body = await pm.sanitizeOutput(uploadedFiles, { action: ACTIONS.read }); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async upload(ctx) { | 
					
						
							|  |  |  |     const { | 
					
						
							|  |  |  |       query: { id }, | 
					
						
							|  |  |  |       request: { files: { files } = {} }, | 
					
						
							|  |  |  |     } = ctx; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (_.isEmpty(files) || files.size === 0) { | 
					
						
							| 
									
										
										
										
											2022-04-12 16:32:05 +02:00
										 |  |  |       if (id) { | 
					
						
							|  |  |  |         return this.updateFileInfo(ctx); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-22 18:19:46 +01:00
										 |  |  |       throw new ApplicationError('Files are empty'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await (id ? this.replaceFile : this.uploadFiles)(ctx); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }; |