2019-08-21 11:05:33 +02:00
|
|
|
const path = require('path');
|
2018-09-10 16:05:00 +08:00
|
|
|
const _ = require('lodash');
|
2019-08-21 11:05:33 +02:00
|
|
|
const crypto = require('crypto');
|
|
|
|
const toArray = require('stream-to-array');
|
|
|
|
const uuid = require('uuid/v4');
|
|
|
|
|
|
|
|
function niceHash(buffer) {
|
|
|
|
return crypto
|
|
|
|
.createHash('sha256')
|
|
|
|
.update(buffer)
|
|
|
|
.digest('base64')
|
|
|
|
.replace(/=/g, '')
|
|
|
|
.replace(/\//g, '-')
|
|
|
|
.replace(/\+/, '_');
|
|
|
|
}
|
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;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const formatFile = async (upload, fields) => {
|
|
|
|
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);
|
|
|
|
|
|
|
|
const fileData = {
|
|
|
|
name: filename,
|
|
|
|
sha256: niceHash(buffer),
|
|
|
|
hash: uuid().replace(/-/g, ''),
|
|
|
|
ext: path.extname(filename),
|
|
|
|
buffer,
|
|
|
|
mime: mimetype,
|
2020-03-02 09:31:06 +01:00
|
|
|
size: Math.round((buffer.length / 1000) * 100) / 100,
|
2019-08-21 11:05:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const { refId, ref, source, field } = fields;
|
|
|
|
|
|
|
|
// Add details to the file to be able to create the relationships.
|
|
|
|
if (refId && ref && field) {
|
|
|
|
fileData.related = [
|
|
|
|
{
|
|
|
|
refId,
|
|
|
|
ref,
|
|
|
|
source,
|
|
|
|
field,
|
|
|
|
},
|
|
|
|
];
|
2018-04-10 12:56:13 +02:00
|
|
|
}
|
2019-08-21 11:05:33 +02:00
|
|
|
|
|
|
|
return fileData;
|
2018-04-10 12:56:13 +02:00
|
|
|
};
|