feat: add folder to filter:uploadImage and filter:uploadFile

v1.18.x
Barış Soner Uşaklı 5 years ago
parent 478c8dd72b
commit 7f24200c73

@ -243,7 +243,7 @@ async function uploadImage(filename, folder, uploadedFile, req, res, next) {
let imageData;
try {
if (plugins.hasListeners('filter:uploadImage')) {
imageData = await plugins.fireHook('filter:uploadImage', { image: uploadedFile, uid: req.uid });
imageData = await plugins.fireHook('filter:uploadImage', { image: uploadedFile, uid: req.uid, folder: folder });
} else {
imageData = await file.saveFileToLocal(filename, folder, uploadedFile.path);
}

@ -64,6 +64,7 @@ async function uploadAsImage(req, uploadedFile) {
return await plugins.fireHook('filter:uploadImage', {
image: uploadedFile,
uid: req.uid,
folder: 'files',
});
}
await image.isFileTypeAllowed(uploadedFile.path);
@ -129,6 +130,7 @@ uploadsController.uploadThumb = async function (req, res, next) {
return await plugins.fireHook('filter:uploadImage', {
image: uploadedFile,
uid: req.uid,
folder: 'files',
});
}
@ -141,6 +143,7 @@ uploadsController.uploadFile = async function (uid, uploadedFile) {
return await plugins.fireHook('filter:uploadFile', {
file: uploadedFile,
uid: uid,
folder: 'files',
});
}
@ -159,16 +162,16 @@ uploadsController.uploadFile = async function (uid, uploadedFile) {
throw new Error('[[error:invalid-file-type, ' + allowed.join(', ') + ']]');
}
return await saveFileToLocal(uid, uploadedFile);
return await saveFileToLocal(uid, 'files', uploadedFile);
};
async function saveFileToLocal(uid, uploadedFile) {
async function saveFileToLocal(uid, folder, uploadedFile) {
const name = uploadedFile.name || 'upload';
const extension = path.extname(name) || '';
const filename = Date.now() + '-' + validator.escape(name.substr(0, name.length - extension.length)).substr(0, 255) + extension;
const upload = await file.saveFileToLocal(filename, 'files', uploadedFile.path);
const upload = await file.saveFileToLocal(filename, folder, uploadedFile.path);
const storedFile = {
url: nconf.get('relative_path') + upload.url,
path: upload.path,

@ -149,6 +149,7 @@ image.uploadImage = async function (filename, folder, imageData) {
return await plugins.fireHook('filter:uploadImage', {
image: imageData,
uid: imageData.uid,
folder: folder,
});
}
await image.isFileTypeAllowed(imageData.path);

@ -40,7 +40,8 @@ module.exports = function (Topics) {
extension = '.' + mime.getExtension(type);
}
const filename = Date.now() + '-topic-thumb' + extension;
pathToUpload = path.join(nconf.get('upload_path'), 'files', filename);
const folder = 'files';
pathToUpload = path.join(nconf.get('upload_path'), folder, filename);
await pipeToFileAsync(data.thumb, pathToUpload);
@ -53,11 +54,15 @@ module.exports = function (Topics) {
});
if (!plugins.hasListeners('filter:uploadImage')) {
data.thumb = '/assets/uploads/files/' + filename;
data.thumb = '/assets/uploads/' + folder + '/' + filename;
return;
}
const uploadedFile = await plugins.fireHook('filter:uploadImage', { image: { path: pathToUpload, name: '' }, uid: data.uid });
const uploadedFile = await plugins.fireHook('filter:uploadImage', {
image: { path: pathToUpload, name: '' },
uid: data.uid,
folder: folder,
});
file.delete(pathToUpload);
data.thumb = uploadedFile.url;
} catch (err) {

Loading…
Cancel
Save