clean up file updates

This commit is contained in:
May 2023-12-05 21:10:23 +00:00 committed by GitHub
parent c14ddf8397
commit 1fabc2ff82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 62 deletions

View file

@ -69,10 +69,12 @@ export class Client {
} }
async sendDataMessage(formData: FormData) { async sendMessageChunk(formData: FormData) {
this.rest.fetch(`/channels/${this.targetChannel}/messages`, { let returned = await this.rest.fetch(`/channels/${this.targetChannel}/messages`, {
method: "POST", method: "POST",
body: formData body: formData
}) })
return (await returned.json() as { id: string }).id
} }
} }

View file

@ -256,7 +256,8 @@ export default class Files {
} }
const { filename, mime, owner } = metadata const { filename, mime, owner } = metadata
return this.writeFile(uploadId, {
this.files[uploadId] = {
filename, filename,
messageids: msgIds, messageids: msgIds,
mime, mime,
@ -272,21 +273,22 @@ export default class Files {
...((existingFile || {}).tag ? { tag: existingFile.tag } : {}), ...((existingFile || {}).tag ? { tag: existingFile.tag } : {}),
chunkSize: this.config.maxDiscordFileSize, chunkSize: this.config.maxDiscordFileSize,
}
return this.write().then(_ => uploadId).catch(_ => {
delete this.files[uploadId]
throw { status: 500, message: "failed to save database" }
}) })
} }
// fs // fs
/** /**
* @description Writes a file to disk * @description Saves file database
* @param uploadId New file's ID *
* @param file FilePointer representing the new file
* @returns Promise which resolves to the file's ID
*/ */
async writeFile(uploadId: string, file: FilePointer): Promise<string> { async write(): Promise<void> {
this.files[uploadId] = file await writeFile(
return writeFile(
process.cwd() + "/.data/files.json", process.cwd() + "/.data/files.json",
JSON.stringify( JSON.stringify(
this.files, this.files,
@ -294,15 +296,31 @@ export default class Files {
process.env.NODE_ENV === "development" ? 4 : undefined process.env.NODE_ENV === "development" ? 4 : undefined
) )
) )
.then(() => uploadId) }
.catch(() => {
delete this.files[uploadId] /**
throw { * @description Update a file from monofile 1.2 to allow for range requests with Content-Length to that file.
status: 500, * @param uploadId Target file's ID
message: */
"server may be misconfigured, contact admin for help",
} async update( uploadId: string ) {
}) let target_file = this.files[uploadId]
let attachment_sizes = []
for (let message of target_file.messageids) {
let attachments = (await this.api.fetchMessage(message)).attachments
for (let attachment of attachments) {
attachment_sizes.push(attachment.size)
}
}
if (!target_file.sizeInBytes)
target_file.sizeInBytes = attachment_sizes.reduce((a, b) => a + b, 0)
if (!target_file.chunkSize)
target_file.chunkSize = attachment_sizes[0]
} }
/** /**
@ -315,15 +333,9 @@ export default class Files {
uploadId: string, uploadId: string,
range?: { start: number; end: number } range?: { start: number; end: number }
): Promise<Readable> { ): Promise<Readable> {
if (!this.uploadChannel) {
throw {
status: 503,
message: "server is not ready - please try again later",
}
}
if (this.files[uploadId]) { if (this.files[uploadId]) {
let file = this.files[uploadId] let file = this.files[uploadId]
if (!file.sizeInBytes || !file.chunkSize) await this.update(uploadId)
let scan_msg_begin = 0, let scan_msg_begin = 0,
scan_msg_end = file.messageids.length - 1, scan_msg_end = file.messageids.length - 1,
@ -346,17 +358,15 @@ export default class Files {
let attachments: APIAttachment[] = [] let attachments: APIAttachment[] = []
/* File updates */
let file_updates: Pick<FilePointer, "chunkSize" | "sizeInBytes"> =
{}
let atSIB: number[] = [] // keeps track of the size of each file...
let msgIdx = scan_msg_begin let msgIdx = scan_msg_begin
let getNextAttachment = async () => { let getNextAttachment = async () => {
// return first in our attachment buffer
let ret = attachments.splice(0,1)[0] let ret = attachments.splice(0,1)[0]
if (ret) return ret if (ret) return ret
// oh, there's none left. let's fetch a new message, then.
if (!file.messageids[msgIdx]) return null
let msg = await this.api let msg = await this.api
.fetchMessage(file.messageids[msgIdx]) .fetchMessage(file.messageids[msgIdx])
.catch(() => { .catch(() => {
@ -378,7 +388,6 @@ export default class Files {
i++ i++
) { ) {
attachments.push(attach[i]) attachments.push(attach[i])
atSIB.push(attach[i].size)
} }
} }
@ -386,34 +395,6 @@ export default class Files {
return attachments.splice(0,1)[0] return attachments.splice(0,1)[0]
} }
if (!file.sizeInBytes)
file_updates.sizeInBytes = atSIB.reduce((a, b) => a + b, 0)
if (!file.chunkSize) file_updates.chunkSize = atSIB[0]
if (Object.keys(file_updates).length) {
let valid_fp_keys = ["sizeInBytes", "chunkSize"]
let isValidFilePointerKey = (
key: string
): key is "sizeInBytes" | "chunkSize" =>
valid_fp_keys.includes(key)
for (let [key, value] of Object.entries(file_updates)) {
if (isValidFilePointerKey(key)) file[key] = value
}
// The original was a callback so I don't think I'm supposed to `await` this -Jack
// Jack you need to get more sleep man we're using fs/promises
// but also it would slow us down so maybe not
writeFile(
process.cwd() + "/.data/files.json",
JSON.stringify(
this.files,
null,
process.env.NODE_ENV === "development" ? 4 : undefined
)
)
}
let position = 0 let position = 0
let getNextChunk = async () => { let getNextChunk = async () => {
@ -515,10 +496,12 @@ export default class Files {
if (!this.uploadChannel) { if (!this.uploadChannel) {
return return
} }
/*
for (let x of tmp.messageids) { for (let x of tmp.messageids) {
this.api.deleteMessage(x) this.api.deleteMessage(x)
.catch((err) => console.error(err)) .catch((err) => console.error(err))
} }*/
this.api.deleteMessages(tmp.messageids)
delete this.files[uploadId] delete this.files[uploadId]
if (noWrite) { if (noWrite) {