S3 : news controller

This commit is contained in:
2026-06-19 12:26:40 +03:30
parent 99a9f9ace5
commit 85c8107799
3 changed files with 114 additions and 152 deletions
+28
View File
@@ -2,6 +2,7 @@ const sharp = require("sharp");
const s3Storage = require("./s3Storage");
const NEWS_IMAGE_PREFIX = "uploads/images/news";
const NEWS_ATTACHMENT_PREFIX = "uploads/files/newsAttachments";
const getStoredImageName = (doc, field) =>
doc.get(field, null, { getters: false });
@@ -38,9 +39,36 @@ const uploadProcessedImage = async (
return imageName;
};
const uploadNewsAttachment = async (
fileData,
fileName,
contentType = "application/pdf"
) => {
await s3Storage.uploadBuffer(
`${NEWS_ATTACHMENT_PREFIX}/${fileName}`,
fileData,
contentType
);
return fileName;
};
const deleteNewsAttachment = async (fileName) => {
if (!fileName) return;
try {
await s3Storage.deleteObject(`${NEWS_ATTACHMENT_PREFIX}/${fileName}`);
} catch (error) {
console.log(`Failed to delete attachment from S3:`, error?.message);
}
};
module.exports = {
NEWS_IMAGE_PREFIX,
NEWS_ATTACHMENT_PREFIX,
getStoredImageName,
deleteNewsImage,
uploadProcessedImage,
uploadNewsAttachment,
deleteNewsAttachment,
};