mirror of
https://github.com/strapi/strapi.git
synced 2025-07-19 23:16:47 +00:00

the public folder shouldn't be hard-coded, it needs to follow the "strapi.config.public.path" value. I faced this issue on my current project where i have a custom path for the "public" folder for development, replacing: ``` path.join(strapi.config.appPath, 'public', `uploads/${file.hash}${file.ext}`) ``` with ``` path.join(strapi.config.public.path, `/uploads/${file.hash}${file.ext}`) ``` solved my problem, so i need to notify you on this issue.
51 lines
1.2 KiB
JavaScript
51 lines
1.2 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* Module dependencies
|
|
*/
|
|
|
|
// Public node modules.
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
/* eslint-disable no-unused-vars */
|
|
module.exports = {
|
|
provider: 'local',
|
|
name: 'Local server',
|
|
init: (config) => {
|
|
return {
|
|
upload: (file) => {
|
|
return new Promise((resolve, reject) => {
|
|
// write file in public/assets folder
|
|
fs.writeFile(path.join(strapi.config.public.path, `/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(strapi.config.public.path, `/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();
|
|
});
|
|
});
|
|
}
|
|
};
|
|
}
|
|
};
|