Skip to content

Commit 9d09674

Browse files
authored
Add name to CompressFiles (#83)
* Add `name` to `CompressFiles` * Add back extension
1 parent 3a42de3 commit 9d09674

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

router/router_server_files.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ func postServerCompressFiles(c *gin.Context) {
429429
var data struct {
430430
RootPath string `json:"root"`
431431
Files []string `json:"files"`
432+
Name string `json:"name"`
432433
}
433434

434435
if err := c.BindJSON(&data); err != nil {
@@ -449,7 +450,7 @@ func postServerCompressFiles(c *gin.Context) {
449450
return
450451
}
451452

452-
f, err := s.Filesystem().CompressFiles(data.RootPath, data.Files)
453+
f, err := s.Filesystem().CompressFiles(data.RootPath, data.Name, data.Files)
453454
if err != nil {
454455
middleware.CaptureAndAbort(c, err)
455456
return

server/filesystem/compress.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
// All paths are relative to the dir that is passed in as the first argument,
2929
// and the compressed file will be placed at that location named
3030
// `archive-{date}.tar.gz`.
31-
func (fs *Filesystem) CompressFiles(dir string, paths []string) (ufs.FileInfo, error) {
31+
func (fs *Filesystem) CompressFiles(dir string, name string, paths []string) (ufs.FileInfo, error) {
3232
var validPaths []string
3333
for _, file := range paths {
3434
if err := fs.IsIgnored(path.Join(dir, file)); err == nil {
@@ -44,8 +44,15 @@ func (fs *Filesystem) CompressFiles(dir string, paths []string) (ufs.FileInfo, e
4444
a := &Archive{Filesystem: fs, BaseDirectory: dir, Files: validPaths}
4545
d := path.Join(
4646
dir,
47-
fmt.Sprintf("archive-%s.tar.gz", strings.ReplaceAll(time.Now().Format(time.RFC3339), ":", "")),
48-
)
47+
fmt.Sprintf("%s.%s",
48+
func() string {
49+
if name != "" {
50+
return name
51+
}
52+
return fmt.Sprintf("archive-%s", strings.ReplaceAll(time.Now().Format(time.RFC3339), ":", ""))
53+
}(),
54+
".tar.gz",
55+
))
4956
f, err := fs.unixFS.OpenFile(d, ufs.O_WRONLY|ufs.O_CREATE, 0o644)
5057
if err != nil {
5158
return nil, err

0 commit comments

Comments
 (0)