drop .getFilePointer()

This commit is contained in:
May 2023-12-06 19:21:52 +00:00 committed by GitHub
parent 1fabc2ff82
commit a5964f5ac8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 57 deletions

View file

@ -245,15 +245,7 @@ export default class Files {
} }
} }
// this code deletes the files from discord, btw if (existingFile) this.api.deleteMessages(existingFile.messageids)
// if need be, replace with job queue system
if (existingFile && this.uploadChannel) {
for (let x of existingFile.messageids) {
this.api.deleteMessage(x)
.catch((err) => console.error(err))
}
}
const { filename, mime, owner } = metadata const { filename, mime, owner } = metadata
@ -482,50 +474,20 @@ export default class Files {
* @param noWrite Whether or not the change should be written to disk. Enable for bulk deletes * @param noWrite Whether or not the change should be written to disk. Enable for bulk deletes
*/ */
async unlink(uploadId: string, noWrite: boolean = false): Promise<void> { async unlink(uploadId: string, noWrite: boolean = false): Promise<void> {
let tmp = this.files[uploadId] let target = this.files[uploadId]
if (!tmp) { if (!target) return
return if (target.owner) {
} let id = files.deindex(target.owner, uploadId, noWrite)
if (tmp.owner) {
let id = files.deindex(tmp.owner, uploadId, noWrite)
if (id) await id if (id) await id
} }
// this code deletes the files from discord, btw
// if need be, replace with job queue system
if (!this.uploadChannel) { await this.api.deleteMessages(target.messageids)
return
}
/*
for (let x of tmp.messageids) {
this.api.deleteMessage(x)
.catch((err) => console.error(err))
}*/
this.api.deleteMessages(tmp.messageids)
delete this.files[uploadId] delete this.files[uploadId]
if (noWrite) { if (noWrite) return
return return this.write().catch((err) => {
}
return writeFile(
process.cwd() + "/.data/files.json",
JSON.stringify(
this.files,
null,
process.env.NODE_ENV === "development" ? 4 : undefined
)
).catch((err) => {
this.files[uploadId] = tmp // !! this may not work, since tmp is a link to this.files[uploadId]?
throw err throw err
}) })
} }
/**
* @description Get a file's FilePointer
* @param uploadId Target file's ID
* @returns FilePointer for the file
*/
getFilePointer(uploadId: string): FilePointer {
return this.files[uploadId]
}
} }

View file

@ -82,7 +82,7 @@ module.exports = function (files: Files) {
return ctx.status(404) return ctx.status(404)
} }
let targetFile = files.getFilePointer(body.target) let targetFile = files.files[body.target]
if (!targetFile) { if (!targetFile) {
return ctx.status(404) return ctx.status(404)
@ -154,7 +154,7 @@ module.exports = function (files: Files) {
return ctx.status(404) return ctx.status(404)
} }
let targetFile = files.getFilePointer(body.target) let targetFile = files.files[body.target]
if (!targetFile) { if (!targetFile) {
return ctx.status(404) return ctx.status(404)
} }
@ -176,7 +176,7 @@ module.exports = function (files: Files) {
targetFile.owner = newOwner ? newOwner.id : undefined targetFile.owner = newOwner ? newOwner.id : undefined
files files
.writeFile(body.target, targetFile) .write()
.then(() => ctx.status(200)) .then(() => ctx.status(200))
.catch(() => ctx.status(500)) .catch(() => ctx.status(500))
}) })
@ -187,12 +187,12 @@ module.exports = function (files: Files) {
return ctx.status(400) return ctx.status(400)
} }
let targetFile = files.getFilePointer(body.target) let targetFile = files.files[body.target]
if (!targetFile) { if (!targetFile) {
return ctx.status(404) return ctx.status(404)
} }
if (files.getFilePointer(body.new)) { if (files.files[body.new]) {
return ctx.status(400) return ctx.status(400)
} }
@ -201,9 +201,10 @@ module.exports = function (files: Files) {
Accounts.files.index(targetFile.owner, body.new) Accounts.files.index(targetFile.owner, body.new)
} }
delete files.files[body.target] delete files.files[body.target]
files.files[body.new] = targetFile
return files return files
.writeFile(body.new, targetFile) .write()
.then(() => ctx.status(200)) .then(() => ctx.status(200))
.catch(() => { .catch(() => {
files.files[body.target] = body.new files.files[body.target] = body.new

View file

@ -41,7 +41,7 @@ module.exports = function (files: Files) {
ctx.json( ctx.json(
acc.files acc.files
.map((e) => { .map((e) => {
let fp = files.getFilePointer(e) let fp = files.files[e]
if (!fp) { if (!fp) {
Accounts.files.deindex(accId, e) Accounts.files.deindex(accId, e)
return null return null
@ -77,7 +77,7 @@ module.exports = function (files: Files) {
body.target.forEach((e: string) => { body.target.forEach((e: string) => {
if (!acc.files.includes(e)) return if (!acc.files.includes(e)) return
let fp = files.getFilePointer(e) let fp = files.files[e]
if (fp.reserved) { if (fp.reserved) {
return return

View file

@ -36,7 +36,7 @@ module.exports = function (files: Files) {
let acc = ctx.get("account") as Accounts.Account let acc = ctx.get("account") as Accounts.Account
let file = files.getFilePointer(fileId) let file = files.files[fileId]
ctx.header("Access-Control-Allow-Origin", "*") ctx.header("Access-Control-Allow-Origin", "*")
ctx.header("Content-Security-Policy", "sandbox allow-scripts") ctx.header("Content-Security-Policy", "sandbox allow-scripts")
if (ctx.req.query("attachment") == "1") if (ctx.req.query("attachment") == "1")
@ -119,7 +119,7 @@ module.exports = function (files: Files) {
// primaryApi.head( // primaryApi.head(
// ["/file/:fileId", "/cpt/:fileId/*", "/:fileId"], // ["/file/:fileId", "/cpt/:fileId/*", "/:fileId"],
// async (ctx) => { // async (ctx) => {
// let file = files.getFilePointer(req.params.fileId) // let file = files.files[req.params.fileId]
// if ( // if (
// file.visibility == "private" && // file.visibility == "private" &&

View file

@ -10,7 +10,7 @@ export = (files: Files): Handler =>
let acc = ctx.get("account") as Accounts.Account let acc = ctx.get("account") as Accounts.Account
const fileId = ctx.req.param("fileId") const fileId = ctx.req.param("fileId")
const host = ctx.req.header("Host") const host = ctx.req.header("Host")
const file = files.getFilePointer(fileId) const file = files.files[fileId]
if (file) { if (file) {
if (file.visibility == "private" && acc?.id != file.owner) { if (file.visibility == "private" && acc?.id != file.owner) {
return ServeError(ctx, 403, "you do not own this file") return ServeError(ctx, 403, "you do not own this file")