Skip to content

Commit

Permalink
Change file structure
Browse files Browse the repository at this point in the history
  • Loading branch information
guanquann committed Sep 20, 2024
1 parent 976fcf1 commit 21f3b88
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
33 changes: 1 addition & 32 deletions backend/question-service/config/firebase.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import admin from "firebase-admin";
import { v4 as uuidv4 } from "uuid";
import serviceAccount from "../firebase.json";

admin.initializeApp({
Expand All @@ -9,34 +8,4 @@ admin.initializeApp({

const bucket = admin.storage().bucket();

const uploadFileToFirebase = async (
file: Express.Multer.File,
): Promise<string> => {
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 };
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
Expand Down
33 changes: 33 additions & 0 deletions backend/question-service/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import mongoose from "mongoose";
import { v4 as uuidv4 } from "uuid";

import { bucket } from "../../config/firebase";

import Question from "../models/Question";

Expand All @@ -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<string> => {
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);
});
};

0 comments on commit 21f3b88

Please sign in to comment.