i hope this works lol

next up: backups
This commit is contained in:
May 2023-05-12 22:37:13 +00:00
parent 9dbeb6d94f
commit f4d7128e04
4 changed files with 33 additions and 29 deletions

View file

@ -99,13 +99,13 @@ export namespace files {
return save()
}
export function deindex(accountId:string,fileId:string) {
export function deindex(accountId:string,fileId:string, noWrite:boolean=false) {
let acc = Accounts.find(e => e.id == accountId)
if (!acc) return
let fi = acc.files.findIndex(e => e == fileId)
if (fi) {
acc.files.splice(fi,1)
return save()
if (!noWrite) return save()
}
}
}

View file

@ -202,11 +202,7 @@ export default class Files {
if (ogf&&this.uploadChannel) {
for (let x of ogf.messageids) {
this.uploadChannel.messages.fetch(x).then((m) => {
m.delete()
}).catch((e) => {
console.error(e)
})
this.uploadChannel.messages.delete(x).catch(err => console.error(err))
}
}
@ -307,25 +303,24 @@ export default class Files {
})
}
unlink(uploadId:string):Promise<void> {
return new Promise((resolve,reject) => {
unlink(uploadId:string, noWrite: boolean = false):Promise<void> {
return new Promise(async (resolve,reject) => {
let tmp = this.files[uploadId];
if (!tmp) {resolve(); return}
if (tmp.owner) {
files.deindex(tmp.owner,uploadId)
let id = files.deindex(tmp.owner,uploadId,noWrite);
if (id) await id
}
// this code deletes the files from discord, btw
// if need be, replace with job queue system
if (!this.uploadChannel) {reject(); return}
for (let x of tmp.messageids) {
this.uploadChannel.messages.fetch(x).then((m) => {
m.delete()
}).catch((e) => {
console.error(e)
})
this.uploadChannel.messages.delete(x).catch(err => console.error(err))
}
delete this.files[uploadId];
if (noWrite) {resolve(); return}
writeFile(process.cwd()+"/.data/files.json",JSON.stringify(this.files),(err) => {
if (err) {
this.files[uploadId] = tmp // !! this may not work, since tmp is a link to this.files[uploadId]?

View file

@ -6,6 +6,8 @@ import * as auth from "../lib/auth";
import ServeError from "../lib/errors";
import Files, { FileVisibility, id_check_regex } from "../lib/files";
import { writeFile } from "fs";
let parser = bodyParser.json({
type: ["text/plain","application/json"]
})
@ -160,7 +162,7 @@ authRoutes.post("/customcss", parser, (req,res) => {
}
})
authRoutes.post("/delete_account", parser, (req,res) => {
authRoutes.post("/delete_account", parser, async (req,res) => {
let acc = Accounts.getFromToken(req.cookies.auth)
if (!acc) {
ServeError(res, 401, "not logged in")
@ -172,15 +174,19 @@ authRoutes.post("/delete_account", parser, (req,res) => {
auth.invalidate(v.token)
})
let cpl = () => Accounts.deleteAccount(accId).then(_ => res.send("account deleted"))
if (req.body.deleteFiles) {
acc.files.forEach((v) => {
files.unlink(v)
let f = acc.files.map(e=>e) // make shallow copy so that iterating over it doesnt Die
for (let v of f) {
files.unlink(v,true).catch(err => console.error(err))
}
writeFile(process.cwd()+"/.data/files.json",JSON.stringify(files.files), (err) => {
if (err) console.log(err)
cpl()
})
}
Accounts.deleteAccount(accId)
res.send("account deleted")
} else cpl()
})
authRoutes.post("/change_username", parser, (req,res) => {

View file

@ -70,7 +70,7 @@ fileApiRoutes.post("/manage", parser, (req,res) => {
switch( req.body.action ) {
case "delete":
files.unlink(e)
files.unlink(e, true)
modified++;
break;
@ -91,10 +91,13 @@ fileApiRoutes.post("/manage", parser, (req,res) => {
}
})
writeFile(process.cwd()+"/.data/files.json",JSON.stringify(files.files), (err) => {
if (err) console.log(err)
res.contentType("text/plain")
res.send(`modified ${modified} files`)
})
Accounts.save().then(() => {
writeFile(process.cwd()+"/.data/files.json",JSON.stringify(files.files), (err) => {
if (err) console.log(err)
res.contentType("text/plain")
res.send(`modified ${modified} files`)
})
}).catch((err) => console.error(err))
})