-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
118 lines (100 loc) · 3.56 KB
/
app.js
File metadata and controls
118 lines (100 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// 引用express框架
const express = require('express');
// 创建网站服务器
const app = express.Router();
// 引入系统模块path获取目录
const path = require('path');
//引入fs文件处理模块
const fs = require("fs");
// 处理文件上传
const formidableMiddleware = require('express-formidable');
// 开放静态文件,使用express自带方法,并使用path模块设置静态文件根目录
app.use(express.static(path.join(__dirname, 'public')));
// 处理post参数
app.use(formidableMiddleware({
// 文件上传目录
uploadDir: path.join(__dirname, 'public', 'uploads'),
// 最大上传文件为2M
maxFileSize: 10 * 1024 * 1024,
// 保留文件扩展名
keepExtensions: true
}, [{
event: "error",
action: () => {
void(0)
}
}]));
//维护一个全局变量,每次调用接口时,更新该变量为最新时间
var Time = new Date();
//该函数用于检测当前时间和全局变量维护的时间是否为同一天,不同则删除文件夹中全部文件
function deleteFile(time, notDeleteFile) {
if (time.getDate() != Time.getDate()) {
//dir存文件夹名称
var dir = fs.readdirSync(path.join(__dirname, 'public/uploads'));
//遍历所有文件夹
dir.forEach(function(itm, index) {
try {
if (itm != notDeleteFile) {
fs.unlinkSync(path.join(__dirname, 'public/uploads', itm));
}
} catch (err) {
console.log("\033[41;30m 删除时错误 \033[0m", new Date().Format("yyyy-MM-dd hh:mm:ss"), ' 抛出的异常 ' + err);
}
});
Time = new Date();
}
}
//从路径中获取文件名
function getFileName(path) {
var name = path.split('/');
name = name[name.length - 1];
name = path.split('\\');
return name[name.length - 1];
}
app.post('/upload', (req, res) => {
deleteFile(new Date(), getFileName(req.files.screenshot.path));
try {
if (req.files) {
fs.renameSync(path.join(req.files.screenshot.path), path.join(__dirname, 'public/uploads', req.fields.key))
return res.send({ message: '文件' + req.files.screenshot.name + '上传成功' });
} else {
return res.status(400).send({ message: '文件上传时遇到错误' });
}
} catch (err) {
return res.status(400).send({ message: '文件上传时遇到错误' + err })
}
})
//遍历检索函数
function searchkey(key) {
//获取文件中数据
try {
//将匹配成功的关键字文件url数组作为返回值
var SearchResult = [];
//dir存文件夹名称
var dir = fs.readdirSync(path.join(__dirname, 'public/uploads'));
//遍历所有文件夹
dir.forEach(function(itm, index) {
if (itm.split('_')[0] == key) {
SearchResult.push({
filename: itm,
path: path.join('/demos/FileTransshipment/uploads', itm)
});
}
})
return SearchResult;
} catch (error) {
console.log("\033[41;30m 错误 \033[0m", error.message);
return [];
}
}
app.post('/query', (req, res) => {
deleteFile(new Date(), getFileName(req.fields.key));
try {
var result = searchkey(req.fields.key);
// console.log(result);
return res.send({ data: result });
} catch (err) {
return res.status(400).send({ message: '文件上传时遇到错误' + err })
}
})
module.exports = app;