修正 FileManager.uploadFile, 添加更多元数据, 修正一个无法被 lint 的错误

其中, 修正 lint 是把 FileBean 从 interface 换成了 class 定义
This commit is contained in:
CrescentLeaf
2025-08-14 12:01:08 +08:00
parent 1eb8b50b78
commit b39be9301d

View File

@@ -7,16 +7,17 @@ import fs from 'node:fs/promises'
import { fileTypeFromBuffer } from 'file-type'
export default class FileManager {
static FileBean = interface {
count: number,
name: string,
hash: string,
mime: string,
static FileBean = class {
declare count: number
declare name: string
declare hash: string
declare mime: string
declare chatid: string
}
static File = class {
declare bean: FileManager.FileBean
constructor(bean: UserBean) {
constructor(bean: FileManager.FileBean) {
this.bean = bean
}
getMime(): string {
@@ -36,6 +37,9 @@ export default class FileManager {
this.bean.hash
)
}
getChatId(): string {
return this.bean.chatid
}
async readAsync(): Buffer {
return await fs.readFile(this.getFilePath())
}
@@ -50,16 +54,49 @@ export default class FileManager {
/* 序号 */ count INTEGER PRIMARY KEY AUTOINCREMENT,
/* 文件名称 */ name TEXT NOT NULL,
/* 文件哈希 */ hash TEXT NOT NULL,
/* MIME 类型 */ mime TEXT NOT NULL
/* MIME 类型 */ mime TEXT NOT NULL,
/* 来源 Chat */ chatid TEXT NOT NULL,
/* 上传时间 */ upload_time INT8 NOT NULL,
/* 最后使用时间 */ last_used_time INT8 NOT NULL
);
`)
return db
}
static uploadFile(fileName: string, data: Buffer) {
static async uploadFile(fileName: string, data: Buffer, chatId: string) {
const hash = crypto.createHash('sha256').update(data).digest('hex')
mime = fileTypeFromBuffer(data)
const mime = fileTypeFromBuffer(data)
await fs.writeFile(
path.join(
config.data_path,
"files",
hash.substring(0, 1),
hash.substring(2, 3),
hash.substring(3, 4),
hash
),
data
)
return new FileManager.File(
FileManager.findAllBeansByCondition(
'count = ?',
FileManager.database.prepare(`INSERT INTO ${FileManager.table_name} (
name,
hash,
mime,
chatid,
upload_time,
last_used_time
) VALUES (?, ?, ?, ?, ?, ?);`).run(
fileName,
hash,
mime,
chatId,
Date.now(),
-1
).lastInsertRowid
)[0]
)
}
private static findAllBeansByCondition(condition: string, ...args: unknown[]): FileManager.FileBean[] {