Compare commits

..

No commits in common. "503fae7cc71a40b47b944452ac7c87759d7c0d82" and "9f12482b304894704ac6d7680effe575ac925467" have entirely different histories.

5 changed files with 22 additions and 56 deletions

View File

@ -1 +1 @@
// file_input.value = null
file_input.value = null

View File

@ -10,7 +10,6 @@ body {
margin: 0 auto;
}
p,
h1,
form {
margin: 1rem auto;

View File

@ -20,7 +20,7 @@ app.use(express.static('public'))
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
app.get('/', (req, res) => {
res.render('upload')
res.render('index')
})
const upload = multer({ dest: TEMP_PATH, limits: { fileSize: UPLOAD_SIZE_LIMIT } })
app.post('/upload', upload.single('file'), async (req, res) => {
@ -31,35 +31,27 @@ app.post('/upload', upload.single('file'), async (req, res) => {
fs.cpSync(file_temp_path, file_save_path) // 复制文件到UPLOAD_PATH
}
fs.unlinkSync(file_temp_path) // 删除临时文件
let file = {
const file = {
md5,
size: req.file.size,
encoding: req.file.encoding,
mimetype: req.file.mimetype,
// 解决了multer将中文文件名错误编码的问题
filename: Buffer.from(req.file.originalname, 'latin1').toString('utf8'),
filename: req.file.originalname,
password: req.body.password ? await bcrypt.hash(req.body.password, 16) : '',
}
// 写入数据库
console.log(req.file)
console.log(file)
const fileshare = new Fileshare(file)
file = await fileshare.save()
console.log(file)
res.status(201).render('upload', { file })
file.id = (await fileshare.save()).id
console.table(file)
res.status(201).render('index', { file })
})
app.get('/file/:id', async (req, res) => {
const file = await Fileshare.findById(req.params.id)
res.render('download', { file })
})
app.post('/file/:id', async (req, res) => {
const file = await Fileshare.findById(req.params.id)
const file = await Fileshare.findByIdAndUpdate(req.params.id, { $inc: { downloads: 1 } })
if (file) {
if (file.password == '' || (await bcrypt.compare(req.body.password, file.password)) == true) {
await file.update({ $inc: { downloads: 1 } })
res.status(200).download(path.join(SAVE_PATH, file.md5), file.filename)
const file_path = path.join(SAVE_PATH, file.md5)
if (file.password == '') {
res.status(200).download(file_path, file.filename)
} else {
res.sendStatus(401)
}
} else {
res.sendStatus(404)

View File

@ -1,27 +0,0 @@
doctype html
html(lang='zh')
head
meta(charset='UTF-8')
meta(http-equiv='X-UA-Compatible' content='IE=edge')
meta(name='viewport' content='width=device-width, initial-scale=1.0')
link(rel='icon' href='/favicon.ico' type='image/x-icon')
link(rel='stylesheet' href='/style.css')
title 下载文件
body
h1 下载文件
form(method='POST')
label 文件
label= file.filename
label 大小
label= file.size
|   字节
label 类型
label= file.mimetype
label 下载
label= file.downloads
|   次
if file.password
label(for='password_input') 密码
input(id='password_input' name='password' type='password' autocomplete='off' required)
button(id='download_button' type='submit') 下载
script(src='/client.js')

View File

@ -6,18 +6,20 @@ html(lang='zh')
meta(name='viewport' content='width=device-width, initial-scale=1.0')
link(rel='icon' href='/favicon.ico' type='image/x-icon')
link(rel='stylesheet' href='/style.css')
title 分享文件
title 文件分享服务器
body
h1 分享文件
h1 文件分享服务器
if file
section
p
| 文件  
var #{file.filename}  
| 分享成功!
p 链接  
-const url = '/file/' + file.id
a(href=url)= url
h2 上传成功
ul
li id #{file.id}
li md5 #{file.md5}
li size #{file.size} bytes
li encoding #{file.encoding}
li mimetype #{file.mimetype}
li filename #{file.filename}
li password '#{file.password}'
form(method='POST' action='/upload' enctype='multipart/form-data')
label(for='file_input') 文件
input(id='file_input' name='file' type='file' required)