i hope this works lol

next up: backups
This commit is contained in:
split / 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() 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) let acc = Accounts.find(e => e.id == accountId)
if (!acc) return if (!acc) return
let fi = acc.files.findIndex(e => e == fileId) let fi = acc.files.findIndex(e => e == fileId)
if (fi) { if (fi) {
acc.files.splice(fi,1) 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) { if (ogf&&this.uploadChannel) {
for (let x of ogf.messageids) { for (let x of ogf.messageids) {
this.uploadChannel.messages.fetch(x).then((m) => { this.uploadChannel.messages.delete(x).catch(err => console.error(err))
m.delete()
}).catch((e) => {
console.error(e)
})
} }
} }
@ -307,25 +303,24 @@ export default class Files {
}) })
} }
unlink(uploadId:string):Promise<void> { unlink(uploadId:string, noWrite: boolean = false):Promise<void> {
return new Promise((resolve,reject) => { return new Promise(async (resolve,reject) => {
let tmp = this.files[uploadId]; let tmp = this.files[uploadId];
if (!tmp) {resolve(); return}
if (tmp.owner) { 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 // this code deletes the files from discord, btw
// if need be, replace with job queue system // if need be, replace with job queue system
if (!this.uploadChannel) {reject(); return} if (!this.uploadChannel) {reject(); return}
for (let x of tmp.messageids) { for (let x of tmp.messageids) {
this.uploadChannel.messages.fetch(x).then((m) => { this.uploadChannel.messages.delete(x).catch(err => console.error(err))
m.delete()
}).catch((e) => {
console.error(e)
})
} }
delete this.files[uploadId]; delete this.files[uploadId];
if (noWrite) {resolve(); return}
writeFile(process.cwd()+"/.data/files.json",JSON.stringify(this.files),(err) => { writeFile(process.cwd()+"/.data/files.json",JSON.stringify(this.files),(err) => {
if (err) { if (err) {
this.files[uploadId] = tmp // !! this may not work, since tmp is a link to this.files[uploadId]? 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 ServeError from "../lib/errors";
import Files, { FileVisibility, id_check_regex } from "../lib/files"; import Files, { FileVisibility, id_check_regex } from "../lib/files";
import { writeFile } from "fs";
let parser = bodyParser.json({ let parser = bodyParser.json({
type: ["text/plain","application/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) let acc = Accounts.getFromToken(req.cookies.auth)
if (!acc) { if (!acc) {
ServeError(res, 401, "not logged in") ServeError(res, 401, "not logged in")
@ -171,16 +173,20 @@ authRoutes.post("/delete_account", parser, (req,res) => {
auth.AuthTokens.filter(e => e.account == accId).forEach((v) => { auth.AuthTokens.filter(e => e.account == accId).forEach((v) => {
auth.invalidate(v.token) auth.invalidate(v.token)
}) })
let cpl = () => Accounts.deleteAccount(accId).then(_ => res.send("account deleted"))
if (req.body.deleteFiles) { if (req.body.deleteFiles) {
acc.files.forEach((v) => { let f = acc.files.map(e=>e) // make shallow copy so that iterating over it doesnt Die
files.unlink(v) 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()
}) })
} } else cpl()
Accounts.deleteAccount(accId)
res.send("account deleted")
}) })
authRoutes.post("/change_username", parser, (req,res) => { authRoutes.post("/change_username", parser, (req,res) => {

View file

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