store file with uuid and store only name in db

This commit is contained in:
Guillaume Dorce 2022-09-02 10:18:08 +02:00
parent 6716937735
commit b3cdc9f518
4 changed files with 18 additions and 1 deletions

View File

@ -21,6 +21,7 @@
"@types/ms": "^0.7.31",
"@types/multer": "^1.4.7",
"@types/node": "^18.7.4",
"@types/uuid": "^8.3.4",
"nodemon": "^2.0.19",
"prisma": "^4.2.1",
"ts-node": "^10.9.1",
@ -37,6 +38,7 @@
"jsonwebtoken": "^8.5.1",
"ms": "^2.1.3",
"multer": "1.4.5-lts.1",
"uuid": "^8.3.2",
"zod": "^3.18.0"
}
}

View File

@ -9,6 +9,7 @@ specifiers:
'@types/ms': ^0.7.31
'@types/multer': ^1.4.7
'@types/node': ^18.7.4
'@types/uuid': ^8.3.4
bcrypt: ^5.0.1
body-parser: ^1.20.0
cors: ^2.8.5
@ -22,6 +23,7 @@ specifiers:
ts-node: ^10.9.1
tsconfig-paths: ^4.1.0
typescript: ^4.7.4
uuid: ^8.3.2
zod: ^3.18.0
dependencies:
@ -34,6 +36,7 @@ dependencies:
jsonwebtoken: 8.5.1
ms: 2.1.3
multer: 1.4.5-lts.1
uuid: 8.3.2
zod: 3.18.0
devDependencies:
@ -44,6 +47,7 @@ devDependencies:
'@types/ms': 0.7.31
'@types/multer': 1.4.7
'@types/node': 18.7.4
'@types/uuid': 8.3.4
nodemon: 2.0.19
prisma: 4.2.1
ts-node: 10.9.1_lpebteyakkup4lmzaiqtzvqavm
@ -210,6 +214,10 @@ packages:
'@types/node': 18.7.4
dev: true
/@types/uuid/8.3.4:
resolution: {integrity: sha512-c/I8ZRb51j+pYGAu5CrFMRxqZ2ke4y2grEBO5AUjgSkSk+qT2Ea+OdWElz/OiMf5MNpn2b17kuVBwZLQJXzihw==}
dev: true
/abbrev/1.1.1:
resolution: {integrity: sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==}
@ -1386,6 +1394,11 @@ packages:
engines: {node: '>= 0.4.0'}
dev: false
/uuid/8.3.2:
resolution: {integrity: sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==}
hasBin: true
dev: false
/v8-compile-cache-lib/3.0.1:
resolution: {integrity: sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==}
dev: true

View File

@ -6,6 +6,7 @@ import { Request, Response } from 'express';
export default async (req: Request, res: Response) => {
try {
req.body.authorId = req.userId;
req.body.image = req.file?.filename;
const post: Post = Post.parse(req.body);
const newPost: PrismaPost = await createPost(post);

View File

@ -1,10 +1,11 @@
import multer from 'multer';
import path from 'path';
import {v4 as uuidv4} from 'uuid';
const storage = multer.diskStorage({
destination: path.join(__dirname, '../../public/uploads'),
filename: (req, file, cb) => {
cb(null, file.originalname);
cb(null, uuidv4() + path.extname(file.originalname));
},
});