diff --git a/src/server/lib/accounts.ts b/src/server/lib/accounts.ts index 7cd1386..87c2357 100644 --- a/src/server/lib/accounts.ts +++ b/src/server/lib/accounts.ts @@ -149,7 +149,7 @@ export namespace files { * @param fileId The target file's ID * @returns Promise that resolves after accounts.json finishes writing */ - export function index(accountId:string,fileId:string) { + export function index(accountId:string,fileId:string,noWrite:boolean = false) { // maybe replace with a obj like // { x:true } // for faster lookups? not sure if it would be faster @@ -158,7 +158,7 @@ export namespace files { if (acc.files.find(e => e == fileId)) return acc.files.push(fileId) - return save() + if (!noWrite) return save() } /** diff --git a/src/server/lib/files.ts b/src/server/lib/files.ts index 3ada911..10c2a39 100644 --- a/src/server/lib/files.ts +++ b/src/server/lib/files.ts @@ -11,6 +11,7 @@ import * as Accounts from "./accounts.js" import { z } from "zod" import * as schemas from "./schemas/files.js" import { issuesToMessage } from "./middleware.js" +import file from "../routes/api/v1/file/index.js" export let alphanum = Array.from( "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" @@ -695,8 +696,41 @@ export default class Files { delete this.files[uploadId] if (!noWrite) - this.write().catch((err) => { - throw err - }) + return this.write() + } + + async chown(uploadId: string, newOwner?: string, noWrite: boolean = false) { + let target = this.files[uploadId] + if (target.owner) { + let i = files.deindex(target.owner, uploadId, Boolean(newOwner && noWrite)) + if (i) await i + } + + target.owner = newOwner + if (newOwner) { + let i = files.index(newOwner, uploadId, noWrite) + if (i) await i + } + + if (!noWrite) + return this.write() + } + + async mv(uploadId: string, newId: string, noWrite: boolean = false) { + let target = this.files[uploadId] + if (target.owner) { + let owner = Accounts.getFromId(target.owner) + if (owner) { + owner.files.splice(owner.files.indexOf(uploadId), 1, newId) + if (!noWrite) + await Accounts.save() + } + } + + this.files[newId] = target + delete this.files[uploadId] + + if (!noWrite) + return this.write() } } \ No newline at end of file diff --git a/src/server/lib/middleware.ts b/src/server/lib/middleware.ts index f69c118..195ad92 100644 --- a/src/server/lib/middleware.ts +++ b/src/server/lib/middleware.ts @@ -103,6 +103,7 @@ export const issuesToMessage = function(issues: z.ZodIssue[]) { export const scheme = function(scheme: z.ZodTypeAny): RequestHandler { return async function(ctx, next) { let chk = scheme.safeParse(await ctx.req.json()) + ctx.set("parsedScheme", chk.data) if (chk.success) return next() else return ServeError(ctx, 400, issuesToMessage(chk.error.issues)) } diff --git a/src/server/routes/api/v1/file/index.ts b/src/server/routes/api/v1/file/index.ts index 5a2ee98..0bc47cb 100644 --- a/src/server/routes/api/v1/file/index.ts +++ b/src/server/routes/api/v1/file/index.ts @@ -4,17 +4,20 @@ import * as auth from "../../../../lib/auth.js" import RangeParser, { type Range } from "range-parser" import ServeError from "../../../../lib/errors.js" import Files, { WebError } from "../../../../lib/files.js" -import { getAccount, requiresPermissions } from "../../../../lib/middleware.js" +import { getAccount, requiresAccount, requiresPermissions, scheme } from "../../../../lib/middleware.js" import {Readable} from "node:stream" import type {ReadableStream as StreamWebReadable} from "node:stream/web" import formidable from "formidable" import { HttpBindings } from "@hono/node-server" import pkg from "../../../../../../package.json" assert {type: "json"} import { type StatusCode } from "hono/utils/http-status" +import { z } from "zod" +import { FileSchemas } from "../../../../lib/schemas/index.js" const router = new Hono<{ Variables: { - account: Accounts.Account + account: Accounts.Account, + parsedSchema: any }, Bindings: HttpBindings }>()