refactor: minor changes and fixes
This commit is contained in:
parent
72c52fbac6
commit
c1c5ec1625
3 changed files with 39 additions and 14 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -20,11 +20,15 @@ func (ms *MemoryStorage) Cleanup() error {
|
|||
}
|
||||
|
||||
func (ms *MemoryStorage) SetKWArgs(kw *kwargs.KWArgs) error {
|
||||
return ms.Set(kwargs.RESERVED_KEY, kw)
|
||||
ms.Data[kwargs.RESERVED_KEY] = kw
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ms *MemoryStorage) GetKWArgs() (*kwargs.KWArgs, error) {
|
||||
kw, err := ms.Get(kwargs.RESERVED_KEY)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return kw.(*kwargs.KWArgs), err
|
||||
}
|
||||
|
||||
|
|
@ -37,7 +41,7 @@ func (ms *MemoryStorage) Get(k string) (any, error) {
|
|||
}
|
||||
|
||||
func (ms *MemoryStorage) Set(k string, v any) error {
|
||||
if k == "kwargs" {
|
||||
if k == kwargs.RESERVED_KEY {
|
||||
return fmt.Errorf("cannot set reserved key '%s' (use SetKWArgs() instead)", k)
|
||||
}
|
||||
ms.Data[k] = v
|
||||
|
|
|
|||
1
pkg/storage/memory_test.go
Normal file
1
pkg/storage/memory_test.go
Normal file
|
|
@ -0,0 +1 @@
|
|||
package storage
|
||||
Loading…
Add table
Add a link
Reference in a new issue