| 
									
										
										
										
											2018-02-08 12:01:06 +01:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Upload.js controller | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-21 14:06:57 +01:00
										 |  |  | const _ = require('lodash'); | 
					
						
							| 
									
										
										
										
											2021-04-29 13:51:12 +02:00
										 |  |  | const { sanitizeEntity } = require('@strapi/utils'); | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  | const apiUploadController = require('./upload/api'); | 
					
						
							|  |  |  | const adminUploadController = require('./upload/admin'); | 
					
						
							| 
									
										
										
										
											2020-06-29 11:12:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  | const resolveController = ctx => { | 
					
						
							| 
									
										
										
										
											2020-07-02 15:58:12 +02:00
										 |  |  |   const { | 
					
						
							|  |  |  |     state: { isAuthenticatedAdmin }, | 
					
						
							|  |  |  |   } = ctx; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  |   return isAuthenticatedAdmin ? adminUploadController : apiUploadController; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const resolveControllerMethod = method => ctx => { | 
					
						
							|  |  |  |   const controller = resolveController(ctx); | 
					
						
							| 
									
										
										
										
											2020-07-02 15:58:12 +02:00
										 |  |  |   const callbackFn = controller[method]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!_.isFunction(callbackFn)) { | 
					
						
							|  |  |  |     return ctx.notFound(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return callbackFn(ctx); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-05 13:51:15 +01:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  |   find: resolveControllerMethod('find'), | 
					
						
							|  |  |  |   findOne: resolveControllerMethod('findOne'), | 
					
						
							|  |  |  |   count: resolveControllerMethod('count'), | 
					
						
							|  |  |  |   destroy: resolveControllerMethod('destroy'), | 
					
						
							|  |  |  |   updateSettings: resolveControllerMethod('updateSettings'), | 
					
						
							|  |  |  |   getSettings: resolveControllerMethod('getSettings'), | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-05 13:51:15 +01:00
										 |  |  |   async upload(ctx) { | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  |     const isUploadDisabled = _.get(strapi.plugins, 'upload.config.enabled', true) === false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (isUploadDisabled) { | 
					
						
							|  |  |  |       throw strapi.errors.badRequest(null, { | 
					
						
							|  |  |  |         errors: [{ id: 'Upload.status.disabled', message: 'File upload is disabled' }], | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2018-02-20 17:10:25 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  |     const { | 
					
						
							|  |  |  |       query: { id }, | 
					
						
							| 
									
										
										
										
											2020-07-02 19:18:33 +02:00
										 |  |  |       request: { files: { files } = {} }, | 
					
						
							|  |  |  |     } = ctx; | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  |     const controller = resolveController(ctx); | 
					
						
							| 
									
										
										
										
											2020-03-24 12:21:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (id && (_.isEmpty(files) || files.size === 0)) { | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  |       return controller.updateFileInfo(ctx); | 
					
						
							| 
									
										
										
										
											2020-03-24 12:21:51 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-08 18:43:50 +01:00
										 |  |  |     if (_.isEmpty(files) || files.size === 0) { | 
					
						
							| 
									
										
										
										
											2020-07-02 18:49:20 +02:00
										 |  |  |       throw strapi.errors.badRequest(null, { | 
					
						
							|  |  |  |         errors: [{ id: 'Upload.status.empty', message: 'Files are empty' }], | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2018-02-21 14:46:10 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-02 19:18:33 +02:00
										 |  |  |     await (id ? controller.replaceFile : controller.uploadFiles)(ctx); | 
					
						
							| 
									
										
										
										
											2018-02-20 17:10:25 +01:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 18:28:56 +02:00
										 |  |  |   async search(ctx) { | 
					
						
							|  |  |  |     const { id } = ctx.params; | 
					
						
							| 
									
										
										
										
											2020-11-16 12:55:35 +01:00
										 |  |  |     const model = strapi.getModel('file', 'upload'); | 
					
						
							|  |  |  |     const entries = await strapi.query('file', 'upload').custom(searchQueries)({ | 
					
						
							| 
									
										
										
										
											2019-07-15 18:28:56 +02:00
										 |  |  |       id, | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-11-16 12:55:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ctx.body = sanitizeEntity(entries, { model }); | 
					
						
							| 
									
										
										
										
											2018-02-22 17:12:03 +01:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2018-02-08 12:01:06 +01:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2019-07-15 18:28:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | const searchQueries = { | 
					
						
							|  |  |  |   bookshelf({ model }) { | 
					
						
							|  |  |  |     return ({ id }) => { | 
					
						
							|  |  |  |       return model | 
					
						
							|  |  |  |         .query(qb => { | 
					
						
							| 
									
										
										
										
											2020-02-26 14:56:27 +01:00
										 |  |  |           qb.whereRaw('LOWER(hash) LIKE ?', [`%${id}%`]).orWhereRaw('LOWER(name) LIKE ?', [ | 
					
						
							| 
									
										
										
										
											2020-02-13 12:30:26 +01:00
										 |  |  |             `%${id}%`, | 
					
						
							| 
									
										
										
										
											2020-02-26 14:56:27 +01:00
										 |  |  |           ]); | 
					
						
							| 
									
										
										
										
											2019-07-15 18:28:56 +02:00
										 |  |  |         }) | 
					
						
							|  |  |  |         .fetchAll() | 
					
						
							|  |  |  |         .then(results => results.toJSON()); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   mongoose({ model }) { | 
					
						
							|  |  |  |     return ({ id }) => { | 
					
						
							|  |  |  |       const re = new RegExp(id, 'i'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-24 17:49:17 +01:00
										 |  |  |       return model | 
					
						
							|  |  |  |         .find({ | 
					
						
							|  |  |  |           $or: [{ hash: re }, { name: re }], | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         .lean(); | 
					
						
							| 
									
										
										
										
											2019-07-15 18:28:56 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }; |