diff --git a/.vscode/settings.json b/.vscode/settings.json index fc0584be1..27642e00d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,6 +5,7 @@ }, "eslint.nodePath": ".yarn/sdks", "prettier.prettierPath": ".yarn/sdks/prettier/index.js", + "prettier.configPath": ".prettierrc.js", "typescript.tsdk": ".yarn/sdks/typescript/lib", "typescript.enablePromptUseWorkspaceTsdk": true, "editor.formatOnSave": true, diff --git a/server/controllers/filesController.ts b/server/controllers/filesController.ts index e843f6265..bb0d305d9 100644 --- a/server/controllers/filesController.ts +++ b/server/controllers/filesController.ts @@ -1,25 +1,36 @@ import type { Request, Response } from 'express'; -// import { v4 as uuidv4 } from 'uuid'; +import fs from 'fs'; +import path from 'path'; -// import { uploadFile } from '../fileUpload'; +import { uploadFile } from '../fileUpload'; import { AppError, ErrorCode } from '../middlewares/handleErrors'; export async function uploadFiles(req: Request, res: Response) { - // const user = req.user; - // if (!user) { - // throw new AppError('Forbidden', ErrorCode.UNKNOWN); - // } - const { files } = req; - if (!files || !files.length) { - throw new AppError('Files are missing', ErrorCode.UNKNOWN); + try { + // const user = req.user; + // if (!user) { + // throw new AppError('Forbidden', ErrorCode.UNKNOWN); + // } + const { files } = req; + if (!files || !files.length) { + throw new AppError('Files are missing', ErrorCode.UNKNOWN); + } + const userId = 1; + const promises: Promise[] = []; + for (const file of files as Express.Multer.File[]) { + // making the filename being the path here is a trick to use + // upload function... + const filename = `images/${userId}/${file.filename}`; + const promise = uploadFile(filename, file.mimetype); + promises.push(promise); + } + const results = await Promise.all(promises); + res.status(200).json(results); + } catch (error) { + if (error instanceof AppError) { + res.status(error.errorCode).json(error.message); + } else { + res.status(500).json('Internal server error'); + } } - // console.log(files); - // const promises: Promise[] = []; - // for (const file of files as Express.Multer.File[]) { - // const promise = uploadFile(file.filename, file.mimetype); - // promises.push(promise); - // } - // const results = await Promise.all(promises); - // res.status(200).json(results); - res.status(200).json('results'); } diff --git a/server/controllers/multer.ts b/server/controllers/multer.ts index 078ec6684..df459d1c3 100644 --- a/server/controllers/multer.ts +++ b/server/controllers/multer.ts @@ -1,3 +1,4 @@ +import fs from 'fs'; import multer from 'multer'; import path from 'path'; import { v4 } from 'uuid'; @@ -13,3 +14,17 @@ export const diskStorage = multer.diskStorage({ }); export const upload = multer({ storage: diskStorage }); + +export const diskStorageToImages = multer.diskStorage({ + destination: function (_req, _file, cb) { + const dirPath = path.join(__dirname, '../fileUpload/images/'); + if (!fs.existsSync(dirPath)) { + fs.mkdirSync(dirPath); + } + cb(null, dirPath); + }, + filename: function (_req, file, cb) { + const uuid = v4(); + cb(null, `${uuid}${path.extname(file.originalname)}`); + }, +}); diff --git a/server/routes/filesRouter.ts b/server/routes/filesRouter.ts index 81b52ce48..729cc05cf 100644 --- a/server/routes/filesRouter.ts +++ b/server/routes/filesRouter.ts @@ -1,16 +1,12 @@ import { Router } from 'express'; +import multer from 'multer'; import { UserType } from '../../types/user.type'; import { uploadFiles } from '../controllers/filesController'; -import { upload } from '../controllers/multer'; +import { diskStorageToImages } from '../controllers/multer'; import { authenticate } from '../middlewares/authenticate'; import { handleErrors } from '../middlewares/handleErrors'; export const filesRouter = Router(); -filesRouter.post( - '/', - upload.array('files'), - // handleErrors(authenticate(UserType.ADMIN)), - uploadFiles, -); +filesRouter.post('/', multer({ storage: diskStorageToImages }).array('files'), handleErrors(authenticate(UserType.ADMIN)), uploadFiles);