chat中添加上传文件功能

This commit is contained in:
539943419 committed 2024-04-03 11:33:46 +08:00
1 parent 709cac04f6
commit dd56403d93
7 files changed
+427 -43

No files matched your search

+63 -2
View File
@@ -53,6 +53,7 @@ CREATE TABLE IF NOT EXISTS USERS(
);`);
const stmt_getPermissionGroup = db.prepare("SELECT user_group FROM USERS WHERE user_id = ? AND password_hash = ?");
const stmt_getFileName = db.prepare(`SELECT fileName FROM id2fileName WHERE id = ?`);
const stmt_getFileInfo = db.prepare(`SELECT fileName,fileSize FROM id2fileName WHERE id = ?`);
const stmt_getUserUploadFilesSizeCount = db.prepare("SELECT uploadFilesSizeCount FROM USERS WHERE user_id = ?");
const stmt_getUserGroup = db.prepare("SELECT user_group FROM USERS WHERE user_id = ?");
const stmt_getTimestamp = db.prepare(`SELECT timestamp FROM id2fileName WHERE id = ?`);
@@ -78,6 +79,30 @@ const db_getFileName = id => {
return null;
}
};
// const db_getFileInfo = id => {
// try {
// if (stmt_getFreeze.get(id)["freeze"] == 1) return null;
// return stmt_getFileSize.get(id);
// } catch (e) {
// return null;
// }
// };
const db_getFileInfo = (function(){
const infoCache = {}
return function(id){
if(infoCache[id]){
return infoCache[id]
}
try {
if (stmt_getFreeze.get(id)["freeze"] == 1) return null;
const info = stmt_getFileInfo.get(id);
infoCache[id]=info
return info;
} catch (e) {
return null;
}
}
})()
// 检查是否可下载
const db_downloadable = id => {
try {
@@ -237,6 +262,28 @@ function downloadFile(req, res, fileId) {
}
}
function getFileInfo(req, res, fileId) {
try {
if (db_downloadable(fileId) == false) {
res.writeHead(404);
res.end(null);
return;
}
// db_updateDownloadCredit(fileId);
let info = db_getFileInfo(fileId);
if (info == null) {
res.writeHead(404);
res.end(null);
return;
}
res.end(JSON.stringify(info));
} catch (err) {
console.log(err);
res.writeHead(404);
res.end(null);
}
}
const getUserGroup = (function(){
const cache = {}
return function(userId){
@@ -318,6 +365,20 @@ app.get("/file/download", verifyToken, function (req, res) {
}
});
app.get("/file/info", verifyToken, function (req, res) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "*");
res.header("Access-Control-Allow-Methods", "GET");
res.header("Access-Control-Expose-Headers", "File-Name");
var fileId = req.query.fileName;
if (!fileId) {
res.statusCode = 500;
res.end("FileName Not Found");
} else {
getFileInfo(req, res, fileId);
}
});
// // 启动服务器
// app.listen(PORT, HOST, function(){
// console.log(app)
@@ -442,10 +503,10 @@ function initMsgHistory(gID) {
let verify = stmt_getGroupVerify.get(gID).verify;
// 暂时这么写
let msgHistory = db
.prepare(`SELECT message,type,sender,timestamp FROM ${gID}`)
.prepare(`SELECT message,type,type1,sender,timestamp FROM ${gID}`)
.all()
.map(v => {
return { type: v.type, msg: v.message, from: v.sender, timestamp: v.timestamp };
return { type: v.type,type1:v.type1, msg: v.message, from: v.sender, timestamp: v.timestamp };
});
// console.log(msgHistory)
groupsMap[gID] = { verify, member: [], msgHistory };