2018-02-08 12:01:06 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Upload.js service
|
|
|
|
*
|
|
|
|
* @description: A set of functions similar to controller's actions to avoid code duplication.
|
|
|
|
*/
|
|
|
|
|
2018-02-19 14:26:20 +01:00
|
|
|
const fs = require('fs');
|
2020-03-03 15:54:59 +01:00
|
|
|
const path = require('path');
|
2018-09-17 15:50:13 +08:00
|
|
|
const crypto = require('crypto');
|
2018-04-30 18:00:01 +02:00
|
|
|
const _ = require('lodash');
|
2018-02-19 14:26:20 +01:00
|
|
|
const toArray = require('stream-to-array');
|
2020-03-03 15:54:59 +01:00
|
|
|
const filenamify = require('filenamify');
|
2020-03-06 18:41:48 +01:00
|
|
|
const { bytesToKbytes } = require('../utils/file');
|
2020-03-03 15:54:59 +01:00
|
|
|
|
|
|
|
const randomSuffix = () => crypto.randomBytes(5).toString('hex');
|
|
|
|
const generateFileName = name => {
|
|
|
|
const baseName = filenamify(name, { replacement: '_' }).replace(/\s/g, '_');
|
|
|
|
|
|
|
|
return `${baseName}_${randomSuffix()}`;
|
|
|
|
};
|
2018-09-17 15:50:13 +08:00
|
|
|
|
2018-02-08 12:01:06 +01:00
|
|
|
module.exports = {
|
2020-03-03 16:31:18 +01:00
|
|
|
formatFileInfo({ filename, type, size }, fileInfo = {}, metas = {}) {
|
2020-03-03 15:54:59 +01:00
|
|
|
const ext = path.extname(filename);
|
|
|
|
const baseName = path.basename(filename, ext);
|
|
|
|
|
|
|
|
const usedName = fileInfo.name || baseName;
|
|
|
|
|
|
|
|
const entity = {
|
|
|
|
name: usedName,
|
|
|
|
alternativeText: fileInfo.alternativeText,
|
|
|
|
caption: fileInfo.caption,
|
|
|
|
// sha256: niceHash(buffer),
|
|
|
|
hash: generateFileName(usedName),
|
|
|
|
ext,
|
|
|
|
mime: type,
|
2020-03-06 18:41:48 +01:00
|
|
|
size: bytesToKbytes(size),
|
2020-03-03 15:54:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const { refId, ref, source, field } = metas;
|
|
|
|
|
|
|
|
if (refId && ref && field) {
|
|
|
|
entity.related = [
|
|
|
|
{
|
|
|
|
refId,
|
|
|
|
ref,
|
|
|
|
source,
|
|
|
|
field,
|
|
|
|
},
|
|
|
|
];
|
2018-02-19 14:26:20 +01:00
|
|
|
}
|
|
|
|
|
2020-03-03 15:54:59 +01:00
|
|
|
if (metas.path) {
|
|
|
|
entity.path = metas.path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return entity;
|
|
|
|
},
|
|
|
|
|
2020-03-05 13:51:15 +01:00
|
|
|
async enhanceFile(file, fileInfo = {}, metas = {}) {
|
2020-03-03 15:54:59 +01:00
|
|
|
const parts = await toArray(fs.createReadStream(file.path));
|
|
|
|
const buffers = parts.map(part => (_.isBuffer(part) ? part : Buffer.from(part)));
|
|
|
|
|
|
|
|
const buffer = Buffer.concat(buffers);
|
|
|
|
|
|
|
|
const formattedFile = this.formatFileInfo(
|
|
|
|
{
|
|
|
|
filename: file.name,
|
|
|
|
type: file.type,
|
|
|
|
size: file.size,
|
|
|
|
},
|
|
|
|
fileInfo,
|
|
|
|
metas
|
|
|
|
);
|
2019-08-01 08:52:35 +02:00
|
|
|
|
2020-03-03 15:54:59 +01:00
|
|
|
return _.assign(formattedFile, {
|
|
|
|
buffer,
|
|
|
|
});
|
2018-02-19 14:26:20 +01:00
|
|
|
},
|
|
|
|
|
2020-03-05 13:51:15 +01:00
|
|
|
async upload({ data, files }) {
|
|
|
|
const { fileInfo, ...metas } = data;
|
2018-02-19 15:41:26 +01:00
|
|
|
|
2020-03-05 13:51:15 +01:00
|
|
|
const fileArray = Array.isArray(files) ? files : [files];
|
|
|
|
const fileInfoArray = Array.isArray(fileInfo) ? fileInfo : [fileInfo];
|
2018-02-19 15:41:26 +01:00
|
|
|
|
2020-03-05 13:51:15 +01:00
|
|
|
const doUpload = async (file, fileInfo) => {
|
|
|
|
const fileData = await this.enhanceFile(file, fileInfo, metas);
|
2018-03-07 14:18:15 +01:00
|
|
|
|
2020-03-05 13:51:15 +01:00
|
|
|
return this.uploadFileAndPersist(fileData);
|
2019-07-15 18:41:23 +02:00
|
|
|
};
|
2019-02-04 13:17:59 -03:00
|
|
|
|
2020-03-05 13:51:15 +01:00
|
|
|
return await Promise.all(
|
|
|
|
fileArray.map((file, idx) => doUpload(file, fileInfoArray[idx] || {}))
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
async uploadFileAndPersist(fileData) {
|
|
|
|
const config = strapi.plugins.upload.config;
|
2020-03-05 17:48:07 +01:00
|
|
|
|
|
|
|
const { getDimensions, generateThumbnail } = strapi.plugins.upload.services[
|
|
|
|
'image-manipulation'
|
|
|
|
];
|
|
|
|
|
2020-03-05 13:51:15 +01:00
|
|
|
await strapi.plugins.upload.provider.upload(fileData);
|
|
|
|
|
2020-03-05 17:48:07 +01:00
|
|
|
const thumbnailFile = await generateThumbnail(fileData);
|
|
|
|
if (thumbnailFile) {
|
|
|
|
await strapi.plugins.upload.provider.upload(thumbnailFile);
|
|
|
|
delete thumbnailFile.buffer;
|
2020-03-09 09:02:51 +01:00
|
|
|
_.set(fileData, 'formats.thumbnail', thumbnailFile);
|
2020-03-05 17:48:07 +01:00
|
|
|
}
|
|
|
|
|
2020-03-05 14:31:06 +01:00
|
|
|
const { width, height } = await getDimensions(fileData.buffer);
|
2020-03-05 17:48:07 +01:00
|
|
|
|
2020-03-05 13:51:15 +01:00
|
|
|
delete fileData.buffer;
|
2020-03-05 14:31:06 +01:00
|
|
|
|
|
|
|
_.assign(fileData, {
|
|
|
|
provider: config.provider,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
});
|
2020-03-05 13:51:15 +01:00
|
|
|
|
|
|
|
const res = await this.add(fileData);
|
|
|
|
|
|
|
|
strapi.eventHub.emit('media.create', { media: res });
|
|
|
|
return res;
|
2018-02-19 15:41:26 +01:00
|
|
|
},
|
|
|
|
|
2020-03-05 13:51:15 +01:00
|
|
|
async replace(id, { data, file }) {
|
2020-03-04 10:31:32 +01:00
|
|
|
const config = strapi.plugins.upload.config;
|
|
|
|
|
2020-03-05 17:58:56 +01:00
|
|
|
const { getDimensions, generateThumbnail } = strapi.plugins.upload.services[
|
|
|
|
'image-manipulation'
|
|
|
|
];
|
|
|
|
|
2020-03-05 13:51:15 +01:00
|
|
|
const dbFile = await this.fetch({ id });
|
|
|
|
|
|
|
|
if (!dbFile) {
|
2020-03-09 09:02:51 +01:00
|
|
|
throw strapi.errors.notFound('file not found');
|
2020-03-05 13:51:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const { fileInfo } = data;
|
|
|
|
const fileData = await this.enhanceFile(file, fileInfo);
|
|
|
|
|
2020-03-04 10:31:32 +01:00
|
|
|
// keep a constant hash
|
2020-03-05 13:51:15 +01:00
|
|
|
_.assign(fileData, {
|
2020-03-04 10:31:32 +01:00
|
|
|
hash: dbFile.hash,
|
|
|
|
ext: dbFile.ext,
|
|
|
|
});
|
|
|
|
|
|
|
|
// execute delete function of the provider
|
|
|
|
if (dbFile.provider === config.provider) {
|
|
|
|
await strapi.plugins.upload.provider.delete(dbFile);
|
2020-03-05 17:58:56 +01:00
|
|
|
|
2020-03-09 09:02:51 +01:00
|
|
|
if (_.has(dbFile, 'formats.thumbnail')) {
|
|
|
|
await strapi.plugins.upload.provider.delete(_.get(dbFile, 'formats.thumbnail'));
|
2020-03-05 17:58:56 +01:00
|
|
|
}
|
2020-03-04 10:31:32 +01:00
|
|
|
}
|
|
|
|
|
2020-03-05 13:51:15 +01:00
|
|
|
await strapi.plugins.upload.provider.upload(fileData);
|
2020-03-04 10:31:32 +01:00
|
|
|
|
2020-03-05 17:58:56 +01:00
|
|
|
const thumbnailFile = await generateThumbnail(fileData);
|
|
|
|
if (thumbnailFile) {
|
|
|
|
await strapi.plugins.upload.provider.upload(thumbnailFile);
|
|
|
|
delete thumbnailFile.buffer;
|
2020-03-09 09:02:51 +01:00
|
|
|
_.set(fileData, 'formats.thumbnail', thumbnailFile);
|
2020-03-05 17:58:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const { width, height } = await getDimensions(fileData.buffer);
|
2020-03-05 13:51:15 +01:00
|
|
|
delete fileData.buffer;
|
2020-03-05 17:58:56 +01:00
|
|
|
|
|
|
|
_.assign(fileData, {
|
|
|
|
provider: config.provider,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
});
|
2020-03-04 10:31:32 +01:00
|
|
|
|
2020-03-05 13:51:15 +01:00
|
|
|
const res = await this.update({ id }, fileData);
|
2020-03-04 10:31:32 +01:00
|
|
|
strapi.eventHub.emit('media.update', { media: res });
|
|
|
|
|
|
|
|
return res;
|
|
|
|
},
|
|
|
|
|
2020-03-05 13:51:15 +01:00
|
|
|
update(params, values) {
|
|
|
|
return strapi.query('file', 'upload').update(params, values);
|
2020-03-04 10:31:32 +01:00
|
|
|
},
|
|
|
|
|
2019-07-15 16:54:35 +02:00
|
|
|
add(values) {
|
2019-07-15 16:25:45 +02:00
|
|
|
return strapi.query('file', 'upload').create(values);
|
2018-02-19 15:41:26 +01:00
|
|
|
},
|
|
|
|
|
2019-07-15 16:25:45 +02:00
|
|
|
fetch(params) {
|
2020-03-05 13:51:15 +01:00
|
|
|
return strapi.query('file', 'upload').findOne(params);
|
2018-02-19 15:41:26 +01:00
|
|
|
},
|
|
|
|
|
2019-07-15 16:25:45 +02:00
|
|
|
fetchAll(params) {
|
|
|
|
return strapi.query('file', 'upload').find(params);
|
2018-02-19 15:41:26 +01:00
|
|
|
},
|
|
|
|
|
2019-07-15 18:41:23 +02:00
|
|
|
count(params) {
|
2019-07-15 18:28:56 +02:00
|
|
|
return strapi.query('file', 'upload').count(params);
|
2018-02-19 19:54:45 +01:00
|
|
|
},
|
|
|
|
|
2020-02-26 19:38:23 +01:00
|
|
|
async remove(file) {
|
|
|
|
const config = strapi.plugins.upload.config;
|
|
|
|
|
2018-02-21 17:18:33 +01:00
|
|
|
// execute delete function of the provider
|
2020-02-26 19:38:23 +01:00
|
|
|
if (file.provider === config.provider) {
|
2020-02-27 19:34:14 +01:00
|
|
|
await strapi.plugins.upload.provider.delete(file);
|
2020-03-05 17:48:07 +01:00
|
|
|
|
2020-03-09 09:02:51 +01:00
|
|
|
if (_.has(file, 'formats.thumbnail')) {
|
|
|
|
await strapi.plugins.upload.provider.delete(_.get(file, 'formats.thumbnail'));
|
2020-03-05 17:48:07 +01:00
|
|
|
}
|
2018-03-07 14:18:15 +01:00
|
|
|
}
|
2018-02-19 16:00:37 +01:00
|
|
|
|
2020-01-10 12:04:58 +01:00
|
|
|
const media = await strapi.query('file', 'upload').findOne({
|
2019-12-17 20:59:57 +01:00
|
|
|
id: file.id,
|
|
|
|
});
|
|
|
|
|
2020-01-08 11:12:41 +01:00
|
|
|
strapi.eventHub.emit('media.delete', { media });
|
2019-12-17 20:59:57 +01:00
|
|
|
|
2019-07-15 18:28:56 +02:00
|
|
|
return strapi.query('file', 'upload').delete({ id: file.id });
|
2018-02-28 12:33:32 +01:00
|
|
|
},
|
|
|
|
|
2019-07-15 16:25:45 +02:00
|
|
|
async uploadToEntity(params, files, source) {
|
2020-03-03 15:54:59 +01:00
|
|
|
const { id, model, field } = params;
|
|
|
|
|
|
|
|
const arr = Array.isArray(files) ? files : [files];
|
|
|
|
return Promise.all(
|
|
|
|
arr.map(file => {
|
|
|
|
return this.enhanceFile(
|
|
|
|
file,
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
refId: id,
|
|
|
|
ref: model,
|
|
|
|
source,
|
|
|
|
field,
|
|
|
|
}
|
|
|
|
);
|
2019-07-15 16:25:45 +02:00
|
|
|
})
|
2020-03-05 13:51:15 +01:00
|
|
|
).then(files => this.uploadFileAndPersist(files));
|
2018-09-10 16:05:00 +08:00
|
|
|
},
|
2020-03-03 15:54:59 +01:00
|
|
|
|
2020-02-24 17:39:15 +01:00
|
|
|
async getConfig() {
|
|
|
|
const config = await strapi
|
|
|
|
.store({
|
|
|
|
environment: strapi.config.environment,
|
|
|
|
type: 'plugin',
|
|
|
|
name: 'upload',
|
|
|
|
})
|
|
|
|
.get({ key: 'provider' });
|
|
|
|
|
|
|
|
return { ...config, sizeLimit: parseFloat(config.sizeLimit) };
|
|
|
|
},
|
2019-07-15 16:25:45 +02:00
|
|
|
};
|