ok so small few changes

This commit is contained in:
May 2023-05-11 20:42:24 +00:00
parent eb53609e79
commit b029dc9e12
6 changed files with 98 additions and 12 deletions

View file

@ -14,6 +14,7 @@ import * as Accounts from "./lib/accounts"
import { authRoutes, auth_setFilesObj } from "./routes/authRoutes";
import { fileApiRoutes, setFilesObj } from "./routes/fileApiRoutes";
import { adminRoutes, admin_setFilesObj } from "./routes/adminRoutes";
require("dotenv").config()
@ -50,6 +51,7 @@ let files = new Files(client,config)
setFilesObj(files)
auth_setFilesObj(files)
admin_setFilesObj(files)
// routes (could probably make these use routers)

View file

@ -49,7 +49,8 @@ export interface FilePointer {
owner?:string,
sizeInBytes?:number,
tag?:string,
visibility?:FileVisibility
visibility?:FileVisibility,
reserved?: boolean
}
export interface StatusCodeError {
@ -140,7 +141,8 @@ export default class Files {
sizeInBytes:0,
owner:settings.owner,
visibility: settings.owner ? "private" : "public"
visibility: settings.owner ? "private" : "public",
reserved: true
}
// get buffer
@ -196,6 +198,18 @@ export default class Files {
files.index(settings.owner,uploadId)
}
// 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 ogf.messageids) {
this.uploadChannel.messages.fetch(x).then((m) => {
m.delete()
}).catch((e) => {
console.error(e)
})
}
resolve(await this.writeFile(
uploadId,
{
@ -205,7 +219,14 @@ export default class Files {
sizeInBytes:fBuffer.byteLength,
owner:settings.owner,
visibility: settings.owner ? Accounts.getFromId(settings.owner)?.defaultFileVisibility : undefined
visibility: ogf ? ogf.visibility
: (
settings.owner
? Accounts.getFromId(settings.owner)?.defaultFileVisibility
: undefined
),
// so that json.stringify doesnt include tag:undefined
...(ogf.tag ? {tag:ogf.tag} : {})
}
))
})
@ -286,10 +307,22 @@ export default class Files {
if (tmp.owner) {
files.deindex(tmp.owner,uploadId)
}
// 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)
})
}
delete this.files[uploadId];
writeFile(process.cwd()+"/.data/files.json",JSON.stringify(this.files),(err) => {
if (err) {
this.files[uploadId] = tmp
this.files[uploadId] = tmp // !! this may not work, since tmp is a link to this.files[uploadId]?
reject()
} else {
resolve()

View file

@ -0,0 +1,36 @@
import bodyParser from "body-parser";
import { Router } from "express";
import * as Accounts from "../lib/accounts";
import * as auth from "../lib/auth";
import bytes from "bytes"
import {writeFile} from "fs";
import ServeError from "../lib/errors";
import Files from "../lib/files";
let parser = bodyParser.json({
type: ["text/plain","application/json"]
})
export let adminRoutes = Router();
let files:Files
export function admin_setFilesObj(newFiles:Files) {
files = newFiles
}
let config = require(`${process.cwd()}/config.json`)
adminRoutes.post("/manage", parser, (req,res) => {
if (!auth.validate(req.cookies.auth)) {
ServeError(res, 401, "not logged in")
return
}
let acc = Accounts.getFromToken(req.cookies.auth) as Accounts.Account
if (!acc) return
if (!acc.admin) return
})

View file

@ -62,6 +62,15 @@ fileApiRoutes.post("/manage", parser, (req,res) => {
req.body.target.forEach((e:string) => {
if (!acc.files.includes(e)) return
let fp = files.getFilePointer(e)
if (fp.reserved) {
if (req.body.target.length == 1) {
ServeError(res, 400, `cannot modify a file that is being uploaded, please contact an administrator if your file is stuck in this state.`)
}
return
}
switch( req.body.action ) {
case "delete":
files.unlink(e)

View file

@ -165,6 +165,14 @@ export function fileOptions(optPicker,file) {
case "tag":
if (file.tag) {
fetch(`/files/manage`, {method: "POST", body: JSON.stringify({
target: [ file.id ],
action: "setTag"
})}).then(fetchFilePointers)
return
}
optPicker.picker("Enter a tag (max 30char)",[
{
name: "Tag name",
@ -175,20 +183,13 @@ export function fileOptions(optPicker,file) {
{
name: "OK",
icon: "/static/assets/icons/update.svg",
description: "",
id: true
}
]).then((exp) => {
if (exp && exp.selected) {
if (file.tag) {
fetch(`/files/manage`, {method: "POST", body: JSON.stringify({
target: [ file.id ],
action: "setTag"
})}).then(fetchFilePointers)
return
}
fetch(`/files/manage`, {method: "POST", body: JSON.stringify({
target: [ file.id ],
action: "setTag",

View file

@ -37,6 +37,11 @@
<p class="detail">
<img src="/static/assets/icons/{file.visibility || "public"}.svg" alt={file.visibility||"public"} />&nbsp;
<span class="number">{file.id}</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="number">{file.mime.split(";")[0]}</span>
{#if file.reserved}
<br />
<img src="/static/assets/icons/update.svg" alt="uploading"/>&nbsp;
This file is currently being uploaded. Please wait.
{/if}
{#if file.tag}
<br />
<img src="/static/assets/icons/tag.svg" alt="tag"/>&nbsp;