2020-10-27 11:27:17 +01:00
|
|
|
'use strict';
|
|
|
|
|
2018-09-10 16:05:00 +08:00
|
|
|
const _ = require('lodash');
|
2020-03-08 18:43:50 +01:00
|
|
|
const { streamToBuffer } = require('../utils/file');
|
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: {
|
2020-01-29 15:30:53 +01:00
|
|
|
resolver: 'plugins::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',
|
2020-01-29 15:30:53 +01:00
|
|
|
resolverOf: 'plugins::upload.upload.upload',
|
2021-01-21 23:18:45 +09:00
|
|
|
resolver: async (obj, { file: upload, info, ...fields }) => {
|
|
|
|
const file = await formatFile(upload, info, fields);
|
2019-08-21 11:05:33 +02:00
|
|
|
|
2020-03-05 13:51:15 +01:00
|
|
|
const uploadedFiles = await strapi.plugins.upload.services.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',
|
2020-01-29 15:30:53 +01:00
|
|
|
resolverOf: 'plugins::upload.upload.upload',
|
2019-08-21 11:05:33 +02:00
|
|
|
resolver: async (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
|
|
|
|
2020-03-05 13:51:15 +01:00
|
|
|
const uploadService = strapi.plugins.upload.services.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',
|
|
|
|
resolverOf: 'plugins::upload.upload.upload',
|
|
|
|
resolver: async (obj, { id, info }) => {
|
|
|
|
return await strapi.plugins.upload.services.upload.updateFileInfo(id, info);
|
|
|
|
},
|
|
|
|
},
|
2020-10-05 11:34:15 +02:00
|
|
|
deleteFile: {
|
|
|
|
description: 'Delete one file',
|
|
|
|
resolverOf: 'plugins::upload.upload.destroy',
|
|
|
|
resolver: async (obj, options, { context }) => {
|
|
|
|
const file = await strapi.plugins.upload.services.upload.fetch({ id: context.params.id });
|
|
|
|
if (file) {
|
|
|
|
const fileResult = await strapi.plugins.upload.services.upload.remove(file);
|
|
|
|
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;
|
|
|
|
|
2020-03-08 18:43:50 +01:00
|
|
|
const { optimize } = strapi.plugins.upload.services['image-manipulation'];
|
|
|
|
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
|
|
|
|
2020-03-05 13:51:15 +01:00
|
|
|
const uploadService = strapi.plugins.upload.services.upload;
|
|
|
|
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
|
|
|
};
|