csa-backend-test/app/services/file.services.js

68 lines
1.9 KiB
JavaScript

// LIBRARIES
const { v4: uuidv4 } = require("uuid");
const { minioClient } = require("./minio.services.js");
const mime = require("mime-types");
const path = require("path");
// ESSENTIALS
exports.upload = async (bucketName, folderName, mimetype, file) => {
try {
if (!file?.length) {
throw new Error("Invalid file!");
}
const fileContent = file[0];
const ext = mime.extension(mimetype) || path.extname(fileContent.originalname);
const fileName = `${uuidv4()}.${ext}`;
const fullPath = `${folderName}/${fileName}`;
const bucketExists = await minioClient.bucketExists(bucketName);
if (!bucketExists) {
await minioClient.makeBucket(bucketName, 'us-east-1');
const bucketPolicy = {
Version: "2012-10-17",
Statement: [
{
Effect: "Allow",
Principal: "*",
Action: ["s3:GetObject"],
Resource: [`arn:aws:s3:::${bucketName}/*`],
},
],
};
await minioClient.setBucketPolicy(bucketName, JSON.stringify(bucketPolicy));
}
await minioClient.putObject(bucketName, fullPath, fileContent.buffer, {
"Content-Type": mimetype,
});
const fileUrl = `https://${minioClient.host}:${minioClient.port}/${bucketName}/${fullPath}`;
return [fileUrl, fileName];
} catch (error) {
throw new Error(error.message);
}
};
exports.delete = async (bucketName, folderName, fileName) => {
try {
if (!fileName) {
throw new Error("Invalid file name");
}
const fullPath = `${folderName}/${fileName}`;
await minioClient.removeObject(bucketName, fullPath);
return true;
} catch (error) {
throw new Error(error.message);
}
};