Bartosz 8c40d2192d
Fix invalid local uploads path (#6925)
* Fix invalid local uploads path

File upload provider does not consider the strapi configuration. This patch should fix the problem.

* getStaticPath -> uploadDir

* Use config.get method

* plugin-upload tests fix
2020-07-27 17:18:55 +02:00

73 lines
1.7 KiB
JavaScript

'use strict';
/**
* Module dependencies
*/
// Public node modules.
const fs = require('fs');
const path = require('path');
module.exports = {
init({ sizeLimit = 1000000 } = {}) {
const verifySize = file => {
if (file.size > sizeLimit) {
throw strapi.errors.badRequest('FileToBig', {
errors: [
{
id: 'Upload.status.sizeLimit',
message: `${file.name} file is bigger than limit size!`,
values: { file: file.name },
},
],
});
}
};
const uploadDir = path.join(
strapi.dir,
strapi.config.get('middleware.settings.public.path', strapi.config.paths.static)
);
return {
upload(file) {
verifySize(file);
return new Promise((resolve, reject) => {
// write file in public/assets folder
fs.writeFile(
path.join(uploadDir, `/uploads/${file.hash}${file.ext}`),
file.buffer,
err => {
if (err) {
return reject(err);
}
file.url = `/uploads/${file.hash}${file.ext}`;
resolve();
}
);
});
},
delete(file) {
return new Promise((resolve, reject) => {
const filePath = path.join(uploadDir, `/uploads/${file.hash}${file.ext}`);
if (!fs.existsSync(filePath)) {
return resolve("File doesn't exist");
}
// remove file from public/assets folder
fs.unlink(filePath, err => {
if (err) {
return reject(err);
}
resolve();
});
});
},
};
},
};