use relative paths in file table
requires manual db migration
This commit is contained in:
parent
f5e3e89d20
commit
c423676180
2 changed files with 50 additions and 20 deletions
|
@ -3,6 +3,7 @@ package server
|
|||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func handleFile(w http.ResponseWriter, r *http.Request) {
|
||||
|
@ -13,28 +14,39 @@ func handleFile(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
// 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)
|
||||
var fpath string
|
||||
var libraryID int
|
||||
err := db.QueryRow(r.Context(), "SELECT path, library_id FROM files WHERE id = $1", id).Scan(&fpath, &libraryID)
|
||||
if err != nil {
|
||||
http.Error(w, "Error Getting Path: "+err.Error(), http.StatusBadRequest)
|
||||
slog.ErrorContext(r.Context(), "Getting Path", "err", err)
|
||||
http.Error(w, "Error Getting File Path: "+err.Error(), http.StatusBadRequest)
|
||||
slog.ErrorContext(r.Context(), "Getting File Path", "err", err)
|
||||
return
|
||||
}
|
||||
|
||||
slog.InfoContext(r.Context(), "Serving File Download", "id", id, "path", path)
|
||||
var lpath string
|
||||
err = db.QueryRow(r.Context(), "SELECT path FROM libraries WHERE id = $1", libraryID).Scan(&lpath)
|
||||
if err != nil {
|
||||
http.Error(w, "Error Getting Library Path: "+err.Error(), http.StatusBadRequest)
|
||||
slog.ErrorContext(r.Context(), "Getting Library Path", "err", err)
|
||||
return
|
||||
}
|
||||
|
||||
fullPath := filepath.Join(lpath, fpath)
|
||||
|
||||
slog.InfoContext(r.Context(), "Serving File Download", "id", id, "path", fullPath)
|
||||
|
||||
// had timeout issues
|
||||
http.ServeFile(w, r, path)
|
||||
http.ServeFile(w, r, fullPath)
|
||||
|
||||
/*
|
||||
reader, err := os.Open(path)
|
||||
reader, err := os.Open(fullPath)
|
||||
if err != nil {
|
||||
http.Error(w, "Error Opening File: "+err.Error(), http.StatusInternalServerError)
|
||||
slog.ErrorContext(r.Context(), "Opening File", "err", err)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Disposition", "attachment;filename="+filepath.Base(path))
|
||||
w.Header().Set("Content-Disposition", "attachment;filename="+filepath.Base(fullPath))
|
||||
_, err = io.Copy(w, reader)
|
||||
if err != nil {
|
||||
http.Error(w, "Copy File: "+err.Error(), http.StatusBadRequest)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue