Updated profile and post capabilities
This commit is contained in:
@@ -34,7 +34,9 @@ const uploadImage = multer({
|
||||
},
|
||||
}).single("image");
|
||||
|
||||
function handlePostUpload(req, res, next) {
|
||||
const ALLOWED_IMAGE_PREFIXES = ["/posts/image/", "/profiles/image/"];
|
||||
|
||||
function handleImageUpload(req, res, next) {
|
||||
uploadImage(req, res, (err) => {
|
||||
if (!err) return next();
|
||||
if (err.code === "LIMIT_FILE_SIZE") {
|
||||
@@ -44,26 +46,47 @@ function handlePostUpload(req, res, next) {
|
||||
});
|
||||
}
|
||||
|
||||
function imageLinkForFile(file) {
|
||||
return file ? `/posts/image/${file.filename}` : null;
|
||||
function handlePostUpload(req, res, next) {
|
||||
return handleImageUpload(req, res, next);
|
||||
}
|
||||
|
||||
function handleProfileUpload(req, res, next) {
|
||||
return handleImageUpload(req, res, next);
|
||||
}
|
||||
|
||||
function imageLinkForFile(file, prefix = "/posts/image/") {
|
||||
return file ? `${prefix}${file.filename}` : null;
|
||||
}
|
||||
|
||||
function profileImageLinkForFile(file) {
|
||||
return imageLinkForFile(file, "/profiles/image/");
|
||||
}
|
||||
|
||||
async function removeImageLink(imageLink) {
|
||||
if (!imageLink || !imageLink.startsWith("/posts/image/")) return;
|
||||
if (!imageLink || !ALLOWED_IMAGE_PREFIXES.some((prefix) => imageLink.startsWith(prefix))) {
|
||||
return;
|
||||
}
|
||||
const filename = path.basename(imageLink);
|
||||
await fs.promises.unlink(path.join(uploadDirectory, filename)).catch((err) => {
|
||||
if (err.code !== "ENOENT") throw err;
|
||||
});
|
||||
}
|
||||
|
||||
async function removeUploadedProfileFile(file) {
|
||||
if (file) await removeImageLink(profileImageLinkForFile(file));
|
||||
}
|
||||
|
||||
async function removeUploadedFile(file) {
|
||||
if (file) await removeImageLink(imageLinkForFile(file));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
handlePostUpload,
|
||||
handleProfileUpload,
|
||||
imageLinkForFile,
|
||||
profileImageLinkForFile,
|
||||
removeImageLink,
|
||||
removeUploadedFile,
|
||||
removeUploadedProfileFile,
|
||||
uploadDirectory,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user