From 58b05e1403b9ba79c3cc2c5ae45a02eb8493eba4 Mon Sep 17 00:00:00 2001 From: marcobaobao Date: Fri, 26 May 2023 15:10:23 +0200 Subject: [PATCH] code refactoring --- frontend/src/Archive.tsx | 3 +-- server/rest/handlers.go | 11 +++-------- server/server.go | 2 +- 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/frontend/src/Archive.tsx b/frontend/src/Archive.tsx index ad73203..4874bf8 100644 --- a/frontend/src/Archive.tsx +++ b/frontend/src/Archive.tsx @@ -66,7 +66,6 @@ export default function Downloaded() { ? folders.slice(-(folders.length - 1)).join('/') : folders.pop() - const _upperLevel = folders.slice(1, -1) const upperLevel = _upperLevel.length === 2 ? ['.', ..._upperLevel].join('/') @@ -131,7 +130,7 @@ export default function Downloaded() { const onFileClick = (path: string) => startTransition(() => { - window.open(`${serverAddr}/play?path=${Buffer.from(path).toString('hex')}`) + window.open(`${serverAddr}/d/${Buffer.from(path).toString('hex')}`) }) const onFolderClick = (path: string) => startTransition(() => { diff --git a/server/rest/handlers.go b/server/rest/handlers.go index f230ca1..4eb3a72 100644 --- a/server/rest/handlers.go +++ b/server/rest/handlers.go @@ -94,12 +94,8 @@ func DeleteFile(ctx *fiber.Ctx) error { return ctx.JSON("ok") } -type PlayRequest struct { - Path string -} - -func PlayFile(ctx *fiber.Ctx) error { - path := ctx.Query("path") +func SendFile(ctx *fiber.Ctx) error { + path := ctx.Params("id") if path == "" { return errors.New("inexistent path") @@ -114,10 +110,9 @@ func PlayFile(ctx *fiber.Ctx) error { // TODO: further path / file validations if strings.Contains(filepath.Dir(string(decoded)), root) { - ctx.SendStatus(fiber.StatusPartialContent) + ctx.SendStatus(fiber.StatusOK) return ctx.SendFile(string(decoded)) } - ctx.Status(fiber.StatusOK) return ctx.SendStatus(fiber.StatusUnauthorized) } diff --git a/server/server.go b/server/server.go index 084fab4..29737f1 100644 --- a/server/server.go +++ b/server/server.go @@ -45,7 +45,7 @@ func RunBlocking(port int, frontend fs.FS) { app.Post("/downloaded", rest.ListDownloaded) app.Post("/delete", rest.DeleteFile) - app.Get("/play", rest.PlayFile) + app.Get("/d/:id", rest.SendFile) // RPC handlers // websocket