-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
48 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
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<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'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |