重命名

This commit is contained in:
赵鑫 2022-08-26 20:46:06 +08:00
parent 503fae7cc7
commit acebcd95f4
5 changed files with 18 additions and 14 deletions

View File

@ -1,5 +1,5 @@
const mongoose = require('mongoose') const mongoose = require('mongoose')
const FileshareSchema = new mongoose.Schema({ const File = new mongoose.Schema({
md5: { type: String, require: true }, md5: { type: String, require: true },
size: { type: Number, require: true }, size: { type: Number, require: true },
encoding: { type: String, require: true }, encoding: { type: String, require: true },
@ -9,4 +9,4 @@ const FileshareSchema = new mongoose.Schema({
downloads: { type: Number, default: 0 }, downloads: { type: Number, default: 0 },
createdAt: { type: Date, default: Date.now }, createdAt: { type: Date, default: Date.now },
}) })
module.exports = mongoose.model('Fileshare', FileshareSchema) module.exports = mongoose.model('File', File)

View File

@ -1,7 +1,7 @@
{ {
"name": "fileshare", "name": "filesharing",
"version": "0.1.0", "version": "0.1.0",
"description": "文件分享服务器", "description": "文件分享",
"main": "server.js", "main": "server.js",
"scripts": { "scripts": {
"start": "nodemon", "start": "nodemon",
@ -9,7 +9,7 @@
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "ssh://git@nas:2222/zhaoxin/fileshare.git" "url": "ssh://git@nas:2222/zhaoxin/filesharing.git"
}, },
"keywords": [], "keywords": [],
"author": "Zhao Xin <7176466@qq.com>", "author": "Zhao Xin <7176466@qq.com>",

View File

@ -6,7 +6,7 @@ const bcrypt = require('bcrypt')
const md5file = require('md5-file') const md5file = require('md5-file')
const express = require('express') const express = require('express')
const mongoose = require('mongoose') const mongoose = require('mongoose')
const Fileshare = require('./models/fileshare') const File = require('./models/File')
mongoose.connect(MONGODB_URL, (error) => { mongoose.connect(MONGODB_URL, (error) => {
if (error) { if (error) {
console.error(error) console.error(error)
@ -43,17 +43,21 @@ app.post('/upload', upload.single('file'), async (req, res) => {
// 写入数据库 // 写入数据库
console.log(req.file) console.log(req.file)
console.log(file) console.log(file)
const fileshare = new Fileshare(file) const fileshare = new File(file)
file = await fileshare.save() file = await fileshare.save()
console.log(file) console.log(file)
res.status(201).render('upload', { file }) res.status(201).render('upload', { file })
}) })
app.get('/file/:id', async (req, res) => { app.get('/file/:id', async (req, res) => {
const file = await Fileshare.findById(req.params.id) const file = await File.findById(req.params.id)
if (file) {
res.render('download', { file }) res.render('download', { file })
} else {
res.sendStatus(404)
}
}) })
app.post('/file/:id', async (req, res) => { app.post('/file/:id', async (req, res) => {
const file = await Fileshare.findById(req.params.id) const file = await File.findById(req.params.id)
if (file) { if (file) {
if (file.password == '' || (await bcrypt.compare(req.body.password, file.password)) == true) { if (file.password == '' || (await bcrypt.compare(req.body.password, file.password)) == true) {
await file.update({ $inc: { downloads: 1 } }) await file.update({ $inc: { downloads: 1 } })

View File

@ -6,9 +6,9 @@ html(lang='zh')
meta(name='viewport' content='width=device-width, initial-scale=1.0') meta(name='viewport' content='width=device-width, initial-scale=1.0')
link(rel='icon' href='/favicon.ico' type='image/x-icon') link(rel='icon' href='/favicon.ico' type='image/x-icon')
link(rel='stylesheet' href='/style.css') link(rel='stylesheet' href='/style.css')
title 下载文件 title 文件下载
body body
h1 下载文件 h1 文件下载
form(method='POST') form(method='POST')
label 文件 label 文件
label= file.filename label= file.filename

View File

@ -6,9 +6,9 @@ html(lang='zh')
meta(name='viewport' content='width=device-width, initial-scale=1.0') meta(name='viewport' content='width=device-width, initial-scale=1.0')
link(rel='icon' href='/favicon.ico' type='image/x-icon') link(rel='icon' href='/favicon.ico' type='image/x-icon')
link(rel='stylesheet' href='/style.css') link(rel='stylesheet' href='/style.css')
title 分享文件 title 文件分享
body body
h1 分享文件 h1 文件分享
if file if file
section section
p p