refactor: changed how file/archives are downloaded and saved

This commit is contained in:
David Allen 2025-08-28 18:20:40 -06:00
parent 98f9acad5d
commit 2536848541
Signed by: towk
GPG key ID: 0430CDBE22619155
2 changed files with 58 additions and 27 deletions

View file

@ -8,7 +8,6 @@ import (
"os"
"path/filepath"
"strings"
"time"
"git.towk2.me/towk/makeshift/internal/archive"
makeshift "git.towk2.me/towk/makeshift/pkg"
@ -54,7 +53,7 @@ func (s *Service) Download() http.HandlerFunc {
if fileInfo, err = os.Stat(path); err == nil {
if fileInfo.IsDir() {
// get the final archive path
archivePath := fmt.Sprintf("%d.tar.gz", time.Now().Unix())
archivePath := fmt.Sprintf("%s.tar.gz", path)
log.Debug().
Str("archive_path", archivePath).
@ -98,6 +97,8 @@ func (s *Service) Download() http.HandlerFunc {
return
}
// send the archive back as response
w.Header().Add("FILETYPE", "archive")
w.Write(contents)
// clean up the temporary archive
@ -128,7 +129,6 @@ func (s *Service) Download() http.HandlerFunc {
errs = []error{}
}
if len(hooks) > 0 {
// run pre-hooks to modify the contents of the file before archiving
log.Debug().Int("hook_count", len(hooks)).Msg("running hooks")
for _, hook := range hooks {
@ -143,7 +143,10 @@ func (s *Service) Download() http.HandlerFunc {
}).Send()
err = hook.Run()
if err != nil {
log.Error().Err(err).Str("plugin", hook.Plugin.Name()).Msg("failed to run plugin")
log.Error().
Err(err).
Str("plugin", hook.Plugin.Name()).
Msg("failed to run plugin")
continue
}
}
@ -158,8 +161,13 @@ func (s *Service) Download() http.HandlerFunc {
s.writeErrorResponse(w, fmt.Sprintf("failed to get data from hook: %v", err), http.StatusInternalServerError)
return
}
// send processed (with plugins) file back as response
w.Header().Add("FILETYPE", "file")
w.Write(data.([]byte))
} else {
// send non-processed file back as response
w.Header().Add("FILETYPE", "file")
w.Write(contents)
}
@ -223,7 +231,7 @@ func (s *Service) GetStatus(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(map[string]any{
"code": http.StatusOK,
"message": "Configurator is healthy",
"message": "The makeshift server is healthy",
})
if err != nil {
fmt.Printf("failed to encode JSON response body: %v\n", err)
@ -274,17 +282,18 @@ func (s *Service) loadPlugins(pluginNames []string, store storage.KVStore, args
err error
)
if i > DEFAULT_PLUGINS_MAX_COUNT {
log.Warn().Msg("max plugins count reached...stopping")
log.Warn().Msg("max plugins count reached or exceeded...stopping")
return hooks, errs
}
if pluginName == "" {
log.Warn().Msg("plugin name is empty...skipping")
log.Warn().Msgf("no plugin name found with index %d...skipping", i)
continue
}
log.Debug().
Str("name", pluginName).
Str("path", pluginPath).
Msg("load plugin")
// load the plugin from disk
plugin, err = LoadPluginFromFile(pluginPath)
if err != nil {