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');
|
|
|
|
|
|
|
|
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,
|
|
|
|
size: Math.round((size / 1000) * 100) / 100,
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
|
|
|
|
async enhanceFile(file, fileInfo, metas) {
|
|
|
|
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-02-26 19:38:23 +01:00
|
|
|
async upload(files) {
|
|
|
|
const config = strapi.plugins.upload.config;
|
|
|
|
|
2019-07-15 18:41:23 +02:00
|
|
|
// upload a single file
|
|
|
|
const uploadFile = async file => {
|
2020-02-27 19:34:14 +01:00
|
|
|
await strapi.plugins.upload.provider.upload(file);
|
2018-02-19 15:41:26 +01:00
|
|
|
|
2019-07-15 18:41:23 +02:00
|
|
|
delete file.buffer;
|
2020-02-27 19:34:14 +01:00
|
|
|
file.provider = config.provider;
|
2018-02-19 15:41:26 +01:00
|
|
|
|
2019-11-26 17:01:58 +01:00
|
|
|
const res = await this.add(file);
|
2018-03-07 14:18:15 +01:00
|
|
|
|
2020-01-08 11:12:41 +01:00
|
|
|
strapi.eventHub.emit('media.create', { media: res });
|
2019-07-15 18:41:23 +02:00
|
|
|
return res;
|
|
|
|
};
|
2019-02-04 13:17:59 -03:00
|
|
|
|
2019-07-15 18:41:23 +02:00
|
|
|
// Execute upload function of the provider for all files.
|
|
|
|
return Promise.all(files.map(file => uploadFile(file)));
|
2018-02-19 15:41:26 +01:00
|
|
|
},
|
|
|
|
|
2020-03-04 10:31:32 +01:00
|
|
|
async replace(dbFile, file) {
|
|
|
|
const config = strapi.plugins.upload.config;
|
|
|
|
|
|
|
|
// keep a constant hash
|
|
|
|
_.assign(file, {
|
|
|
|
hash: dbFile.hash,
|
|
|
|
ext: dbFile.ext,
|
|
|
|
});
|
|
|
|
|
|
|
|
// execute delete function of the provider
|
|
|
|
if (dbFile.provider === config.provider) {
|
|
|
|
await strapi.plugins.upload.provider.delete(dbFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
await strapi.plugins.upload.provider.upload(file);
|
|
|
|
|
|
|
|
delete file.buffer;
|
|
|
|
file.provider = config.provider;
|
|
|
|
|
|
|
|
const res = await this.update({ id: dbFile.id }, {});
|
|
|
|
strapi.eventHub.emit('media.update', { media: res });
|
|
|
|
|
|
|
|
return res;
|
|
|
|
},
|
|
|
|
|
|
|
|
update(id, values) {
|
|
|
|
return strapi.query('file', 'upload').update({ id }, values);
|
|
|
|
},
|
|
|
|
|
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) {
|
|
|
|
return strapi.query('file', 'upload').findOne({
|
2019-08-13 16:31:29 +02:00
|
|
|
id: params.id,
|
2019-07-15 16:25:45 +02:00
|
|
|
});
|
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);
|
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-03 15:54:59 +01:00
|
|
|
).then(files => this.upload(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
|
|
|
};
|