Skip to content

Commit

Permalink
Merge pull request #196 from HackGT/reese/hexathonFiles
Browse files Browse the repository at this point in the history
Reese/hexathon files
  • Loading branch information
PhillipPhanhthourath authored Feb 19, 2024
2 parents 1360732 + e6445bf commit 6a1f678
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 10 additions & 2 deletions services/files/src/common/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
5 changes: 3 additions & 2 deletions services/files/src/routes/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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({
Expand Down

0 comments on commit 6a1f678

Please sign in to comment.