2018-02-20 15:57:34 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Public node modules.
|
2022-01-04 19:21:05 +01:00
|
|
|
const { pipeline } = require('stream');
|
2018-02-20 15:57:34 +01:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2022-02-15 14:20:25 +01:00
|
|
|
const fse = require('fs-extra');
|
2023-02-08 15:57:14 +01:00
|
|
|
const {
|
|
|
|
errors: { PayloadTooLargeError },
|
|
|
|
file: { kbytesToBytes, bytesToHumanReadable },
|
|
|
|
} = require('@strapi/utils');
|
2020-02-26 19:38:23 +01:00
|
|
|
|
2022-02-15 14:20:25 +01:00
|
|
|
const UPLOADS_FOLDER_NAME = 'uploads';
|
|
|
|
|
2018-02-20 15:57:34 +01:00
|
|
|
module.exports = {
|
2022-09-16 15:26:29 +02:00
|
|
|
init({ sizeLimit: providerOptionsSizeLimit } = {}) {
|
|
|
|
// TODO V5: remove providerOptions sizeLimit
|
|
|
|
if (providerOptionsSizeLimit) {
|
2022-09-13 16:14:34 +02:00
|
|
|
process.emitWarning(
|
|
|
|
`[deprecated] In future versions, "sizeLimit" argument will be ignored from upload.config.providerOptions. Move it to upload.config`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-15 14:20:25 +01:00
|
|
|
// Ensure uploads folder exists
|
2022-03-14 17:54:35 +01:00
|
|
|
const uploadPath = path.resolve(strapi.dirs.static.public, UPLOADS_FOLDER_NAME);
|
2022-03-02 15:52:32 +01:00
|
|
|
if (!fse.pathExistsSync(uploadPath)) {
|
2022-02-22 11:37:21 +01:00
|
|
|
throw new Error(
|
2022-03-02 15:52:32 +01:00
|
|
|
`The upload folder (${uploadPath}) doesn't exist or is not accessible. Please make sure it exists.`
|
2022-02-22 11:37:21 +01:00
|
|
|
);
|
|
|
|
}
|
2020-02-26 19:38:23 +01:00
|
|
|
|
2018-02-20 17:10:25 +01:00
|
|
|
return {
|
2022-09-16 15:26:29 +02:00
|
|
|
checkFileSize(file, { sizeLimit } = {}) {
|
|
|
|
// TODO V5: remove providerOptions sizeLimit
|
|
|
|
if (providerOptionsSizeLimit) {
|
2022-09-23 14:44:20 +02:00
|
|
|
if (kbytesToBytes(file.size) > providerOptionsSizeLimit)
|
|
|
|
throw new PayloadTooLargeError(
|
|
|
|
`${file.name} exceeds size limit of ${bytesToHumanReadable(
|
|
|
|
providerOptionsSizeLimit
|
|
|
|
)}.`
|
|
|
|
);
|
2022-09-16 15:26:29 +02:00
|
|
|
} else if (sizeLimit) {
|
2022-09-23 14:44:20 +02:00
|
|
|
if (kbytesToBytes(file.size) > sizeLimit)
|
|
|
|
throw new PayloadTooLargeError(
|
|
|
|
`${file.name} exceeds size limit of ${bytesToHumanReadable(sizeLimit)}.`
|
|
|
|
);
|
2022-09-16 15:26:29 +02:00
|
|
|
}
|
|
|
|
},
|
2022-01-04 19:21:05 +01:00
|
|
|
uploadStream(file) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
pipeline(
|
|
|
|
file.stream,
|
2022-03-02 15:32:36 +01:00
|
|
|
fs.createWriteStream(path.join(uploadPath, `${file.hash}${file.ext}`)),
|
2022-08-08 23:33:39 +02:00
|
|
|
(err) => {
|
2022-01-04 19:21:05 +01:00
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
file.url = `/uploads/${file.hash}${file.ext}`;
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
2020-02-27 19:34:14 +01:00
|
|
|
upload(file) {
|
2018-02-20 15:57:34 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-02-21 17:18:33 +01:00
|
|
|
// write file in public/assets folder
|
2022-08-08 23:33:39 +02:00
|
|
|
fs.writeFile(path.join(uploadPath, `${file.hash}${file.ext}`), file.buffer, (err) => {
|
2022-02-15 14:20:25 +01:00
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
2018-02-20 15:57:34 +01:00
|
|
|
|
2022-02-15 14:20:25 +01:00
|
|
|
file.url = `/${UPLOADS_FOLDER_NAME}/${file.hash}${file.ext}`;
|
2018-02-20 15:57:34 +01:00
|
|
|
|
2022-02-15 14:20:25 +01:00
|
|
|
resolve();
|
|
|
|
});
|
2018-02-20 15:57:34 +01:00
|
|
|
});
|
|
|
|
},
|
2020-02-27 19:34:14 +01:00
|
|
|
delete(file) {
|
2018-02-20 15:57:34 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-02-21 11:59:30 +01:00
|
|
|
const filePath = path.join(uploadPath, `${file.hash}${file.ext}`);
|
2018-02-23 14:57:58 +01:00
|
|
|
|
|
|
|
if (!fs.existsSync(filePath)) {
|
2022-08-08 23:33:39 +02:00
|
|
|
// eslint-disable-next-line no-promise-executor-return
|
2020-02-26 19:38:23 +01:00
|
|
|
return resolve("File doesn't exist");
|
2018-02-23 14:57:58 +01:00
|
|
|
}
|
|
|
|
|
2018-02-21 17:18:33 +01:00
|
|
|
// remove file from public/assets folder
|
2022-08-08 23:33:39 +02:00
|
|
|
fs.unlink(filePath, (err) => {
|
2018-02-20 15:57:34 +01:00
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
2020-02-26 19:38:23 +01:00
|
|
|
},
|
2018-02-20 15:57:34 +01:00
|
|
|
};
|
2020-02-26 19:38:23 +01:00
|
|
|
},
|
2018-02-20 15:57:34 +01:00
|
|
|
};
|