diff --git a/src/server/routes/fileApiRoutes.ts b/src/server/routes/fileApiRoutes.ts index d6982c3..3c252aa 100644 --- a/src/server/routes/fileApiRoutes.ts +++ b/src/server/routes/fileApiRoutes.ts @@ -3,6 +3,7 @@ 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"; @@ -44,15 +45,35 @@ fileApiRoutes.get("/list", (req,res) => { }) -fileApiRoutes.post("/action", (req,res) => { +fileApiRoutes.post("/manage", (req,res) => { if (!auth.validate(req.cookies.auth)) { ServeError(res, 401, "not logged in") return } - let acc = Accounts.getFromToken(req.cookies.auth) + let acc = Accounts.getFromToken(req.cookies.auth) as Accounts.Account if (!acc) return + if (!req.body.target || req.body.target.length < 1) return + + req.body.target.forEach((e:string) => { + if (!acc.files.includes(e)) return + + switch( req.body.action ) { + case "delete": + files.unlink(e) + break; + + case "changeFileVisibility": + if (!["public","anonymous","private"].includes(req.body.value)) return; + files.files[e].visibility = req.body.visibility; + + writeFile(process.cwd()+"/.data/files.json",JSON.stringify(files.files), (err) => { + if (err) console.log(err) + }) + break; + } + }) }) \ No newline at end of file diff --git a/src/svelte/elem/prompts/uploads.js b/src/svelte/elem/prompts/uploads.js index 2db6879..1322209 100644 --- a/src/svelte/elem/prompts/uploads.js +++ b/src/svelte/elem/prompts/uploads.js @@ -1,8 +1,8 @@ import { fetchAccountData, account } from "../stores.mjs" import { get } from "svelte/store"; -export function dfv(optPicker) { - optPicker.picker("Default file visibility",[ +export let options = { + FV: [ { name: "Public", icon: "/static/assets/icons/public.svg", @@ -21,7 +21,18 @@ export function dfv(optPicker) { description: "Nobody but you can view your uploads", id: "private" } - ]).then((exp) => { + ], + AYS: [ + { + name: "Yes", + icon: "/static/assets/icons/update.svg", + id: true + } + ] +} + +export function dfv(optPicker) { + optPicker.picker("Default file visibility",options.FV).then((exp) => { if (exp && exp.selected) { fetch(`/auth/dfv`,{method:"POST", body:JSON.stringify({ defaultFileVisibility: exp.selected @@ -47,11 +58,11 @@ export function update_all_files(optPicker) { } ]).then((exp) => { if (exp && exp.selected) { - fetch(`/files/action`,{method:"POST", body:JSON.stringify({ + fetch(`/files/manage`,{method:"POST", body:JSON.stringify({ target:get(account).files, - action: { - visibility: get(account).defaultFileVisibility - } + action: "changeFileVisibility", + + value: get(account).defaultFileVisibility })}).then((response) => { if (response.status != 200) { @@ -85,8 +96,52 @@ export function fileOptions(optPicker,file) { id: "delete" } ]).then((exp) => { + if (exp && exp.selected) { + switch( exp.selected ) { + + case "delete": + + fetch(`/files/manage`,{method:"POST", body:JSON.stringify({ + target: [ file ], + action: "delete", + })}).then((response) => { + + if (response.status != 200) { + optPicker.picker(`${response.status} ${response.statusText}`,[]) + } + + fetchFilePointers() + }) + + case "changeFileVisibility": + + optPicker.picker("Set file visibility", options.FV).then((exp) => { + + if (exp && exp.selected) { + + fetch(`/files/manage`, {method: "POST", body: JSON.stringify({ + target: [ file ], + action: "changeFileVisibility", + + value: exp.selected + })}).then((response) => { + + if (response.status != 200) { + optPicker.picker(`${response.status} ${response.statusText}`,[]) + } + + fetchFilePointers() + }) + + } + + }) + + } + } + }) } \ No newline at end of file diff --git a/src/svelte/elem/pulldowns/Accounts.svelte b/src/svelte/elem/pulldowns/Accounts.svelte index fee83f6..1135ef9 100644 --- a/src/svelte/elem/pulldowns/Accounts.svelte +++ b/src/svelte/elem/pulldowns/Accounts.svelte @@ -187,7 +187,7 @@ @@ -196,6 +196,11 @@

Delete file

+ + {/if}


{$account.id}