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');
|
2020-03-08 18:43:50 +01:00
|
|
|
const util = require('util');
|
2020-03-03 15:54:59 +01:00
|
|
|
const filenamify = require('filenamify');
|
2020-04-03 13:35:33 +02:00
|
|
|
const mime = require('mime-types');
|
|
|
|
|
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
|
|
|
|
2020-04-27 16:08:55 +02:00
|
|
|
const sendMediaMetrics = data => {
|
|
|
|
if (_.has(data, 'caption') && !_.isEmpty(data.caption)) {
|
|
|
|
strapi.telemetry.send('didSaveMediaWithCaption');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_.has(data, 'alternativeText') && !_.isEmpty(data.alternativeText)) {
|
|
|
|
strapi.telemetry.send('didSaveMediaWithAlternativeText');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-31 16:56:49 +02:00
|
|
|
const combineFilters = params => {
|
|
|
|
// FIXME: until we support boolean operators for querying we need to make mime_ncontains use AND instead of OR
|
|
|
|
if (_.has(params, 'mime_ncontains') && Array.isArray(params.mime_ncontains)) {
|
|
|
|
params._where = params.mime_ncontains.map(val => ({ mime_ncontains: val }));
|
|
|
|
delete params.mime_ncontains;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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-04-16 10:50:18 +02:00
|
|
|
const ext = '.' + mime.extension(type) || path.extname(filename);
|
2020-04-03 13:35:33 +02:00
|
|
|
const baseName = path.basename(filename, path.extname(filename));
|
2020-03-03 15:54:59 +01:00
|
|
|
|
|
|
|
const usedName = fileInfo.name || baseName;
|
|
|
|
|
|
|
|
const entity = {
|
|
|
|
name: usedName,
|
|
|
|
alternativeText: fileInfo.alternativeText,
|
|
|
|
caption: fileInfo.caption,
|
|
|
|
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-08 18:43:50 +01:00
|
|
|
const readBuffer = await util.promisify(fs.readFile)(file.path);
|
2020-03-03 15:54:59 +01:00
|
|
|
|
2020-03-08 18:43:50 +01:00
|
|
|
const { optimize } = strapi.plugins.upload.services['image-manipulation'];
|
|
|
|
|
|
|
|
const { buffer, info } = await optimize(readBuffer);
|
2020-03-03 15:54:59 +01:00
|
|
|
|
|
|
|
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-08 18:43:50 +01:00
|
|
|
return _.assign(formattedFile, info, {
|
2020-03-03 15:54:59 +01:00
|
|
|
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
|
|
|
|
2020-03-08 20:03:45 +01:00
|
|
|
const {
|
|
|
|
getDimensions,
|
|
|
|
generateThumbnail,
|
|
|
|
generateResponsiveFormats,
|
|
|
|
} = strapi.plugins.upload.services['image-manipulation'];
|
2020-03-05 17:48:07 +01:00
|
|
|
|
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-08 20:03:45 +01:00
|
|
|
const formats = await generateResponsiveFormats(fileData);
|
2020-03-20 18:32:29 +01:00
|
|
|
if (Array.isArray(formats) && formats.length > 0) {
|
|
|
|
for (const format of formats) {
|
|
|
|
if (!format) continue;
|
2020-03-08 20:03:45 +01:00
|
|
|
|
2020-03-20 18:32:29 +01:00
|
|
|
const { key, file } = format;
|
2020-03-08 20:03:45 +01:00
|
|
|
|
2020-03-20 18:32:29 +01:00
|
|
|
await strapi.plugins.upload.provider.upload(file);
|
|
|
|
delete file.buffer;
|
2020-03-08 20:03:45 +01:00
|
|
|
|
2020-03-20 18:32:29 +01:00
|
|
|
_.set(fileData, ['formats', key], file);
|
|
|
|
}
|
2020-03-08 20:03:45 +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
|
|
|
|
2020-03-24 12:21:51 +01:00
|
|
|
return this.add(fileData);
|
|
|
|
},
|
2020-03-05 13:51:15 +01:00
|
|
|
|
2020-03-24 12:21:51 +01:00
|
|
|
async updateFileInfo(id, { name, alternativeText, caption }) {
|
|
|
|
const dbFile = await this.fetch({ id });
|
|
|
|
|
|
|
|
if (!dbFile) {
|
|
|
|
throw strapi.errors.notFound('file not found');
|
|
|
|
}
|
|
|
|
|
2020-03-27 16:00:26 +01:00
|
|
|
const newInfos = {
|
2020-03-24 12:21:51 +01:00
|
|
|
name: _.isNil(name) ? dbFile.name : name,
|
|
|
|
alternativeText: _.isNil(alternativeText) ? dbFile.alternativeText : alternativeText,
|
|
|
|
caption: _.isNil(caption) ? dbFile.caption : caption,
|
2020-03-27 16:00:26 +01:00
|
|
|
};
|
2020-03-24 12:21:51 +01:00
|
|
|
|
|
|
|
return this.update({ id }, newInfos);
|
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-08 20:03:45 +01:00
|
|
|
const {
|
|
|
|
getDimensions,
|
|
|
|
generateThumbnail,
|
|
|
|
generateResponsiveFormats,
|
|
|
|
} = strapi.plugins.upload.services['image-manipulation'];
|
2020-03-05 17:58:56 +01:00
|
|
|
|
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-08 20:03:45 +01:00
|
|
|
if (dbFile.formats) {
|
|
|
|
await Promise.all(
|
|
|
|
Object.keys(dbFile.formats).map(key => {
|
|
|
|
return strapi.plugins.upload.provider.delete(dbFile.formats[key]);
|
|
|
|
})
|
|
|
|
);
|
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-26 15:52:56 +01:00
|
|
|
// clear old formats
|
|
|
|
_.set(fileData, 'formats', {});
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-03-08 20:03:45 +01:00
|
|
|
const formats = await generateResponsiveFormats(fileData);
|
2020-03-20 18:32:29 +01:00
|
|
|
if (Array.isArray(formats) && formats.length > 0) {
|
|
|
|
for (const format of formats) {
|
|
|
|
if (!format) continue;
|
2020-03-08 20:03:45 +01:00
|
|
|
|
2020-03-20 18:32:29 +01:00
|
|
|
const { key, file } = format;
|
2020-03-08 20:03:45 +01:00
|
|
|
|
2020-03-20 18:32:29 +01:00
|
|
|
await strapi.plugins.upload.provider.upload(file);
|
|
|
|
delete file.buffer;
|
2020-03-08 20:03:45 +01:00
|
|
|
|
2020-03-20 18:32:29 +01:00
|
|
|
_.set(fileData, ['formats', key], file);
|
|
|
|
}
|
2020-03-08 20:03:45 +01:00
|
|
|
}
|
|
|
|
|
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-24 12:21:51 +01:00
|
|
|
return this.update({ id }, fileData);
|
2020-03-04 10:31:32 +01:00
|
|
|
},
|
|
|
|
|
2020-03-24 12:21:51 +01:00
|
|
|
async update(params, values) {
|
2020-04-27 16:08:55 +02:00
|
|
|
sendMediaMetrics(values);
|
|
|
|
|
2020-03-24 12:21:51 +01:00
|
|
|
const res = await strapi.query('file', 'upload').update(params, values);
|
|
|
|
strapi.eventHub.emit('media.update', { media: res });
|
|
|
|
return res;
|
2020-03-04 10:31:32 +01:00
|
|
|
},
|
|
|
|
|
2020-03-24 12:21:51 +01:00
|
|
|
async add(values) {
|
2020-04-27 16:08:55 +02:00
|
|
|
sendMediaMetrics(values);
|
|
|
|
|
2020-03-24 12:21:51 +01:00
|
|
|
const res = await strapi.query('file', 'upload').create(values);
|
|
|
|
strapi.eventHub.emit('media.create', { media: res });
|
|
|
|
return res;
|
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) {
|
2020-03-31 16:56:49 +02:00
|
|
|
combineFilters(params);
|
2019-07-15 16:25:45 +02:00
|
|
|
return strapi.query('file', 'upload').find(params);
|
2018-02-19 15:41:26 +01:00
|
|
|
},
|
|
|
|
|
2020-03-25 17:34:39 +01:00
|
|
|
search(params) {
|
|
|
|
return strapi.query('file', 'upload').search(params);
|
|
|
|
},
|
|
|
|
|
2020-03-25 19:17:46 +01:00
|
|
|
countSearch(params) {
|
|
|
|
return strapi.query('file', 'upload').countSearch(params);
|
|
|
|
},
|
|
|
|
|
2019-07-15 18:41:23 +02:00
|
|
|
count(params) {
|
2020-03-31 16:56:49 +02:00
|
|
|
combineFilters(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-08 20:03:45 +01:00
|
|
|
if (file.formats) {
|
|
|
|
await Promise.all(
|
|
|
|
Object.keys(file.formats).map(key => {
|
|
|
|
return strapi.plugins.upload.provider.delete(file.formats[key]);
|
|
|
|
})
|
|
|
|
);
|
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];
|
2020-03-16 17:41:16 +01:00
|
|
|
const enhancedFiles = await Promise.all(
|
2020-03-03 15:54:59 +01:00
|
|
|
arr.map(file => {
|
|
|
|
return this.enhanceFile(
|
|
|
|
file,
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
refId: id,
|
|
|
|
ref: model,
|
|
|
|
source,
|
|
|
|
field,
|
|
|
|
}
|
|
|
|
);
|
2019-07-15 16:25:45 +02:00
|
|
|
})
|
2020-03-16 17:41:16 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
await Promise.all(enhancedFiles.map(file => this.uploadFileAndPersist(file)));
|
2018-09-10 16:05:00 +08:00
|
|
|
},
|
2020-03-03 15:54:59 +01:00
|
|
|
|
2020-03-08 18:43:50 +01:00
|
|
|
getSettings() {
|
|
|
|
return strapi
|
2020-02-24 17:39:15 +01:00
|
|
|
.store({
|
|
|
|
type: 'plugin',
|
|
|
|
name: 'upload',
|
2020-03-08 12:59:21 +01:00
|
|
|
key: 'settings',
|
2020-02-24 17:39:15 +01:00
|
|
|
})
|
2020-03-08 12:59:21 +01:00
|
|
|
.get();
|
|
|
|
},
|
|
|
|
|
2020-03-08 18:43:50 +01:00
|
|
|
setSettings(value) {
|
2020-04-27 16:08:55 +02:00
|
|
|
if (value.responsiveDimensions === true) {
|
|
|
|
strapi.telemetry.send('didEnableResponsiveDimensions');
|
|
|
|
} else {
|
|
|
|
strapi.telemetry.send('didDisableResponsiveDimensions');
|
|
|
|
}
|
|
|
|
|
2020-03-08 18:43:50 +01:00
|
|
|
return strapi
|
2020-03-08 12:59:21 +01:00
|
|
|
.store({
|
|
|
|
type: 'plugin',
|
|
|
|
name: 'upload',
|
|
|
|
key: 'settings',
|
|
|
|
})
|
|
|
|
.set({ value });
|
2020-02-24 17:39:15 +01:00
|
|
|
},
|
2019-07-15 16:25:45 +02:00
|
|
|
};
|