wip: port FileDownload

This commit is contained in:
Michael Hohn
2024-05-20 14:27:50 -07:00
committed by =Michael Hohn
parent ccf064fe6c
commit cf595f338a
2 changed files with 42 additions and 3 deletions

View File

@@ -10,13 +10,13 @@ import (
"log"
"log/slog"
"net/http"
"path/filepath"
"strconv"
"strings"
"mrvacommander/pkg/storage"
"github.com/gorilla/mux"
"github.com/hohn/ghes-mirva-server/analyze"
"github.com/hohn/ghes-mirva-server/api"
co "github.com/hohn/ghes-mirva-server/common"
)
@@ -237,11 +237,35 @@ func (c *CommanderSingle) DownloadResponse(w http.ResponseWriter, js co.JobSpec,
}
func (c *CommanderSingle) MirvaDownloadServe(w http.ResponseWriter, r *http.Request) {
// TODO Port this function from ghes-mirva-server
vars := mux.Vars(r)
slog.Info("File download request", "local_path", vars["local_path"])
analyze.FileDownload(w, vars["local_path"])
FileDownload(w, vars["local_path"])
}
func FileDownload(w http.ResponseWriter, path string) {
slog.Debug("Sending zip file with .sarif/.bqrs", "path", path)
fpath, res, err := storage.ResultAsFile(path)
if err != nil {
http.Error(w, "Failed to read results", http.StatusInternalServerError)
return
}
// Set headers
fname := filepath.Base(fpath)
w.Header().Set("Content-Disposition", "attachment; filename="+fname)
w.Header().Set("Content-Type", "application/octet-stream")
// Copy the file contents to the response writer
rdr := bytes.NewReader(res)
_, err = io.Copy(w, rdr)
if err != nil {
http.Error(w, "Failed to send file", http.StatusInternalServerError)
return
}
slog.Debug("Uploaded file", "path", fpath)
}
func (c *CommanderSingle) MirvaRequestID(w http.ResponseWriter, r *http.Request) {

View File

@@ -195,3 +195,18 @@ func GetStatus(sessionid int, orl co.OwnerRepo) co.Status {
defer mutex.Unlock()
return status[co.JobSpec{ID: sessionid, OwnerRepo: orl}]
}
func ResultAsFile(path string) (string, []byte, error) {
fpath := path
if !filepath.IsAbs(path) {
fpath = "/" + path
}
file, err := os.ReadFile(fpath)
if err != nil {
slog.Warn("Failed to read results file", fpath, err)
return "", nil, err
}
return fpath, file, nil
}