From acebcd95f417536a46af33214214e212c79d9e02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E9=91=AB?= <7176466@qq.com> Date: Fri, 26 Aug 2022 20:46:06 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/{fileshare.js => File.js} | 4 ++-- package.json | 6 +++--- server.js | 14 +++++++++----- views/download.pug | 4 ++-- views/upload.pug | 4 ++-- 5 files changed, 18 insertions(+), 14 deletions(-) rename models/{fileshare.js => File.js} (79%) diff --git a/models/fileshare.js b/models/File.js similarity index 79% rename from models/fileshare.js rename to models/File.js index 53f50b0..fd98ddb 100644 --- a/models/fileshare.js +++ b/models/File.js @@ -1,5 +1,5 @@ const mongoose = require('mongoose') -const FileshareSchema = new mongoose.Schema({ +const File = new mongoose.Schema({ md5: { type: String, require: true }, size: { type: Number, require: true }, encoding: { type: String, require: true }, @@ -9,4 +9,4 @@ const FileshareSchema = new mongoose.Schema({ downloads: { type: Number, default: 0 }, createdAt: { type: Date, default: Date.now }, }) -module.exports = mongoose.model('Fileshare', FileshareSchema) +module.exports = mongoose.model('File', File) diff --git a/package.json b/package.json index d899221..66694db 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "fileshare", + "name": "filesharing", "version": "0.1.0", - "description": "文件分享服务器", + "description": "文件分享", "main": "server.js", "scripts": { "start": "nodemon", @@ -9,7 +9,7 @@ }, "repository": { "type": "git", - "url": "ssh://git@nas:2222/zhaoxin/fileshare.git" + "url": "ssh://git@nas:2222/zhaoxin/filesharing.git" }, "keywords": [], "author": "Zhao Xin <7176466@qq.com>", diff --git a/server.js b/server.js index 624b022..9c2c3ef 100644 --- a/server.js +++ b/server.js @@ -6,7 +6,7 @@ const bcrypt = require('bcrypt') const md5file = require('md5-file') const express = require('express') const mongoose = require('mongoose') -const Fileshare = require('./models/fileshare') +const File = require('./models/File') mongoose.connect(MONGODB_URL, (error) => { if (error) { console.error(error) @@ -43,17 +43,21 @@ app.post('/upload', upload.single('file'), async (req, res) => { // 写入数据库 console.log(req.file) console.log(file) - const fileshare = new Fileshare(file) + const fileshare = new File(file) file = await fileshare.save() console.log(file) res.status(201).render('upload', { file }) }) app.get('/file/:id', async (req, res) => { - const file = await Fileshare.findById(req.params.id) - res.render('download', { file }) + const file = await File.findById(req.params.id) + if (file) { + res.render('download', { file }) + } else { + res.sendStatus(404) + } }) 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.password == '' || (await bcrypt.compare(req.body.password, file.password)) == true) { await file.update({ $inc: { downloads: 1 } }) diff --git a/views/download.pug b/views/download.pug index d126ed3..01a9537 100644 --- a/views/download.pug +++ b/views/download.pug @@ -6,9 +6,9 @@ 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 文件下载 form(method='POST') label 文件 label= file.filename diff --git a/views/upload.pug b/views/upload.pug index bbda386..bf4fa85 100644 --- a/views/upload.pug +++ b/views/upload.pug @@ -6,9 +6,9 @@ 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