Compare commits

...

4 Commits

Author SHA1 Message Date
CrescentLeaf
d2007fbdcc 進一步完善對外 API 2025-08-29 23:43:59 +08:00
CrescentLeaf
7bf5921298 chore: MAKE LINIT HAPPT 2025-08-29 23:43:24 +08:00
CrescentLeaf
f79fe55fe5 chore: MAKE LINIT HAPPT 2025-08-29 23:43:09 +08:00
CrescentLeaf
91bfbcb3b3 docs: add readme 2025-08-29 22:51:03 +08:00
5 changed files with 103 additions and 67 deletions

11
readme.md Normal file
View File

@@ -0,0 +1,11 @@
## TheWhiteSilk
Under developing...
### TODO
* [x] 接口完備
* [ ] 對外接口
* [ ] 單元測試
* [ ] 基本可用
* [ ] 負載均衡

View File

@@ -2,6 +2,7 @@ import BaseApi from './BaseApi.ts'
import HttpServerLike from '../types/HttpServerLike.ts'
import UserApi from "./UserApi.ts"
import SocketIo from "socket.io"
import UnknownFunction from "../types/UnknownFunction.ts";
export default class ApiManager {
static httpServer: HttpServerLike
@@ -14,9 +15,22 @@ export default class ApiManager {
static getHttpServer() {
return this.httpServer
}
static getSocketIoServer() {
return this.socketIoServer
}
static initAllApis() {
this.apis_instance = {
user: new UserApi()
}
}
static initEvents() {
const io = this.socketIoServer
io.on('connection', (socket) => {
socket.on("The_White_Silk", (name: string, args: {}, callback: UnknownFunction) => {
if (name == null || args == null) return callback({
})
})
})
}
}

View File

@@ -1,4 +1,5 @@
import UnknownFunction from '../types/UnknownFunction.ts'
import ApiManager from "./ApiManager.ts";
export default abstract class BaseApi {
abstract getName(): string
@@ -7,6 +8,7 @@ export default abstract class BaseApi {
}
abstract onInit(): void
registerEvent(name: string, func: UnknownFunction) {
const io = ApiManager.getSocketIoServer()
}
}

View File

@@ -1,65 +1,70 @@
import { DatabaseSync } from "node:sqlite"
import { DatabaseSync, SQLInputValue } from "node:sqlite"
import { Buffer } from "node:buffer"
import path from 'node:path'
import crypto from 'node:crypto'
import fs_sync from 'node:fs'
import chalk from "chalk"
import { fileTypeFromBuffer } from 'file-type'
import config from "../config.ts"
class FileBean {
declare count: number
declare name: string
declare hash: string
declare mime: string
declare chatid?: string
declare upload_time: number
declare last_used_time: number
}
class File {
declare bean: FileBean
constructor(bean: FileBean) {
this.bean = bean
}
private setAttr(key: string, value: SQLInputValue) {
FileManager.database.prepare(`UPDATE ${FileManager.table_name} SET ${key} = ? WHERE count = ?`).run(value, this.bean.count)
this.bean[key] = value
}
getMime() {
return this.bean.mime
}
getName() {
return this.bean.name
}
getFilePath() {
const hash = this.bean.hash
return path.join(
config.data_path,
"files",
hash.substring(0, 1),
hash.substring(2, 3),
hash.substring(3, 4),
this.bean.hash
)
}
getChatId() {
return this.bean.chatid
}
getUploadTime() {
return this.bean.upload_time
}
getLastUsedTime() {
return this.bean.last_used_time
}
readSync() {
this.setAttr("last_used_time", Date.now())
return fs_sync.readFileSync(this.getFilePath())
}
}
export default class FileManager {
static FileBean = class {
declare count: number
declare name: string
declare hash: string
declare mime: string
declare chatid: string | null
declare upload_time: number
declare last_used_time: number
}
static File = class {
declare bean: FileManager.FileBean
constructor(bean: FileManager.FileBean) {
this.bean = bean
}
private setAttr(key: string, value: unknown): void {
FileManager.database.prepare(`UPDATE ${FileManager.table_name} SET ${key} = ? WHERE count = ?`).run(value, this.bean.count)
this.bean[key] = value
}
getMime(): string {
return this.bean.mime
}
getName(): string {
return this.bean.name
}
getFilePath(): string {
const hash = this.bean.hash
return path.join(
config.data_path,
"files",
hash.substring(0, 1),
hash.substring(2, 3),
hash.substring(3, 4),
this.bean.hash
)
}
getChatId(): string | null {
return this.bean.chatid
}
getUploadTime(): number {
return this.bean.upload_time
}
getLastUsedTime(): number {
return this.bean.last_used_time
}
readSync(): Buffer {
this.setAttr("last_used_time", Date.now())
return fs_sync.readFileSync(this.getFilePath())
}
}
static FileBean = FileBean
static File = File
static table_name: string = "FileReferences"
private static database: DatabaseSync = FileManager.init()
static database: DatabaseSync = FileManager.init()
private static init(): DatabaseSync {
const db: DatabaseSync = new DatabaseSync(path.join(config.data_path, FileManager.table_name + '.db'))
db.exec(`
@@ -73,21 +78,21 @@ export default class FileManager {
/* 最后使用时间 */ last_used_time INT8 NOT NULL
);
`)
return db
return db
}
static uploadFile(fileName: string, data: Buffer, chatId: string | null) {
static async uploadFile(fileName: string, data: Buffer, chatId?: string) {
const hash = crypto.createHash('sha256').update(data).digest('hex')
try {
return FileManager.findByHash(hash)
} catch(_e) {
} catch (_e) {
// Do nothing...
}
const mime = fileTypeFromBuffer(data)
const mime = (await fileTypeFromBuffer(data))?.mime || 'application/octet-stream'
fs_sync.writeFileSync(
path.join(
config.data_path,
config.data_path,
"files",
hash.substring(0, 1),
hash.substring(2, 3),
@@ -98,7 +103,7 @@ export default class FileManager {
)
return new FileManager.File(
FileManager.findAllBeansByCondition(
'count = ?',
'count = ?',
FileManager.database.prepare(`INSERT INTO ${FileManager.table_name} (
name,
hash,
@@ -110,23 +115,23 @@ export default class FileManager {
fileName,
hash,
mime,
chatId,
chatId || null,
Date.now(),
-1
).lastInsertRowid
)[0]
)
}
private static findAllBeansByCondition(condition: string, ...args: unknown[]): FileManager.FileBean[] {
return FileManager.database.prepare(`SELECT * FROM ${FileManager.table_name} WHERE ${condition};`).all(...args)
private static findAllBeansByCondition(condition: string, ...args: SQLInputValue[]): FileBean[] {
return FileManager.database.prepare(`SELECT * FROM ${FileManager.table_name} WHERE ${condition};`).all(...args) as unknown as FileBean[]
}
static findByHash(hash: string): FileManager.File {
static findByHash(hash: string): File {
const beans = FileManager.findAllBeansByCondition('hash = ?', hash)
if (beans.length == 0)
throw new Error(`找不到 hash 为 ${hash} 的文件`)
else if (beans.length > 1)
console.error(chalk.red(`警告: 查询 hash = ${id} 时, 查询到多个相同 Hash 的文件`))
console.error(chalk.red(`警告: 查询 hash = ${hash} 时, 查询到多个相同 Hash 的文件`))
return new FileManager.File(beans[0])
}
}

View File

@@ -0,0 +1,4 @@
type ApiCallbackMessage = {
msg: string,
code: 200 | 400 | 401 | 403 | 404 | 500 | 501
}