-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
837 lines (712 loc) · 23.8 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
require('dotenv').config()
const express = require('express')
const app = express()
const PORT = process.env.PORT || 3000
const path = require('path');
const { MongoClient, ObjectId } = require('mongodb')
const session = require('express-session')
const passport = require('passport')
const LocalStrategy = require('passport-local')
const { S3Client, DeleteObjectsCommand } = require('@aws-sdk/client-s3')
const multer = require('multer')
const multerS3 = require('multer-s3')
const bcrypt = require('bcrypt')
const MongoStore = require('connect-mongo');
const { v4: uuidv4 } = require('uuid');
const s3 = new S3Client({
region: process.env.AWS_S3_REGION || "",
credentials: {
accessKeyId: process.env.AWS_S3_KEY || "",
secretAccessKey: process.env.AWS_S3_SECRET || ""
}
})
const upload = multer({
storage: multerS3({
s3: s3,
bucket: process.env.AWS_S3_BUCKET,
// acl : "public-read",
contentType: multerS3.AUTO_CONTENT_TYPE,
key: function (req, file, cb) {
cb(null, file.originalname + '-' + Date.now().toString())
}
}),
limits: { fileSize: 5 * 1024 * 1024, files: 100 }
})
const tags = ["algorithm", "htmlcss", "javascript", "nodejs", "react", "other"]
// app.use(methodOverride('_method'))
app.use(express.static(path.join(__dirname + '/public')))
app.set('view engine', 'ejs')
app.set('views', './views');
app.use(express.json({
limit: "100mb"
}))
app.use(express.urlencoded({ extended: true }))
app.use(passport.initialize())
app.use(session({
secret: process.env.SESSION_SECRET_KEY,
resave: false,
saveUninitialized: true,
cookie: { maxAge: 30 * 60 * 1000 },
store: MongoStore.create({
mongoUrl: process.env.MongoDB_URL,
dbName: 'blog'
}),
rolling: true
}))
app.use(passport.session())
// 아이디 비번 검사
passport.use(new LocalStrategy(async (username, password, cb) => {
try {
let result = await mongoDB.collection('user').findOne({ username: username })
if (!result) {
return cb(null, false, { message: '아이디 DB에 없음' })
}
if (await bcrypt.compare(password, result.password)) {
return cb(null, result)
} else {
return cb(null, false, { message: '비밀번호 틀림' })
}
} catch (e) {
console.log(e)
}
}))
// 세션 생성하는 코드
passport.serializeUser((user, done) => {
// nodejs 내부 코드를 비동기적으로 처리해준다.
process.nextTick(() => {
done(null, { id: user._id, username: user.username })
})
})
/**
* 유저가 보낸 쿠키를 분석 해준다.
*
* req.user에 지금 접속한 유저의 정보가 남는다.
*
* serializeUser와
* deserializeUser 밑에서만 작동하므로
* 기능 개발 시 유의해야 한다.
*
* @todo redis에 세션 저장하기
* @todo 특정 라우팅에만 작동시키기
*/
passport.deserializeUser(async (user, done) => {
// 문제가 없는데 왜 ObjectId에 가로선이 뜨는지 모르겠음
// 이유는 그냥 권장하지 않는 방식이라 그렇지 오류가 발생하지 않는다.
let result = await mongoDB.collection('user').findOne({ _id: new ObjectId(user.id) })
// password는 보안상의 이유로 제거한다.
delete result.password
process.nextTick(() => {
return done(null, result)
})
})
let mongoDB;
new MongoClient(process.env.MongoDB_URL).connect()
.then((client) => {
console.log('MongoDB 연결성공')
mongoDB = client.db('blog')
app.listen(PORT, () => {
console.log(`http://localhost:${PORT} 에서 서버 실행중`)
})
}).catch((err) => {
console.log(err)
})
function isValidObjectId(id) {
if (typeof id !== 'string') return false;
if (id.length !== 24) return false;
if (!id.match(/^[0-9a-fA-F]+$/)) return false;
return true;
}
const checkLogin = (req, res, next) => {
if (req.user === undefined) {
return res.redirect(`/login?goto=${req.url}`)
}
return next()
}
const checkTempStorage = async (req, res, next) => {
let temp = await mongoDB.collection('temp').findOne({ userId: req.user._id })
if (temp === null) {
let tempLayout = {
userId: req.user._id,
write: { images: [] },
edit: { images: [] }
}
await mongoDB.collection('temp').insertOne(tempLayout)
return next()
}
if (temp.write === undefined) {
await mongoDB.collection('temp')
.updateOne(
{ userId: req.user._id },
{ $set: { write: { images: [] } } }
)
} else if (temp.write.images === undefined) {
await mongoDB.collection('temp')
.updateOne(
{ userId: req.user._id },
{ $set: { write: { images: [] } } }
)
} else if (temp.write.images.length > 0) {
let input = {
Bucket: process.env.AWS_S3_BUCKET,
Delete: {
Objects: temp.write.images
}
}
const command = new DeleteObjectsCommand(input)
const response = await s3.send(command)
}
if (temp.edit === undefined) {
await mongoDB.collection('temp')
.updateOne(
{ userId: req.user._id },
{ $set: { edit: { images: [] } } }
)
} else if (temp.edit.images === undefined) {
await mongoDB.collection('temp')
.updateOne(
{ userId: req.user._id },
{ $set: { edit: { images: [] } } }
)
} else if (temp.edit.images.length > 0) {
let input = {
Bucket: process.env.AWS_S3_BUCKET,
Delete: {
Objects: temp.edit.images
}
}
const command = new DeleteObjectsCommand(input)
const response = await s3.send(command)
}
return next()
}
function getDatetime() {
const date = new Date()
const year = date.getFullYear() // 연도
const month = String(date.getMonth() + 1).padStart(2, '0') // 월 (0부터 시작하므로 +1)
const day = String(date.getDate()).padStart(2, '0') // 일
const hours = String(date.getHours()).padStart(2, '0') // 시
const minutes = String(date.getMinutes()).padStart(2, '0') // 분
const seconds = String(date.getSeconds()).padStart(2, '0') // 초
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}` // DATETIME 형식으로 조합
}
app.get('/', (req, res) => {
res.render('index')
})
app.get('/login', (req, res) => {
if (req.user !== undefined) return res.redirect('/user')
res.render('login')
})
/**
* @todo
* 로그인 시 id, pw 정규식 체크 추가 필요
*/
app.post('/login', async (req, res, next) => {
passport.authenticate('local', (error, user, info) => {
if (error) return res.status(500).json(error)
if (!user) return res.status(401).json(info.message)
req.logIn(user, (err) => {
if (err) return next(err)
// uuid 생성 로직
// if (req.user.uuid === undefined) {
// const uuid = uuidv4();
// mongoDB.collection('user').updateOne(
// { _id : req.user._id },
// {
// $set: {
// uuid: uuid
// }
// })
// }
res.redirect(`/immigration?goto=${req.query.goto}`)
})
})(req, res, next)
})
app.post('/join', async (req, res) => {
let hashPassword = await bcrypt.hash(req.body.password, 10)
/**
* 신규 가입자 정보
*
* @property { string } username 아이디
* @property { string } password 비밀번호
* @property { string } authority 권한
*
* @todo 정보 정규식 검사
* @todo email등 기타 정보 추가하기
*/
let newcomer = {
username: req.body.username,
password: hashPassword,
authority: "user",
create: getDatetime()
}
const result = await mongoDB.collection('user').insertOne(newcomer)
res.redirect('/login')
})
app.get('/forgot', (req, res) => {
res.send('힘내렴')
})
/**
* 로그인 하기 이전 사용자가 있던 위치로 이동시키는 요청
*
* 로그인한 유저에게 필요한 정보를 업데이트하거나
* 저장공간등을 할당해주는 장소
*/
app.get('/immigration', checkTempStorage, async (req, res, next) => {
return res.status(200).json({ url: req.query.goto })
})
/**
* 지금은 a 태그로 이동하지만
* 이후 버튼을 통한 동적 이동으로 바꿀거임
* 클라이언트에서 이동 요청을 보낼 때 서버에서
* 클라이언트의 정보를 확인하고 이동시킬지 경고를 보낼지 판단할거임
*/
app.get('/write', checkLogin, async (req, res, next) => {
try {
let temp = await mongoDB.collection('temp').findOne({ userId: req.user._id })
if (temp.write.images.length > 0) {
let input = {
Bucket: process.env.AWS_S3_BUCKET,
Delete: {
Objects: temp.write.images
}
}
const command = new DeleteObjectsCommand(input)
const response = await s3.send(command)
}
await mongoDB.collection('temp')
.updateOne(
{ userId: req.user._id },
{ $set: { write: { images: [] } } }
)
return res.render('write')
} catch (e) {
return res.send(e)
}
})
/**
* @todo
*
* 전달받은 데이터를 검증한다.
*/
app.post('/write', checkLogin, async (req, res) => {
try {
let { title, categori, tag, delta, html, images } = req.body
let saveImages = [] // 실제 저장할 이미지 모음
let deleteImages = [] // 실제 사용하지 않은 이미지
let temp = await mongoDB.collection('temp')
.findOne(
{ userId: req.user._id }
)
temp.write.images.forEach(e => {
let flag = false
images.some(image => {
if (e.Key === image) {
saveImages.push(e)
return flag = true
}
return false
})
if (!flag) deleteImages.push(e)
})
if (deleteImages.length > 0) {
let input = {
Bucket: process.env.AWS_S3_BUCKET,
Delete: {
Objects: deleteImages
}
}
const command = new DeleteObjectsCommand(input)
const response = await s3.send(command)
}
await mongoDB.collection('temp')
.updateOne(
{ userId: req.user._id },
{ $set: { write: { images: [] } } }
)
let post = {
userId: req.user._id,
username: req.user.username,
title: title,
categori: categori,
tag: tag,
delta: delta,
html: html,
images: saveImages,
date: getDatetime(),
edit: null
}
let result = await mongoDB.collection('post').insertOne(post)
return res.json(`/detail/${result.insertedId}`)
} catch (e) {
return res.send(e)
}
})
app.get('/list', async (req, res) => {
res.render('list')
})
app.get('/get/list', async (req, res) => {
const tag = req.query.tag ?? undefined;
const categori = req.query.categori ?? undefined;
const search = { categori: categori, tag: tag };
let posts;
try {
posts = await mongoDB.collection('post').find(search).project({ username: 1, title: 1, tag: 1, date: 1 }).toArray();
} catch (e) {
return res.send(e);
}
return res.json(posts);
})
app.get('/get/post', async (req, res) => {
let postId = new ObjectId(req.query.id)
let post = await mongoDB.collection('post').findOne({ _id: postId })
res.json(post)
})
/***
* 업로드 하려는 게시물이 본인 게시물이 맞는지 확인 필요
*/
app.post('/upload/image', checkLogin, upload.single('file'), async (req, res) => {
try {
const task = req.query.task
let temp = await mongoDB.collection('temp').findOne({ userId: req.user._id })
if (task === 'write') {
let images = temp.write.images
let length = images.length
images[length] = { Key: req.file.key }
await mongoDB.collection('temp').updateOne(
{ userId: req.user._id },
{ $set: { write: { images: images } } }
)
} else if (task === 'edit') {
let images = temp.edit.images
let length = images.length
images[length] = { Key: req.file.key }
await mongoDB.collection('temp').updateOne(
{ userId: req.user._id },
{ $set: { edit: { images: images } } }
)
} else {
let input = {
Bucket: process.env.AWS_S3_BUCKET,
Delete: {
Objects: [{ Key: req.file.key }]
}
}
const command = new DeleteObjectsCommand(input)
const response = await s3.send(command)
return res.send('업로드 실패')
}
return res.send({
key: req.file.key,
location: req.file.location
})
} catch (e) {
return res.send(e)
}
})
// reply를 ejs로 보낼지 동적으로 보낼지 고민해봄
app.get('/detail/:id', async (req, res) => {
try {
// 잘못된 게시물 아이디
if (!isValidObjectId(req.params.id)) {
return res.status(403).json({ msg: "잘못된 접근" })
}
let postId = new ObjectId(req.params.id)
let post = await mongoDB.collection('post').findOne({ _id: postId })
// 없는 게시물
if (post === null) {
return res.status(403).json({ msg: "잘못된 접근" })
}
let login = req.user === undefined ? false : true
// 게시물 권한
let authority = "disallowed";
if (req.user !== undefined) {
if (post.userId.equals(req.user._id)) {
authority = "allowed"
}
}
//작성자 ID를 제거하고 전송
delete post.userId
return res.render('detail', {
authority: authority,
login: login,
post: post,
})
} catch (e) {
console.log(e)
return res.send('400')
}
})
// url params
// 경로 문제였다 쉬이벌...
app.get('/edit/:id', async (req, res) => {
try {
if (req.user === undefined) {
return res.status(401).json({
msg: "승인되지 않음",
})
}
if (!isValidObjectId(req.params.id)) {
return res.status(403).json({
msg: "승인되지 않음",
})
}
let postId = new ObjectId(req.params.id)
let post = await mongoDB.collection('post').findOne({ _id: postId })
if (post === null) {
return res.status(403).json({
msg: "승인되지 않음",
})
}
if (!post.userId.equals(req.user._id)) {
return res.status(403).json({
msg: "승인되지 않음",
})
}
return res.render('edit')
} catch (e) {
console.log(e)
res.status(404).json({ msg: 'error' })
}
})
app.put('/edit/:id', checkLogin, async (req, res, next) => {
try {
const postId = new ObjectId(req.params.id)
let { title, categori, tag, delta, html, images } = req.body
let post = await mongoDB.collection('post').findOne({ _id: postId })
let temp = await mongoDB.collection('temp').findOne({ userId: req.user._id })
let allImages = [...post.images, ...temp.edit.images]
let saveImages = []
let deleteImages = []
allImages.forEach(e => {
let flag = false
images.some(image => {
if (e.Key === image) {
saveImages.push(e)
return flag = true
} else return false
})
if (!flag) deleteImages.push(e)
})
if (deleteImages.length > 0) {
let input = {
Bucket: process.env.AWS_S3_BUCKET,
Delete: {
Objects: deleteImages
}
}
const command = new DeleteObjectsCommand(input)
const response = await s3.send(command)
}
await mongoDB.collection('temp')
.updateOne(
{ userId: req.user._id },
{ $set: { edit: { images: [] } } }
)
const editPost = {
title: title,
categori: categori,
tag: tag,
delta: delta,
html: html,
images: saveImages,
edit: getDatetime()
}
await mongoDB.collection('post').updateOne(
{ _id: postId },
{ $set: editPost })
res.json(`/detail/${postId}`)
} catch (e) {
res.send(e)
}
})
app.delete('/delete/post/:id', async (req, res, next) => {
try {
// 로그인 안 함
if (req.user === undefined) {
return res.status(401).json({
msg: "승인되지 않음",
})
}
// 게시글 아이디가 잘못됨
if (!isValidObjectId(req.params.id)) {
return res.status(403).json({
msg: "잘못된 접근",
})
}
const postId = new ObjectId(req.params.id)
const post = await mongoDB.collection('post').findOne({ _id: postId })
// 존재하지 않는 게시글
if (post === null) {
if (!isValidObjectId(req.params.id)) {
return res.status(403).json({
msg: "잘못된 접근",
})
}
}
// 게시글 작성자와 삭제 요청자의 아이디가 다름
if (!req.user._id.equals(post.userId)) {
return res.status(403).json({
msg: "승인되지 않음",
})
}
// 이미지를 삽입하지 않은 게시물
// 이미지를 삭제에 실패 했을 때 저장하는 저장소 DB에 만들기
if (post.images.length > 0) {
let input = {
Bucket: process.env.AWS_S3_BUCKET,
Delete: {
Objects: post.images
}
}
const command = new DeleteObjectsCommand(input)
const response = await s3.send(command)
}
const postResult = await mongoDB.collection('post').deleteOne({ _id: postId })
if (!postResult.acknowledged) {
return res.status(403).json({
msg: "게시글 삭제 실패",
})
}
const commentResult = await mongoDB.collection('comment').deleteMany({ postId: post._id })
return res.json({
msg: "게시글 삭제 완료",
url: "/"
})
} catch (e) {
return res.send(e)
}
})
app.get('/user', checkLogin, async (req, res) => {
let result = await mongoDB.collection('user').findOne({ _id: req.user._id })
console.log(req.session)
res.render('user', { user: result })
})
app.get('/logout', checkLogin, async (req, res) => {
req.logout((err) => {
if (err) return next(err)
req.session.save(() => {
res.redirect('/')
});
});
});
app.post('/comment', checkLogin, async (req, res) => {
// ERROR #1 전달받은 데이터 없음
if (req.body === undefined) {
return res.status(403).send('comment error code #1')
}
// ERROR #2 게시물 ID 형식이 올바르지 않음
if (!isValidObjectId(req.body.postId)) {
return res.status(403).send('comment error code #2')
}
// ERROR #3 댓글 타입이 올바르지 않음
if (req.body.type !== 'text' && req.body.type !== 'emoticon') {
return res.status(403).send('comment error code #3')
}
// ERROR #4 댓글 내용이 없음
if (req.body.content === '') {
return res.status(403).send('comment error code #4')
}
const postId = new ObjectId(req.body.postId)
let post = await mongoDB.collection('post').findOne({ _id: postId })
// ERROR #5 게시물이 존재하지 않음
if (post === null) {
return res.status(403).send('comment error code #5')
}
try {
const result = await mongoDB.collection('comment')
.insertOne({
postId: postId,
userId: req.user._id,
username: req.user.username,
type: req.body.type,
content: req.body.content,
date: getDatetime(),
})
} catch (e) {
return res.status(404).send('comment upload fail')
}
res.redirect(`/detail/${req.body.postId}`)
})
app.get('/get/comment/:id', async (req, res) => {
try {
const postId = new ObjectId(req.params.id)
const result = await mongoDB.collection('comment').find({ postId: postId }).toArray()
if (req.user !== undefined) {
result.forEach(e => {
if (e.userId.equals(req.user._id)) {
e.authority = 'allowed'
} else {
e.authority = 'notAllowed'
}
})
}
return res.send(result)
} catch (e) {
}
})
/**
* @todo
*
* 댓글 수정하기를 만드시오
*
*/
// app.put('/edit/comment/:id', checkLogin, async (req, res) => {
// const commentId = new ObjectId(req.params.id)
// const result = await mongoDB.collection('comment').find({_id : commentId})
// })
app.delete('/delete/comment/:id', checkLogin, async (req, res) => {
const commentId = new ObjectId(req.params.id)
const comment = await mongoDB.collection('comment').findOne({ _id: commentId })
if (req.user._id.equals(comment.userId)) {
const result = await mongoDB.collection('comment').deleteOne({ _id: commentId })
return res.status(200).send(result)
} else {
return res.status(403).send('not found')
}
})
app.post('/emoticon', checkLogin, upload.array('files[]', 100), async (req, res) => {
try {
if (req.user.authority === 'admin') {
const data = {
title: req.body.title,
location: "https://millennium00forum1.s3.ap-northeast-2.amazonaws.com/",
files: [],
}
if (req.files.length > 0) {
req.files.forEach(e => {
data.files.push({ Key: e.key })
})
}
let result = await mongoDB.collection('emoticon').insertOne(data)
return res.send(result)
}
return res.send('권한없음')
} catch (e) {
return res.send(e)
}
})
app.delete('/delete/emoticon/:id', checkLogin, async (req, res) => {
const emoticonId = req.params.id
const emoticon = await mongoDB.collection('emoticon').find(
{ _id: emoticonId })
if (emoticon.files.length > 0) {
let input = {
Bucket: process.env.AWS_S3_BUCKET,
Delete: {
Objects: emoticon.files
}
}
const command = new DeleteObjectsCommand(input)
const response = await s3.send(command)
console.log(response)
}
})
app.get('/get/emoticon', checkLogin, async (req, res) => {
if (req.query.value === undefined) {
const result = await mongoDB.collection('emoticon').find().toArray()
res.send(result)
} else {
const result = await mongoDB.collection('emoticon').findOne({ title: req.query.value })
res.send(result)
}
})