Skip to content

Commit

Permalink
finalized fileUpload route
Browse files Browse the repository at this point in the history
  • Loading branch information
Neo-Ryo committed Apr 30, 2024
1 parent 2e9d245 commit 34f3d87
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 25 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
47 changes: 29 additions & 18 deletions server/controllers/filesController.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
import type { Request, Response } from 'express';
// import { v4 as uuidv4 } from 'uuid';
import fs from 'fs';

Check warning on line 2 in server/controllers/filesController.ts

View workflow job for this annotation

GitHub Actions / lint

'fs' is defined but never used
import path from 'path';

Check warning on line 3 in server/controllers/filesController.ts

View workflow job for this annotation

GitHub Actions / lint

'path' is defined but never used

// 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<string | null>[] = [];
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<string | null>[] = [];
// 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');
}
15 changes: 15 additions & 0 deletions server/controllers/multer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'fs';
import multer from 'multer';
import path from 'path';
import { v4 } from 'uuid';
Expand All @@ -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)}`);
},
});
10 changes: 3 additions & 7 deletions server/routes/filesRouter.ts
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 34f3d87

Please sign in to comment.