From 445c1321ba2c48255d5313ea1e99b5d97fe512aa Mon Sep 17 00:00:00 2001 From: speatzle Date: Thu, 9 May 2024 20:06:30 +0200 Subject: [PATCH] serve library files for download --- server/file.go | 26 ++++++++++++++++++++++++++ server/server.go | 1 + 2 files changed, 27 insertions(+) create mode 100644 server/file.go diff --git a/server/file.go b/server/file.go new file mode 100644 index 0000000..3fb7c84 --- /dev/null +++ b/server/file.go @@ -0,0 +1,26 @@ +package server + +import ( + "log/slog" + "net/http" +) + +func handleFile(w http.ResponseWriter, r *http.Request) { + id := r.PathValue("id") + if id == "" { + http.Error(w, "No id", http.StatusBadRequest) + return + } + + // TODO check if worker is working on a task involving this file + + var path string + err := db.QueryRow(r.Context(), "SELECT path FROM files WHERE id = $1", id).Scan(&path) + if err != nil { + http.Error(w, "Error Getting Path: "+err.Error(), http.StatusBadRequest) + slog.ErrorContext(r.Context(), "Getting Path", "err", err) + return + } + + http.ServeFile(w, r, path) +} diff --git a/server/server.go b/server/server.go index 0a5985d..7992fba 100644 --- a/server/server.go +++ b/server/server.go @@ -75,6 +75,7 @@ func Start(_conf config.Config, tmplFS embed.FS, staticFS embed.FS, migrationsFS mux.HandleFunc("/worker", handleWorkerWebsocket) mux.Handle("/static/", fs) mux.HandleFunc("/tasks", handleTasks) + mux.HandleFunc("/files/{id}", handleFile) mux.HandleFunc("/tasks/{id}", handleTask) mux.HandleFunc("/scan/{id}", handleScan) mux.HandleFunc("/libraries/{id}", handleLibrary)