-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
124 lines (92 loc) · 3 KB
/
index.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
require('dotenv').config()
const express = require('express')
const flash = require('express-flash')
const session = require('express-session')
const cookieParser = require('cookie-parser')
const bodyParser = require('body-parser')
const fs = require('fs')
const FileReader = require('./fileReader')
const multer = require('multer')
const UserRouter = require('./router/UserRouter')
const e = require('express')
const { Console } = require('console')
const app = express()
app.set('view engine', 'ejs')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))
app.use(cookieParser('hkt'));
app.use(session({ cookie: { maxAge: 600000 }}));
app.use(flash());
const uploader = multer({dest: __dirname + '/uploads/'})
app.use((req, res, next) => {
req.vars = {root: __dirname}
next()
})
app.use('/user', UserRouter)
const getCurrentDir = (req, res, next) => {
if (!req.session.user) {
// chưa đăng nhập
return next();
}
const {userRoot} = req.session.user
let {dir} = req.query
if (dir === undefined) {
dir = ''
}
let currentDir =`${userRoot}/${dir}`
if (!fs.existsSync(currentDir)) {
currentDir = userRoot
}
req.vars.currentDir = currentDir
req.vars.userRoot = userRoot
next();
}
app.get('/', getCurrentDir, (req, res) =>{
if (!req.session.user) {
return res.redirect('/user/login')
}
let {userRoot, currentDir} = req.vars
// console.log('cần nạp thư mục: ' + currentDir);
FileReader.load(userRoot, currentDir)
.then(files => {
const user = req.session.user
res.render('index', {user, files})
})
})
app.post('/upload', uploader.single('attachment'), (req, res) => {
console.log(req.body)
const {email} = req.body
const file = req.file
if (!email || !file) {
return res.json({code: 1, message: ' Thông tin không hợp lệ'})
}
const {root} = req.vars
// const currentDir = `${root}/users/${email}/${path}`
const currentDir = `${root}/users/${email}`
if (!fs.existsSync(currentDir)) {
return res.json({code: 2, message: 'Đường dẫn cần lưu không đúng'})
}
let name = file.originalname
let newPath = currentDir + '/' + name
fs.renameSync(file.path, newPath)
return res.json({code: 0, message: 'Upload thành công, đã lưu tại ' + newPath})
})
app.post('/delete', (req, res) => {
console.log(req.body)
console.log(req.vars)
const {email, name} = req.body
const {root} = req.vars //
const currentDir = `${root}/users/${email}/${name}`
console.log(currentDir)
fs.unlink(currentDir, (err) => {
if (err) {
console.error(err)
return
}
//file removed
return res.json({code: 0, message: 'Xóa thành công tập tin tại path:' + currentDir})
})
// console.log(req.body)
})
const port = process.env.PORT || 8080
app.listen(port, () => console.log(`http://localhost:${port}`))