| 
									
										
										
										
											2020-10-27 11:27:17 +01:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-10 16:05:00 +08:00
										 |  |  | const _ = require('lodash'); | 
					
						
							| 
									
										
										
										
											2021-08-19 16:49:33 +02:00
										 |  |  | const { streamToBuffer } = require('./utils/file'); | 
					
						
							|  |  |  | const { getService } = require('./utils'); | 
					
						
							| 
									
										
										
										
											2018-09-10 16:05:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-10 12:56:13 +02:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2020-09-03 12:17:40 +02:00
										 |  |  |   definition: `
 | 
					
						
							|  |  |  |     input FileInfoInput { | 
					
						
							|  |  |  |       name: String | 
					
						
							|  |  |  |       alternativeText: String | 
					
						
							|  |  |  |       caption: String | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   `,
 | 
					
						
							| 
									
										
										
										
											2018-09-10 16:05:00 +08:00
										 |  |  |   mutation: `
 | 
					
						
							| 
									
										
										
										
											2021-01-21 23:18:45 +09:00
										 |  |  |     upload(refId: ID, ref: String, field: String, source: String, info: FileInfoInput, file: Upload!): UploadFile! | 
					
						
							| 
									
										
										
										
											2019-08-21 11:05:33 +02:00
										 |  |  |     multipleUpload(refId: ID, ref: String, field: String, source: String, files: [Upload]!): [UploadFile]! | 
					
						
							| 
									
										
										
										
											2020-09-03 12:17:40 +02:00
										 |  |  |     updateFileInfo(id: ID!, info: FileInfoInput!): UploadFile! | 
					
						
							| 
									
										
										
										
											2018-09-10 16:05:00 +08:00
										 |  |  |   `,
 | 
					
						
							| 
									
										
										
										
											2018-04-10 12:56:13 +02:00
										 |  |  |   resolver: { | 
					
						
							|  |  |  |     Query: { | 
					
						
							| 
									
										
										
										
											2018-04-10 18:54:01 +02:00
										 |  |  |       file: false, | 
					
						
							| 
									
										
										
										
											2018-04-10 12:56:13 +02:00
										 |  |  |       files: { | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |         resolver: 'plugin::upload.upload.find', | 
					
						
							| 
									
										
										
										
											2019-08-21 11:05:33 +02:00
										 |  |  |       }, | 
					
						
							| 
									
										
										
										
											2018-09-10 16:05:00 +08:00
										 |  |  |     }, | 
					
						
							|  |  |  |     Mutation: { | 
					
						
							|  |  |  |       createFile: false, | 
					
						
							|  |  |  |       updateFile: false, | 
					
						
							|  |  |  |       upload: { | 
					
						
							| 
									
										
										
										
											2019-08-21 11:05:33 +02:00
										 |  |  |         description: 'Upload one file', | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |         resolverOf: 'plugin::upload.upload.upload', | 
					
						
							| 
									
										
										
										
											2021-09-13 12:03:12 +02:00
										 |  |  |         async resolver(obj, { file: upload, info, ...fields }) { | 
					
						
							| 
									
										
										
										
											2021-01-21 23:18:45 +09:00
										 |  |  |           const file = await formatFile(upload, info, fields); | 
					
						
							| 
									
										
										
										
											2019-08-21 11:05:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-08 22:07:52 +02:00
										 |  |  |           const uploadedFiles = await getService('upload').uploadFileAndPersist(file); | 
					
						
							| 
									
										
										
										
											2018-09-10 16:05:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |           // Return response.
 | 
					
						
							| 
									
										
										
										
											2019-08-21 11:05:33 +02:00
										 |  |  |           return uploadedFiles.length === 1 ? uploadedFiles[0] : uploadedFiles; | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       multipleUpload: { | 
					
						
							|  |  |  |         description: 'Upload one file', | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |         resolverOf: 'plugin::upload.upload.upload', | 
					
						
							| 
									
										
										
										
											2021-09-13 12:03:12 +02:00
										 |  |  |         async resolver(obj, { files: uploads, ...fields }) { | 
					
						
							| 
									
										
										
										
											2021-01-21 23:18:45 +09:00
										 |  |  |           const files = await Promise.all(uploads.map(upload => formatFile(upload, {}, fields))); | 
					
						
							| 
									
										
										
										
											2019-08-21 11:05:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-08 22:07:52 +02:00
										 |  |  |           const uploadService = getService('upload'); | 
					
						
							| 
									
										
										
										
											2019-08-21 11:05:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-05 13:51:15 +01:00
										 |  |  |           return Promise.all(files.map(file => uploadService.uploadFileAndPersist(file))); | 
					
						
							| 
									
										
										
										
											2019-08-21 11:05:33 +02:00
										 |  |  |         }, | 
					
						
							|  |  |  |       }, | 
					
						
							| 
									
										
										
										
											2020-09-03 12:17:40 +02:00
										 |  |  |       updateFileInfo: { | 
					
						
							|  |  |  |         description: 'Update file information', | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |         resolverOf: 'plugin::upload.upload.upload', | 
					
						
							| 
									
										
										
										
											2021-09-13 12:03:12 +02:00
										 |  |  |         async resolver(obj, { id, info }) { | 
					
						
							|  |  |  |           return getService('upload').updateFileInfo(id, info); | 
					
						
							| 
									
										
										
										
											2020-09-03 12:17:40 +02:00
										 |  |  |         }, | 
					
						
							|  |  |  |       }, | 
					
						
							| 
									
										
										
										
											2020-10-05 11:34:15 +02:00
										 |  |  |       deleteFile: { | 
					
						
							|  |  |  |         description: 'Delete one file', | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |         resolverOf: 'plugin::upload.upload.destroy', | 
					
						
							| 
									
										
										
										
											2021-09-13 12:03:12 +02:00
										 |  |  |         async resolver(obj, options, { context }) { | 
					
						
							| 
									
										
										
										
											2021-07-08 22:07:52 +02:00
										 |  |  |           const file = await getService('upload').findOne({ | 
					
						
							|  |  |  |             id: context.params.id, | 
					
						
							|  |  |  |           }); | 
					
						
							| 
									
										
										
										
											2020-10-05 11:34:15 +02:00
										 |  |  |           if (file) { | 
					
						
							| 
									
										
										
										
											2021-07-08 22:07:52 +02:00
										 |  |  |             const fileResult = await getService('upload').remove(file); | 
					
						
							| 
									
										
										
										
											2020-10-05 11:34:15 +02:00
										 |  |  |             return { file: fileResult }; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }, | 
					
						
							| 
									
										
										
										
											2019-08-21 11:05:33 +02:00
										 |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-21 23:18:45 +09:00
										 |  |  | const formatFile = async (upload, extraInfo, metas) => { | 
					
						
							| 
									
										
										
										
											2019-08-21 11:05:33 +02:00
										 |  |  |   const { filename, mimetype, createReadStream } = await upload; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-19 22:27:00 +02:00
										 |  |  |   const { optimize } = strapi.plugin('upload').service('image-manipulation'); | 
					
						
							| 
									
										
										
										
											2020-03-08 18:43:50 +01:00
										 |  |  |   const readBuffer = await streamToBuffer(createReadStream()); | 
					
						
							| 
									
										
										
										
											2019-08-21 11:05:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-08 18:43:50 +01:00
										 |  |  |   const { buffer, info } = await optimize(readBuffer); | 
					
						
							| 
									
										
										
										
											2019-08-21 11:05:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-08 22:07:52 +02:00
										 |  |  |   const uploadService = getService('upload'); | 
					
						
							| 
									
										
										
										
											2020-03-05 13:51:15 +01:00
										 |  |  |   const fileInfo = uploadService.formatFileInfo( | 
					
						
							| 
									
										
										
										
											2020-03-03 15:54:59 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |       filename, | 
					
						
							|  |  |  |       type: mimetype, | 
					
						
							|  |  |  |       size: buffer.length, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-01-21 23:18:45 +09:00
										 |  |  |     extraInfo || {}, | 
					
						
							| 
									
										
										
										
											2020-03-03 15:54:59 +01:00
										 |  |  |     metas | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2019-08-21 11:05:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-08 18:43:50 +01:00
										 |  |  |   return _.assign(fileInfo, info, { buffer }); | 
					
						
							| 
									
										
										
										
											2018-04-10 12:56:13 +02:00
										 |  |  | }; |