refactor: minor changes and fixes

This commit is contained in:
David Allen 2025-09-20 15:44:08 -06:00
parent 72c52fbac6
commit c1c5ec1625
Signed by: towk
GPG key ID: 0430CDBE22619155
3 changed files with 39 additions and 14 deletions

View file

@ -1,6 +1,7 @@
package service
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
@ -16,44 +17,63 @@ import (
makeshift "git.towk2.me/towk/makeshift/pkg"
"git.towk2.me/towk/makeshift/pkg/storage"
"git.towk2.me/towk/makeshift/pkg/util"
"github.com/go-chi/chi/v5"
"github.com/rs/zerolog/log"
)
func (s *Service) Download() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
path = s.PathForData() + strings.TrimPrefix(r.URL.Path, "/download")
pluginKWArgs = chi.URLParam(r, "kwargs")
pluginArgs = strings.Split(r.URL.Query().Get("args"), ",")
pluginNames = strings.Split(r.URL.Query().Get("plugins"), ",")
profileIDs = strings.Split(r.URL.Query().Get("profiles"), ",")
path = s.PathForData() + strings.TrimPrefix(r.URL.Path, "/download")
pluginArgs = strings.Split(r.URL.Query().Get("args"), ",")
pluginNames = strings.Split(r.URL.Query().Get("plugins"), ",")
profileIDs = strings.Split(r.URL.Query().Get("profiles"), ",")
kw *kwargs.KWArgs
kw *kwargs.KWArgs = new(kwargs.KWArgs)
fileInfo os.FileInfo
out *os.File
store *storage.MemoryStorage = new(storage.MemoryStorage)
hooks []makeshift.Hook
contents []byte
decoded []byte
errs []error
err error
)
// parse the KWArgs from request
kw.Set(pluginKWArgs)
// initialize storage
store.Init()
store.SetKWArgs(kw)
decoded, err = base64.RawURLEncoding.DecodeString(r.URL.Query().Get("kwargs"))
if err != nil {
s.writeErrorResponse(w, err.Error(), http.StatusBadRequest)
return
}
log.Debug().
Str("path", path).
Str("client_host", r.Host).
Strs("plugins", pluginNames).
Strs("profiles", profileIDs).
Strs("args", pluginArgs).
Str("kwargs", string(decoded)).
Any("query", r.URL.Query()).
Msg("Service.Download()")
err = kw.Set(string(decoded))
if err != nil {
s.writeErrorResponse(w, err.Error(), http.StatusBadRequest)
return
}
// initialize storage
err = store.Init()
if err != nil {
s.writeErrorResponse(w, err.Error(), http.StatusInternalServerError)
return
}
err = store.SetKWArgs(kw)
if err != nil {
s.writeErrorResponse(w, err.Error(), http.StatusInternalServerError)
return
}
// prepare profiles
errs = s.LoadProfiles(profileIDs, store, errs)
if len(errs) > 0 {