Compare commits

...

5 Commits

6 changed files with 1218 additions and 26 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
upload/
# ---> Node # ---> Node
# Logs # Logs
logs logs

View File

@ -3,4 +3,7 @@ module.exports = {
SERVER_HOST: process.env.HOST || 'localhost', SERVER_HOST: process.env.HOST || 'localhost',
SERVER_PORT: process.env.PORT || '3000', SERVER_PORT: process.env.PORT || '3000',
MONGODB_URL: process.env.MONGDB || 'mongodb://localhost/data', MONGODB_URL: process.env.MONGDB || 'mongodb://localhost/data',
TEMP_PATH: '/tmp',
SAVE_PATH: 'upload',
UPLOAD_SIZE_LIMIT: 1024,
} }

1190
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -15,9 +15,12 @@
"author": "Zhao Xin <7176466@qq.com>", "author": "Zhao Xin <7176466@qq.com>",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"bcrypt": "^5.0.1",
"dotenv": "^16.0.1", "dotenv": "^16.0.1",
"express": "^4.18.1", "express": "^4.18.1",
"md5-file": "^5.0.0",
"mongoose": "^6.5.3", "mongoose": "^6.5.3",
"multer": "^1.4.5-lts.1",
"pug": "^3.0.2" "pug": "^3.0.2"
}, },
"devDependencies": { "devDependencies": {

View File

@ -1,4 +1,9 @@
const { SERVER_HOST, SERVER_PORT, MONGODB_URL } = require('./config') const { SERVER_HOST, SERVER_PORT, MONGODB_URL, TEMP_PATH, SAVE_PATH, UPLOAD_SIZE_LIMIT } = require('./config')
const fs = require('fs')
const path = require('path')
const multer = require('multer')
const bcrypt = require('bcrypt')
const md5file = require('md5-file')
const express = require('express') const express = require('express')
const mongoose = require('mongoose') const mongoose = require('mongoose')
mongoose.connect(MONGODB_URL, (error) => { mongoose.connect(MONGODB_URL, (error) => {
@ -16,14 +21,26 @@ app.use(express.json())
app.get('/', (req, res) => { app.get('/', (req, res) => {
res.render('index') res.render('index')
}) })
app.post('/upload', (req, res) => { const upload = multer({ dest: TEMP_PATH, limits: { fileSize: UPLOAD_SIZE_LIMIT } })
res.sendStatus(201) app.post('/upload', upload.single('file'), async (req, res) => {
const file_temp_path = path.join(TEMP_PATH, req.file.filename)
const md5 = await md5file(file_temp_path)
const file_save_path = path.join(__dirname, SAVE_PATH, md5)
const file = {
id: req.file.filename,
md5,
size: req.file.size,
encoding: req.file.encoding,
mimetype: req.file.mimetype,
filename: req.file.originalname,
password: req.body.password ? await bcrypt.hash(req.body.password, 16) : '',
}
if (!fs.existsSync(file_save_path)) {
fs.cpSync(file_temp_path, file_save_path) // 复制文件到UPLOAD_PATH
}
fs.unlinkSync(file_temp_path) // 删除临时文件
res.status(201).render('index', { file })
}) })
const server = app.listen(SERVER_PORT, SERVER_HOST, () => { const server = app.listen(SERVER_PORT, SERVER_HOST, () => {
console.info(`server is running at http://${SERVER_HOST}:${SERVER_PORT}`) console.info(`server is running at http://${SERVER_HOST}:${SERVER_PORT}`)
}) })
/**
* "bcrypt": "^5.0.1",
"md5-file": "^5.0.0",
"multer": "^1.4.5-lts.1",
*/

View File

@ -9,8 +9,17 @@ html(lang='zh')
title 文件分享服务器 title 文件分享服务器
body body
h1 文件分享服务器 h1 文件分享服务器
if uploaded if file
p= uploaded section
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') form(method='POST' action='/upload' enctype='multipart/form-data')
label(for='file_input') 文件 label(for='file_input') 文件
input(id='file_input' name='file' type='file' required) input(id='file_input' name='file' type='file' required)