This commit is contained in:
CrescentLeaf
2025-11-23 16:52:48 +08:00
parent 7fcf4ce50b
commit fd3684c436
2 changed files with 54 additions and 11 deletions

View File

@@ -18,6 +18,7 @@ export {
export default class LingChairClient {
declare client: Socket
declare access_token: string
declare device_id: string
declare refresh_token?: string
declare auto_fresh_token: boolean
declare auth_cache: {
@@ -30,15 +31,16 @@ export default class LingChairClient {
server_url: string
device_id: string,
io?: Partial<ManagerOptions & SocketOptions>
auto_fresh_token: boolean
auto_fresh_token?: boolean
}) {
this.auto_fresh_token = args.auto_fresh_token || false
this.device_id = args.device_id
this.client = io(args.server_url, {
transports: ["polling", "websocket", "webtransport"],
...args.io,
auth: {
...args.io?.auth,
device_id: args.device_id,
device_id: this.device_id,
session_id: crypto.randomUUID(),
},
})
@@ -78,7 +80,7 @@ export default class LingChairClient {
else
resolve(res)
} else
resolve(res)
resolve(res)
})
})
}
@@ -181,4 +183,42 @@ export default class LingChairClient {
if (re.code != 200)
throw new CallbackError(re)
}
async uploadFile({
chatId,
fileData,
fileName,
}: { fileName: string, fileData: ArrayBuffer | Blob | Response, chatId?: string }) {
const form = new FormData()
form.append("file",
fileData instanceof ArrayBuffer
? new File([fileData], fileName, { type: 'application/octet-stream' })
: (
fileData instanceof Blob ? fileData :
new File([await fileData.arrayBuffer()], fileName, { type: 'application/octet-stream' })
)
)
form.append('file_name', fileName)
chatId && form.append('chat_id', chatId)
const re = await fetch('./upload_file', {
method: 'POST',
headers: {
"Token": this.access_token,
"Device-Id": this.device_id,
} as HeadersInit,
body: form,
credentials: 'omit',
})
const text = await (await re.blob()).text()
let json
try {
json = JSON.parse(text)
// deno-lint-ignore no-empty
} catch (_) { }
return {
...(json == null ? {
msg: text
} : json),
code: re.status,
} as ApiCallbackMessage
}
}

View File

@@ -35,7 +35,10 @@ export default async function createLingChairServer() {
})
await fs.mkdir(config.data_path + '/upload_cache', { recursive: true })
await fs.unlink(config.data_path + '/upload_cache')
try {
await fs.rmdir(config.data_path + '/upload_cache')
// deno-lint-ignore no-empty
} catch (_) {}
app.use(fileUpload({
limits: { fileSize: 2 * 1024 * 1024 * 1024 },
useTempFiles: true,