2018-09-10 16:05:00 +08:00
|
|
|
const _ = require('lodash');
|
2019-08-21 11:05:33 +02:00
|
|
|
const toArray = require('stream-to-array');
|
2018-09-10 16:05:00 +08:00
|
|
|
|
2018-04-10 12:56:13 +02:00
|
|
|
module.exports = {
|
2018-09-10 16:05:00 +08:00
|
|
|
mutation: `
|
2019-08-21 11:05:33 +02:00
|
|
|
upload(refId: ID, ref: String, field: String, source: String, file: Upload!): UploadFile!
|
|
|
|
multipleUpload(refId: ID, ref: String, field: String, source: String, files: [Upload]!): [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,
|
|
|
|
deleteFile: 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',
|
2019-08-21 11:05:33 +02:00
|
|
|
resolver: async (obj, { file: upload, ...fields }) => {
|
|
|
|
const file = await formatFile(upload, fields);
|
|
|
|
|
2020-02-26 19:38:23 +01:00
|
|
|
const uploadedFiles = await strapi.plugins.upload.services.upload.upload([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 }) => {
|
2020-03-02 15:18:08 +01:00
|
|
|
const files = await Promise.all(uploads.map(upload => formatFile(upload, fields)));
|
2019-08-21 11:05:33 +02:00
|
|
|
|
2020-02-26 19:38:23 +01:00
|
|
|
const uploadedFiles = await strapi.plugins.upload.services.upload.upload(files);
|
2019-08-21 11:05:33 +02:00
|
|
|
|
|
|
|
// Return response.
|
|
|
|
return uploadedFiles;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-03-03 15:54:59 +01:00
|
|
|
const formatFile = async (upload, metas) => {
|
2019-08-21 11:05:33 +02:00
|
|
|
const { filename, mimetype, createReadStream } = await upload;
|
|
|
|
|
|
|
|
const stream = createReadStream();
|
|
|
|
|
|
|
|
const parts = await toArray(stream);
|
2020-03-02 15:18:08 +01:00
|
|
|
const buffers = parts.map(part => (_.isBuffer(part) ? part : Buffer.from(part)));
|
2019-08-21 11:05:33 +02:00
|
|
|
|
|
|
|
const buffer = Buffer.concat(buffers);
|
|
|
|
|
2020-03-03 15:54:59 +01:00
|
|
|
const { formatFileInfo } = strapi.plugins.upload.services.upload;
|
|
|
|
const fileInfo = formatFileInfo(
|
|
|
|
{
|
|
|
|
filename,
|
|
|
|
type: mimetype,
|
|
|
|
size: buffer.length,
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
metas
|
|
|
|
);
|
2019-08-21 11:05:33 +02:00
|
|
|
|
2020-03-03 15:54:59 +01:00
|
|
|
return _.assign(fileInfo, { buffer });
|
2018-04-10 12:56:13 +02:00
|
|
|
};
|