diff --git a/services/files/src/common/storage.ts b/services/files/src/common/storage.ts index 1803c568..345618d0 100644 --- a/services/files/src/common/storage.ts +++ b/services/files/src/common/storage.ts @@ -7,9 +7,17 @@ import { File } from "../models/file"; const storage = new Storage(); -export const uploadFile = async (file: Express.Multer.File, bucketName: string) => { +export const uploadFile = async ( + file: Express.Multer.File, + bucketName: string, + folder?: string +) => { const { originalname, buffer } = file; - const googleFileName = `${path.parse(originalname).name}_${Date.now()}`; + let folderName = ""; + if (folder !== undefined) { + folderName = `${folder}/`; + } + const googleFileName = `${folderName}${path.parse(originalname).name}_${Date.now()}`; const blob = storage.bucket(bucketName).file(googleFileName); const blobStream = blob.createWriteStream({ diff --git a/services/files/src/routes/file.ts b/services/files/src/routes/file.ts index 1fbe402f..ba50df6f 100644 --- a/services/files/src/routes/file.ts +++ b/services/files/src/routes/file.ts @@ -34,7 +34,7 @@ const multerMid = multer({ export const fileRoutes = express.Router(); -fileRoutes.route("/upload").post( +fileRoutes.route("/upload/:folder?").post( checkAbility("create", "File"), multerMid.single("file"), asyncHandler(async (req, res) => { @@ -44,7 +44,8 @@ fileRoutes.route("/upload").post( const googleFileName = await uploadFile( req.file, - config.common.googleCloud.storageBuckets.default + config.common.googleCloud.storageBuckets.default, + req.params.folder ); const file = await FileModel.create({