mirror of
https://github.com/strapi/strapi.git
synced 2025-07-27 02:44:13 +00:00
Support thumbnail replacement
Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
parent
b144b19372
commit
2e0913d5d5
@ -131,6 +131,10 @@ module.exports = {
|
|||||||
async replace(id, { data, file }) {
|
async replace(id, { data, file }) {
|
||||||
const config = strapi.plugins.upload.config;
|
const config = strapi.plugins.upload.config;
|
||||||
|
|
||||||
|
const { getDimensions, generateThumbnail } = strapi.plugins.upload.services[
|
||||||
|
'image-manipulation'
|
||||||
|
];
|
||||||
|
|
||||||
const dbFile = await this.fetch({ id });
|
const dbFile = await this.fetch({ id });
|
||||||
|
|
||||||
if (!dbFile) {
|
if (!dbFile) {
|
||||||
@ -140,8 +144,6 @@ module.exports = {
|
|||||||
const { fileInfo } = data;
|
const { fileInfo } = data;
|
||||||
const fileData = await this.enhanceFile(file, fileInfo);
|
const fileData = await this.enhanceFile(file, fileInfo);
|
||||||
|
|
||||||
// TODO: maybe check if same extension ??
|
|
||||||
|
|
||||||
// keep a constant hash
|
// keep a constant hash
|
||||||
_.assign(fileData, {
|
_.assign(fileData, {
|
||||||
hash: dbFile.hash,
|
hash: dbFile.hash,
|
||||||
@ -151,12 +153,29 @@ module.exports = {
|
|||||||
// execute delete function of the provider
|
// execute delete function of the provider
|
||||||
if (dbFile.provider === config.provider) {
|
if (dbFile.provider === config.provider) {
|
||||||
await strapi.plugins.upload.provider.delete(dbFile);
|
await strapi.plugins.upload.provider.delete(dbFile);
|
||||||
|
|
||||||
|
if (dbFile.thumbnail !== null) {
|
||||||
|
await strapi.plugins.upload.provider.delete(dbFile.thumbnail);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await strapi.plugins.upload.provider.upload(fileData);
|
await strapi.plugins.upload.provider.upload(fileData);
|
||||||
|
|
||||||
|
const thumbnailFile = await generateThumbnail(fileData);
|
||||||
|
if (thumbnailFile) {
|
||||||
|
await strapi.plugins.upload.provider.upload(thumbnailFile);
|
||||||
|
delete thumbnailFile.buffer;
|
||||||
|
fileData.thumbnail = thumbnailFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { width, height } = await getDimensions(fileData.buffer);
|
||||||
delete fileData.buffer;
|
delete fileData.buffer;
|
||||||
fileData.provider = config.provider;
|
|
||||||
|
_.assign(fileData, {
|
||||||
|
provider: config.provider,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
});
|
||||||
|
|
||||||
const res = await this.update({ id }, fileData);
|
const res = await this.update({ id }, fileData);
|
||||||
strapi.eventHub.emit('media.update', { media: res });
|
strapi.eventHub.emit('media.update', { media: res });
|
||||||
|
Loading…
x
Reference in New Issue
Block a user