Skip to content

Commit

Permalink
archiver: allow to override the archive type via query parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
glpatcern committed Jul 28, 2023
1 parent 2795321 commit 40d26e8
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions internal/http/services/archiver/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"errors"
"fmt"
"net/http"
"path/filepath"
"regexp"
"time"

Expand Down Expand Up @@ -102,7 +103,7 @@ func New(ctx context.Context, conf map[string]interface{}) (global.Service, erro

func (c *Config) ApplyDefaults() {
if c.Prefix == "" {
c.Prefix = "download_archive"
c.Prefix = "archiver"
}

if c.Name == "" {
Expand Down Expand Up @@ -229,13 +230,27 @@ func (s *svc) Handler() http.Handler {
return
}

userAgent := ua.Parse(r.Header.Get("User-Agent"))
archTypeParams, ok := v["arch_type"] // optional, either "tar" or "zip"
var archType string
if ok {
archType = archTypeParams[0]
}
if !ok || archType != "tar" && archType != "zip" {
// in case of missing or bogus arch_type, detect it via user-agent
userAgent := ua.Parse(r.Header.Get("User-Agent"))
if userAgent.OS == ua.Windows {
archType = "zip"
} else {
archType = "tar"
}
}

archName := s.config.Name
if userAgent.OS == ua.Windows {
archName += ".zip"
var archName string
if len(files) == 1 {
archName = filepath.Base(files[0]) + "." + archType
} else {
archName += ".tar"
// TODO(lopresti) we may want to generate a meaningful name out of the list
archName = s.config.Name + "." + archType
}

log.Debug().Msg("Requested the following files/folders to archive: " + render.Render(files))
Expand All @@ -244,7 +259,7 @@ func (s *svc) Handler() http.Handler {
rw.Header().Set("Content-Transfer-Encoding", "binary")

// create the archive
if userAgent.OS == ua.Windows {
if archType == "zip" {
err = arch.CreateZip(ctx, rw)
} else {
err = arch.CreateTar(ctx, rw)
Expand Down

0 comments on commit 40d26e8

Please sign in to comment.