diff --git a/frontend/src/auth/login.tsx b/frontend/src/auth/login.tsx
index c5c3eed0b4e897187d9944e53ef31bc040c9fe4c..3374530e0eceaaf3d5b16224d71e320555d26c47 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 a3be100083d390a158592ba35bf0ad4a5a2ef1f9..2a1b9f709653fef34c3d04b6672538dfd21b7f78 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) => (

-
diff --git a/frontend/src/home/header.tsx b/frontend/src/home/header.tsx
index ff3c9c08dcec4f89921e3476ab8c0c33b771608f..75162ba22063ebba8474ca0a22c44312fd6835b1 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 a4617babb5c9a57d4a569a1f07c2bc3f06ff91b2..48ee863c6a3aa2d3c7a94d337b9530fee6e8462f 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 a3d1c435950c8bc3dc95028830431aaab84f38f6..3e95bfcc12d7c47203f97c0fd7af9d699f5fb0e9 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 02267198e9027bc43b39c5c7887943760ff7bd06..3946c1236da0c0900577e7164321e3a00acca625 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 c01b8c2e9518e610695f50bfff3cf18ddeed2b2c..30a58de714733fef8282ae1329667c5628d0cba4 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 a89dfe161ed3fb3aefd6b851bdd2d448725727b1..83e53e1455d9dc69e6c66767e2c436673ffe3e5a 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;