Toggle navigation
Toggle navigation
This project
Loading...
Sign in
2020-2-capstone-design2
/
2017110267
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
Flare-k
2020-10-28 01:10:26 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
2218d5f7855a7f2d5c8fc500c889a05cca6271fb
2218d5f7
1 parent
87adf5e3
[Add] File Upload
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
21 additions
and
19 deletions
Project/controllers/homeController.js
Project/init.js
Project/middlewares.js
Project/models/File.js
Project/routers/fileRouter.js
Project/views/upload.pug
Project/controllers/homeController.js
View file @
2218d5f
/* eslint-disable no-console */
import
routes
from
"../routes"
;
import
File
from
"../models/File"
;
export
const
home
=
async
(
req
,
res
)
=>
{
...
...
@@ -30,26 +31,24 @@ export const search = async (req, res) => {
// upload 또한 upload를 준비하기 위한 get 페이지와 실제 데이터를 보내는 post 페이지가 필요하다.
export
const
getUpload
=
(
req
,
res
)
=>
res
.
render
(
"upload"
,
{
pageTitle
:
"Upload"
});
export
const
postUpload
=
async
(
req
,
res
)
=>
{
// const {} 를 통해 body를 받아와 요청하는 정보들을 확인한다.
// 이는 pug와 db.js를 확인해야하는 듯 하다.
const
{
body
:
{
title
,
description
},
body
:
{
title
},
file
:
{
path
},
// path로 할때는 로컬의 경로. S3는 location
}
=
req
;
// file에 path라는 요소가 있다.
const
new
Video
=
await
Video
.
create
({
const
new
File
=
await
File
.
create
({
fileUrl
:
path
,
title
,
description
,
creator
:
req
.
user
.
id
,
// 여기있는 fileUrl, title, description은 videoDB의 속성이다.
// 여기있는 fileUrl, title은 fileDB의 속성이다.
});
// console.log(newVideo);
req
.
user
.
videos
.
push
(
newVideo
.
id
);
// user DB의 video atribute에 추가
req
.
user
.
save
();
res
.
redirect
(
routes
.
videoDetail
(
newVideo
.
id
));
// id는 DB의 id
// id는 mongoDB가 랜덤하게 만들어준다.
console
.
log
(
newFile
);
res
.
redirect
(
routes
.
home
);
};
export
const
fileDetail
=
async
(
req
,
res
)
=>
{
...
...
@@ -73,12 +72,12 @@ export const deleteFile = async (req, res) => {
params
:
{
id
},
}
=
req
;
try
{
const
video
=
await
Video
.
findById
(
id
);
const
file
=
await
File
.
findById
(
id
);
// video를 받아서 render로 통해 템플릿으로 던져준다,
if
(
String
(
video
.
creator
)
!==
req
.
user
.
id
)
{
if
(
String
(
file
.
creator
)
!==
req
.
user
.
id
)
{
throw
Error
();
}
else
{
await
Video
.
findOneAndRemove
({
_id
:
id
});
await
File
.
findOneAndRemove
({
_id
:
id
});
}
}
catch
(
error
)
{
console
.
log
(
error
);
...
...
Project/init.js
View file @
2218d5f
dotenv
.
config
();
import
dotenv
from
"dotenv"
;
import
app
from
"./app"
;
// app.js에서 export default app했기 때문에 불러올 수 있다.
import
"./db"
;
import
"./models/File"
;
dotenv
.
config
();
const
PORT
=
process
.
env
.
PORT
||
80
;
const
handleListening
=
()
=>
{
...
...
Project/middlewares.js
View file @
2218d5f
...
...
@@ -12,11 +12,13 @@ const s3 = new aws.S3({
region
:
"ap-northeast-2"
,
});
const
multerFile
=
multer
({
dest
:
"uploads/files/"
});
export
const
localsMiddleware
=
(
req
,
res
,
next
)
=>
{
res
.
locals
.
siteName
=
"my Storage"
;
res
.
locals
.
routes
=
routes
;
// res.locals.loggedUser = req.user || null;
// console.log(req);
next
();
};
export
const
uploadFile
=
multerFile
.
single
(
"file"
);
// single에 들어간 File은 upload.pug의 file 부분 input name
\ No newline at end of file
...
...
Project/models/File.js
View file @
2218d5f
...
...
@@ -17,7 +17,7 @@ const FileSchema = new mongoose.Schema({
},
});
// 이제 이 스키마를 이용하여 model을 만들어준다.
// 모델의 이름은 "
Video
"
// 모델의 이름은 "
File
"
const
model
=
mongoose
.
model
(
"File"
,
FileSchema
);
export
default
model
;
// 모델이 만들어짐을 알리기 위해 init.js에 import해준다.
...
...
Project/routers/fileRouter.js
View file @
2218d5f
...
...
@@ -6,11 +6,12 @@ import {
fileDetail
,
deleteFile
,
}
from
"../controllers/homeController"
;
import
{
uploadFile
}
from
"../middlewares"
;
const
fileRouter
=
express
.
Router
();
// Upload
fileRouter
.
get
(
routes
.
upload
,
getUpload
);
fileRouter
.
post
(
routes
.
upload
,
postUpload
);
fileRouter
.
post
(
routes
.
upload
,
uploadFile
,
postUpload
);
// File Detail
fileRouter
.
get
(
routes
.
fileDetail
(),
fileDetail
);
...
...
Project/views/upload.pug
View file @
2218d5f
...
...
@@ -5,6 +5,6 @@ block content
form(action=`/files${routes.upload}`, method="post", enctype="multipart/form-data")
div.fileUpload
label(for="file") File
input(type="file", id="file", name="file", required=true
, accept = "file/*"
)
input(type="file", id="file", name="file", required=true)
input(type="text", placeholder="Title", name="title", required=true)
input(type="submit", value="Upload File")
\ No newline at end of file
...
...
Please
register
or
login
to post a comment