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)