diff --git a/backend/question-service/config/firebase.ts b/backend/question-service/config/firebase.ts index 8e69742b09..73ac0bf372 100644 --- a/backend/question-service/config/firebase.ts +++ b/backend/question-service/config/firebase.ts @@ -1,5 +1,4 @@ import admin from "firebase-admin"; -import { v4 as uuidv4 } from "uuid"; import serviceAccount from "../firebase.json"; admin.initializeApp({ @@ -9,34 +8,4 @@ admin.initializeApp({ const bucket = admin.storage().bucket(); -const uploadFileToFirebase = async ( - file: Express.Multer.File, -): Promise => { - return new Promise((resolve, reject) => { - const fileName = uuidv4(); - const ref = bucket.file(fileName); - - const blobStream = ref.createWriteStream({ - metadata: { - contentType: file.mimetype, - }, - }); - - blobStream.on("error", (error) => { - reject(error); - }); - - blobStream.on("finish", async () => { - try { - await ref.makePublic(); - resolve(`https://storage.googleapis.com/${bucket.name}/${fileName}`); - } catch (error) { - reject(error); - } - }); - - blobStream.end(file.buffer); - }); -}; - -export { bucket, uploadFileToFirebase }; +export { bucket }; diff --git a/backend/question-service/src/controllers/questionController.ts b/backend/question-service/src/controllers/questionController.ts index 4c74d581c3..e945c7d599 100644 --- a/backend/question-service/src/controllers/questionController.ts +++ b/backend/question-service/src/controllers/questionController.ts @@ -1,5 +1,4 @@ import { Request, Response } from "express"; -import { v4 as uuidv4 } from "uuid"; import Question from "../models/Question.ts"; import { checkIsExistingQuestion } from "../utils/utils.ts"; import { @@ -8,8 +7,8 @@ import { QN_DESC_CHAR_LIMIT, } from "../utils/constants.ts"; -import { bucket, uploadFileToFirebase } from "../../config/firebase"; import { upload } from "../../config/multer"; +import { uploadFileToFirebase } from "../utils/utils"; export const createQuestion = async ( req: Request, diff --git a/backend/question-service/src/utils/utils.ts b/backend/question-service/src/utils/utils.ts index 70bce97ed7..f6330915b8 100644 --- a/backend/question-service/src/utils/utils.ts +++ b/backend/question-service/src/utils/utils.ts @@ -1,4 +1,7 @@ import mongoose from "mongoose"; +import { v4 as uuidv4 } from "uuid"; + +import { bucket } from "../../config/firebase"; import Question from "../models/Question"; @@ -15,3 +18,33 @@ export const checkIsExistingQuestion = async ( _id: { $ne: objectIdToExclude }, // Exclude current question's ID if provided }); }; + +export const uploadFileToFirebase = async ( + file: Express.Multer.File, +): Promise => { + return new Promise((resolve, reject) => { + const fileName = uuidv4(); + const ref = bucket.file(fileName); + + const blobStream = ref.createWriteStream({ + metadata: { + contentType: file.mimetype, + }, + }); + + blobStream.on("error", (error) => { + reject(error); + }); + + blobStream.on("finish", async () => { + try { + await ref.makePublic(); + resolve(`https://storage.googleapis.com/${bucket.name}/${fileName}`); + } catch (error) { + reject(error); + } + }); + + blobStream.end(file.buffer); + }); +};