From 29d3c7196a138ef9953cc581ad715581964a0d74 Mon Sep 17 00:00:00 2001 From: Baek SeungMin Date: Wed, 27 Jul 2022 14:30:44 +0900 Subject: [PATCH] only css left --- frontend/src/Pages/myslide.tsx | 2 +- frontend/src/auth/login.tsx | 8 +- frontend/src/home/body.tsx | 6 +- frontend/src/home/header.tsx | 4 +- frontend/src/types/index.tsx | 3 +- src/controllers/fileinfo.controller.ts | 2 +- src/controllers/mainimg.controller.ts | 99 ++++++++++++---------- src/db/mainimg.db.ts | 112 ++++++++++++++++--------- src/routes/mainimg.route.ts | 4 +- 9 files changed, 142 insertions(+), 98 deletions(-) diff --git a/frontend/src/Pages/myslide.tsx b/frontend/src/Pages/myslide.tsx index cb1e0ba..78b32ec 100644 --- a/frontend/src/Pages/myslide.tsx +++ b/frontend/src/Pages/myslide.tsx @@ -48,7 +48,7 @@ export function MySlide({ slides}: num) { {slides.slice(page - 1, page + 2).map((slide) => (
-
+
{slide}
diff --git a/frontend/src/auth/login.tsx b/frontend/src/auth/login.tsx index c5c3eed..3374530 100644 --- a/frontend/src/auth/login.tsx +++ b/frontend/src/auth/login.tsx @@ -35,11 +35,9 @@ export default function Login() { setLoading(true); await login(user.email, user.password, () => { - if (user.email == "admin@korea.ac.kr" && user.password == "111111") { - navigate("/admin", { replace: true }); - } else { - navigate("/", { replace: true }); - } + + navigate("/", { replace: true }); + }); // console.log("서버연결됬나요", res); // console.log("로그인"); diff --git a/frontend/src/home/body.tsx b/frontend/src/home/body.tsx index a3be100..2a1b9f7 100644 --- a/frontend/src/home/body.tsx +++ b/frontend/src/home/body.tsx @@ -65,16 +65,16 @@ export default function Body() { Idpics.slice(i * limit, i * limit + limit).map( (picture, index: number) => (
-
+
{picture.title}
diff --git a/frontend/src/home/header.tsx b/frontend/src/home/header.tsx index ff3c9c0..75162ba 100644 --- a/frontend/src/home/header.tsx +++ b/frontend/src/home/header.tsx @@ -24,10 +24,10 @@ export default function Header() {
-
diff --git a/frontend/src/types/index.tsx b/frontend/src/types/index.tsx index a4617ba..48ee863 100644 --- a/frontend/src/types/index.tsx +++ b/frontend/src/types/index.tsx @@ -39,7 +39,8 @@ export interface SignupUser { export interface Profile { _id: string; email: string; - fileInfo: { + name: string; + avatar: { originalfilename: string; newfilename: string; picturepath: string; diff --git a/src/controllers/fileinfo.controller.ts b/src/controllers/fileinfo.controller.ts index a3d1c43..3e95bfc 100644 --- a/src/controllers/fileinfo.controller.ts +++ b/src/controllers/fileinfo.controller.ts @@ -4,7 +4,7 @@ import { TypedRequest } from "../types"; export const uploadFile = asyncWrap(async (reqExp, res, next) => { const req = reqExp as TypedRequest; - const form = formidable({ multiples: false, uploadDir: "uploads" }); + const form = formidable({ multiples: false, uploadDir: "uploads",keepExtensions : true }); await new Promise((resolve, reject) => { form.parse(req, (err, fields, files) => { diff --git a/src/controllers/mainimg.controller.ts b/src/controllers/mainimg.controller.ts index 0226719..3946c12 100644 --- a/src/controllers/mainimg.controller.ts +++ b/src/controllers/mainimg.controller.ts @@ -1,11 +1,11 @@ import { NextFunction, Request, Response } from "express"; import isLength from "validator/lib/isLength"; -import { TypedRequestAuth } from "./auth.controller"; import { asyncWrap } from "../helpers"; import { mainimgDb } from "../db"; import { TypedRequest } from "../types"; import { ObjectId } from "mongoose"; import formidable from "formidable"; +import { TypedRequestAuth } from "./auth.controller"; export const createMainimg = asyncWrap(async (reqExp, res) => { const req = reqExp as TypedRequestAuth<{ userId: ObjectId }>; @@ -65,49 +65,58 @@ export const deleteMainimg = asyncWrap(async (req, res) => { }); export const updateMainimg = asyncWrap(async (reqExp, res) => { - const req = reqExp as TypedRequestAuth<{ userId: ObjectId }>; + const req = reqExp as TypedRequest; - const form = formidable({ - uploadDir: "uploads", - keepExtensions: true, - multiples: false, - }); + // const { id } = reqExp.params; + const { id, theme, city, title } = req.body; + const { updatemainimg }: { updatemainimg: formidable.File } = req.files; +console.log("전부 제대로", id,theme, city, title, updatemainimg) + const img = await mainimgDb.updateOneMainimg(id, theme,city,title, updatemainimg); - form.parse(req, (err, fields, files) => { - if (!Array.isArray(files.updatemainimg)) { - //파일 좁히기 중 - if ( - !( - Array.isArray(fields.id) || - Array.isArray(fields.city) || - Array.isArray(fields.title) || - Array.isArray(fields.theme) - ) - ) { - const id = fields.id; - const city = fields.city; - const title = fields.title; - const theme = fields.theme; - console.log("error"); - if (!(files.updatemainimg === undefined)) { - const originalfilename = files.updatemainimg?.originalFilename; - const newfilename = files.updatemainimg.newFilename; - if (!(originalfilename === null || newfilename === undefined)) { - const imgRes = mainimgDb.updateOneMainimg( - id, - theme, - city, - title, - originalfilename, - newfilename - ); - return res.json(imgRes); - } - } else { - const imgRes = mainimgDb.updateOneMainimg(id, theme, city, title); - return res.json(imgRes); - } - } - } - }); -}); + res.json(img); +}) +// const form = formidable({ +// uploadDir: "uploads", +// keepExtensions: true, +// multiples: false, +// }); + +// form.parse(req, (err, fields, files) => { +// if (!Array.isArray(files.updatemainimg)) { +// //파일 좁히기 중 +// if ( +// !( +// Array.isArray(fields.id) || +// Array.isArray(fields.city) || +// Array.isArray(fields.title) || +// Array.isArray(fields.theme) +// ) +// ) { +// const id = fields.id; +// const city = fields.city; +// const title = fields.title; +// const theme = fields.theme; +// console.log("error"); +// if (!(files.updatemainimg === undefined)) { +// const originalfilename = files.updatemainimg?.originalFilename; +// const newfilename = files.updatemainimg.newFilename; +// if (!(originalfilename === null || newfilename === undefined)) { +// const imgRes = mainimgDb.updateOneMainimg( +// id, +// theme, +// city, +// title, +// // fileInfo +// originalfilename, +// newfilename +// ); +// return res.json(imgRes); +// } +// } else { +// const imgRes = mainimgDb.updateOneMainimg(id, theme, city, title); +// return res.json(imgRes); +// } +// } +// } +// }); +// }); diff --git a/src/db/mainimg.db.ts b/src/db/mainimg.db.ts index c01b8c2..30a58de 100644 --- a/src/db/mainimg.db.ts +++ b/src/db/mainimg.db.ts @@ -1,13 +1,14 @@ -import { ObjectId } from "mongoose"; +import { HydratedDocument, ObjectId } from "mongoose"; import { FileInfo, IFileInfo, Mainimg, MainimgType } from "../models"; import fs from "fs/promises"; import { fileInfoCtrl } from "../controllers"; +import formidable from "formidable"; export const createMainimg = async (mainimg: MainimgType, pic: IFileInfo) => { const newPic = await FileInfo.create({ originalfilename: pic.originalfilename, newfilename: pic.newfilename, - pictureauth: pic.picturepath, + picturepath: pic.picturepath, }); const newMainimg = await Mainimg.create({ @@ -43,41 +44,76 @@ export const updateOneMainimg = async ( theme: string, city: string, title: string, - originalfilename?: string | null, - newfilename?: string + fileInfo: formidable.File + // originalfilename?: string | null, + // newfilename?: string ) => { - const newMainimg = await Mainimg.findById(_Id); - console.log("error2", _Id); - if (!(newMainimg?.fileInfo === undefined)) { - if (originalfilename === undefined) { - await Mainimg.findByIdAndUpdate(newMainimg._id, { - theme: theme, - city: city, - title: title, - }); - console.log("errrror4"); - } else if ( - !(originalfilename === undefined) && - (!(theme === undefined) || - !(city === undefined) || - !(title === undefined)) - ) { - await Mainimg.findByIdAndUpdate(newMainimg._id, { - theme: theme, - city: city, - title: title, - }); - await FileInfo.findByIdAndUpdate(newMainimg.fileInfo._id, { - originalfilename: originalfilename, - newfilename: newfilename, - }); - console.log("error6"); - } else { - await FileInfo.findByIdAndUpdate(newMainimg.fileInfo._id, { - originalfilename: originalfilename, - newfilename: newfilename, - }); - console.log("error5", newfilename, originalfilename, theme, city, title); + // const newMainimg = await Mainimg.findById(_Id) + const newMainimg = await Mainimg.findById(_Id).populate<{ fileInfo: IFileInfo }>( + "fileInfo" + ); + + console.log("error2", newMainimg); + if (!newMainimg) { + throw new Error("mainimg가 존재하지 않습니다") + } + if ( + fileInfo.originalFilename && + newMainimg?.fileInfo.originalfilename !== fileInfo.originalFilename + ) { + // 같지 않으면 기존의 파일을 디스크에서 삭제한 후 + try { + console.log("picturepath", newMainimg.fileInfo.picturepath) + await fs.unlink(newMainimg.fileInfo.picturepath); + } catch (error) { + console.log("error", error); } - } else console.log("error3", newMainimg); -}; + const mainimgAvatar = newMainimg.fileInfo as HydratedDocument; + // 기존 fileinfo의 파일이름과 경로 변경 설정 + mainimgAvatar.originalfilename = fileInfo.originalFilename; + mainimgAvatar.newfilename = fileInfo.newFilename; + mainimgAvatar.picturepath = fileInfo.filepath; + await mainimgAvatar.save(); + } + + + newMainimg.theme = theme; + newMainimg.city = city; + newMainimg.title = title; + await newMainimg.save(); + console.log("mainimg updated", newMainimg); + return newMainimg; +} +// if (!(newMainimg?.fileInfo === undefined)) { +// if (originalfilename === undefined) { +// await Mainimg.findByIdAndUpdate(newMainimg._id, { +// theme: theme, +// city: city, +// title: title, +// }); +// console.log("errrror4"); +// } else if ( +// !(originalfilename === undefined) && +// (!(theme === undefined) || +// !(city === undefined) || +// !(title === undefined)) +// ) { +// await Mainimg.findByIdAndUpdate(newMainimg._id, { +// theme: theme, +// city: city, +// title: title, +// }); +// await FileInfo.findByIdAndUpdate(newMainimg.fileInfo._id, { +// originalfilename: originalfilename, +// newfilename: newfilename, +// }); +// console.log("error6"); +// } else { +// await FileInfo.findByIdAndUpdate(newMainimg.fileInfo._id, { +// originalfilename: originalfilename, +// newfilename: newfilename, +// }); +// console.log("error5", newfilename, originalfilename, theme, city, title); +// } +// } else console.log("error3", newMainimg); +// }; \ No newline at end of file diff --git a/src/routes/mainimg.route.ts b/src/routes/mainimg.route.ts index a89dfe1..83e53e1 100644 --- a/src/routes/mainimg.route.ts +++ b/src/routes/mainimg.route.ts @@ -1,5 +1,5 @@ import express from "express"; -import { mainimgCtrl, authCtrl } from "../controllers"; +import { mainimgCtrl, authCtrl, fileInfoCtrl } from "../controllers"; const router = express.Router(); @@ -11,7 +11,7 @@ router router .route("/:imgId") .delete(authCtrl.requireLogin,authCtrl.hasRole("admin"), mainimgCtrl.deleteMainimg) - .put(authCtrl.requireLogin,authCtrl.hasRole("admin"), mainimgCtrl.updateMainimg); + .put(authCtrl.requireLogin,authCtrl.hasRole("admin"),fileInfoCtrl.uploadFile, mainimgCtrl.updateMainimg); export default router; -- GitLab