Commit 8fb026f5 authored by Kim, MinGyu's avatar Kim, MinGyu
Browse files

Avatar -> fileInfo

parent 0be923a8
...@@ -2,15 +2,7 @@ import React from "react"; ...@@ -2,15 +2,7 @@ import React from "react";
import { BrowserRouter, Route, Routes } from "react-router-dom"; import { BrowserRouter, Route, Routes } from "react-router-dom";
import "tailwindcss/tailwind.css"; import "tailwindcss/tailwind.css";
import { IntoPost } from "./post/intopost"; import { IntoPost } from "./post/intopost";
import { import { Login, Profile, RequireAuth, Signup, Admin, ImgRewrite } from "./auth";
Login,
Profile,
RequireAuth,
Signup,
Admin,
ImgRewrite,
Search,
} from "./auth";
import { Header, Body } from "./home"; import { Header, Body } from "./home";
import { Board } from "./board"; import { Board } from "./board";
import Posting from "./post/posting"; import Posting from "./post/posting";
...@@ -37,7 +29,6 @@ export const App = () => { ...@@ -37,7 +29,6 @@ export const App = () => {
<Route path="board" element={<Board />} /> <Route path="board" element={<Board />} />
<Route path="post/:postId" element={<IntoPost />} /> <Route path="post/:postId" element={<IntoPost />} />
<Route path="edit" element={<EditPost />} /> <Route path="edit" element={<EditPost />} />
{/* </Route> */}
<Route <Route
path="profile" path="profile"
element={ element={
...@@ -48,7 +39,6 @@ export const App = () => { ...@@ -48,7 +39,6 @@ export const App = () => {
/> />
<Route path="admin" element={<Admin />} /> <Route path="admin" element={<Admin />} />
<Route path="rewrite" element={<ImgRewrite />} /> <Route path="rewrite" element={<ImgRewrite />} />
<Route path="search" element={<Search />} />
</Route> </Route>
</Route> </Route>
</Routes> </Routes>
......
...@@ -7,9 +7,7 @@ export const RequireAuth: FC<{ children: JSX.Element }> = ({ children }) => { ...@@ -7,9 +7,7 @@ export const RequireAuth: FC<{ children: JSX.Element }> = ({ children }) => {
const location = useLocation(); const location = useLocation();
if (!user.isLoggedIn) { if (!user.isLoggedIn) {
return ( return <Navigate to={"/"} state={{ from: location.pathname }} replace />;
<Navigate to={"/login"} state={{ from: location.pathname }} replace />
);
} }
return children; return children;
}; };
import React, { FormEvent, useEffect, useState, MouseEvent } from "react"; import React, { FormEvent, useEffect, useState, MouseEvent } from "react";
import { Link } from "react-router-dom"; import { Link } from "react-router-dom";
import { mainimgApi } from "../apis"; import { mainimgApi } from "../apis";
import { picture } from "../apis/profile.api"; import { profileUpload } from "../apis/profile.api";
import { catchErrors } from "../helpers"; import { catchErrors } from "../helpers";
import { MainimgType } from "../types"; import { MainimgType } from "../types";
import { MySlide } from "./adminslide"; import { MySlide } from "./adminslide";
......
...@@ -4,4 +4,3 @@ export { default as Profile } from "./profile"; ...@@ -4,4 +4,3 @@ export { default as Profile } from "./profile";
export { RequireAuth } from "./RequireAuth"; export { RequireAuth } from "./RequireAuth";
export { default as Admin } from "./admin"; export { default as Admin } from "./admin";
export { default as ImgRewrite } from "./imgrewrite"; export { default as ImgRewrite } from "./imgrewrite";
export { default as Search } from "./search";
import React from "react";
import { useLocation } from "react-router-dom";
export default function Search() {
const a = useLocation().state;
return (
<div>
<div></div>
</div>
);
}
...@@ -31,7 +31,7 @@ export interface SignupUser { ...@@ -31,7 +31,7 @@ export interface SignupUser {
export interface Profile { export interface Profile {
_id: string; _id: string;
email: string; email: string;
avatar: { fileInfo: {
originalfilename: string; originalfilename: string;
newfilename: string; newfilename: string;
picturepath: string; picturepath: string;
......
...@@ -12,7 +12,6 @@ app.use(cookieParser()); ...@@ -12,7 +12,6 @@ app.use(cookieParser());
app.use("/api", router); app.use("/api", router);
app.use("/images", express.static(path.join(__dirname, "..", "/uploads"))); app.use("/images", express.static(path.join(__dirname, "..", "/uploads")));
app.use("/images", express.static(path.join(__dirname, "..", "/adminpics")));
app.use((err: any, req: Request, res: Response, next: NextFunction) => { app.use((err: any, req: Request, res: Response, next: NextFunction) => {
console.log("익스프레스 에러: ", err); console.log("익스프레스 에러: ", err);
......
import formidable from "formidable";
import { asyncWrap } from "../helpers/asyncWrap";
import { TypedRequest } from "../types";
export const uploadFile = asyncWrap(async (reqExp, res, next) => {
const req = reqExp as TypedRequest;
const form = formidable({ multiples: false, uploadDir: "uploads" });
await new Promise((resolve, reject) => {
form.parse(req, (err, fields, files) => {
if (err) {
reject(err);
return;
}
console.log("fields", fields);
console.log("files", files);
req.body = fields;
req.files = files;
resolve(files);
});
});
next();
return;
});
export const uploadFiles = asyncWrap(async (reqExp, res, next) => {
const req = reqExp as TypedRequest;
const form = formidable({ multiples: true, uploadDir: "uploads" });
await new Promise((resolve, reject) => {
form.parse(req, (err, fields, files) => {
if (err) {
reject(err);
return;
}
console.log("fields", fields);
console.log("files", files);
req.body = fields;
req.files = files;
resolve(files);
});
});
next();
return;
});
...@@ -3,3 +3,4 @@ export * as postCtrl from "./post.controller"; ...@@ -3,3 +3,4 @@ export * as postCtrl from "./post.controller";
export * as roleCtrl from "./role.controller"; export * as roleCtrl from "./role.controller";
export * as userCtrl from "./user.controller"; export * as userCtrl from "./user.controller";
export * as mainimgCtrl from "./mainimg.controller"; export * as mainimgCtrl from "./mainimg.controller";
export * as fileInfoCtrl from "./fileinfo.controller";
...@@ -12,7 +12,7 @@ export const createMainimg = asyncWrap(async (reqExp, res) => { ...@@ -12,7 +12,7 @@ export const createMainimg = asyncWrap(async (reqExp, res) => {
const { userId } = req.auth; const { userId } = req.auth;
const form = formidable({ const form = formidable({
uploadDir: "adminpics", uploadDir: "uploads",
keepExtensions: true, keepExtensions: true,
multiples: false, multiples: false,
}); });
......
import { ObjectId } from "mongoose"; import { ObjectId } from "mongoose";
import { Avatar, IAvatar, Mainimg, MainimgType } from "../models"; import { FileInfo, IFileInfo, Mainimg, MainimgType } from "../models";
export const createMainimg = async (mainimg: MainimgType, pic: IAvatar) => { export const createMainimg = async (mainimg: MainimgType, pic: IFileInfo) => {
const newPic = await Avatar.create({ const newPic = await FileInfo.create({
originalfilename: pic.originalfilename, originalfilename: pic.originalfilename,
newfilename: pic.newfilename, newfilename: pic.newfilename,
pictureauth: pic.picturepath, pictureauth: pic.picturepath,
......
import { model, Schema } from "mongoose"; import { model, Schema } from "mongoose";
export interface IAvatar { export interface IFileInfo {
originalfilename?: string; originalfilename?: string;
newfilename?: string; newfilename?: string;
picturepath?: string; picturepath?: string;
nickname?: string; nickname?: string;
} }
const Avatarschema = new Schema<IAvatar>({ const schema = new Schema<IFileInfo>({
originalfilename: { type: String, unique: true }, originalfilename: { type: String, unique: true },
newfilename: { type: String }, newfilename: { type: String },
nickname: { type: String }, nickname: { type: String },
picturepath: { type: String }, picturepath: { type: String },
}); });
export default model<IAvatar>("Avatar", Avatarschema); export default model<IFileInfo>("FileInfo", schema);
export { default as User, IUser } from "./user.model"; export { default as User, IUser } from "./user.model";
export { default as Post, PostType } from "./post.model"; export { default as Post, PostType } from "./post.model";
export { default as Role } from "./role.model"; export { default as Role } from "./role.model";
export { default as Avatar, IAvatar } from "./fileinfo.model"; export { default as FileInfo, IFileInfo } from "./fileinfo.model";
export { default as Mainimg, MainimgType } from "./mainimg.model"; export { default as Mainimg, MainimgType } from "./mainimg.model";
...@@ -4,7 +4,7 @@ export interface MainimgType { ...@@ -4,7 +4,7 @@ export interface MainimgType {
theme: string; theme: string;
city: string; city: string;
title: string; title: string;
pic?: Types.ObjectId; fileInfo?: Types.ObjectId;
} }
const MainimgSchema = new Schema<MainimgType>({ const MainimgSchema = new Schema<MainimgType>({
...@@ -18,7 +18,7 @@ const MainimgSchema = new Schema<MainimgType>({ ...@@ -18,7 +18,7 @@ const MainimgSchema = new Schema<MainimgType>({
type: String, type: String,
required: true, required: true,
}, },
pic: { type: Schema.Types.ObjectId, ref: "Avatar" }, fileInfo: { type: Schema.Types.ObjectId, ref: "Fileinfo" },
}); });
export default model<MainimgType>("Mainimg", MainimgSchema); export default model<MainimgType>("Mainimg", MainimgSchema);
...@@ -5,7 +5,7 @@ export interface IUser { ...@@ -5,7 +5,7 @@ export interface IUser {
name?: string; name?: string;
password: string; password: string;
role?: Types.ObjectId; role?: Types.ObjectId;
avatar?: Types.ObjectId; fileInfo?: Types.ObjectId;
} }
const validateEmail = (email: string) => { const validateEmail = (email: string) => {
...@@ -22,7 +22,7 @@ const schema = new Schema<IUser>( ...@@ -22,7 +22,7 @@ const schema = new Schema<IUser>(
validate: [validateEmail, "이메일을 입력해주세요"], validate: [validateEmail, "이메일을 입력해주세요"],
}, },
name: { type: String }, name: { type: String },
avatar: { type: Schema.Types.ObjectId, ref: "Avatar" }, fileInfo: { type: Schema.Types.ObjectId, ref: "FileInfo" },
password: { type: String, required: true, select: false }, password: { type: String, required: true, select: false },
role: { type: Schema.Types.ObjectId, ref: "Role" }, role: { type: Schema.Types.ObjectId, ref: "Role" },
}, },
......
import express from "express"; import express from "express";
import { userCtrl, authCtrl } from "../controllers"; import { userCtrl, authCtrl, fileInfoCtrl } from "../controllers";
const router = express.Router(); const router = express.Router();
router router
.route("/") .route("/")
.get(authCtrl.requireLogin, userCtrl.getUsers) .get(authCtrl.requireLogin, userCtrl.getUsers)
.post(authCtrl.requireLogin, authCtrl.hasRole("admin"), userCtrl.createUser); .post(
authCtrl.requireLogin,
authCtrl.hasRole("admin"),
fileInfoCtrl.uploadFile,
userCtrl.createUser
);
export default router; export default router;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment