重命名

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 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)

View File

@ -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>",

View File

@ -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)
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 } })

View File

@ -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

View File

@ -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