Commit 1a47f775 authored by James Ramsay's avatar James Ramsay

Detect archive format from the file extension

The entire file name including the extension is used to detect the
format of the archive, like `archive.tar.gz` but this doesn't permit
the addition of more flexible routes that accept names like
`project-name-1.2.tar.gz`.

Only the extension of the file should be used to detect the archive
format so that more flexible archive routes are possible.
parent 2292c123
...@@ -12,6 +12,7 @@ import ( ...@@ -12,6 +12,7 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"regexp"
"time" "time"
pb "gitlab.com/gitlab-org/gitaly-proto/go" pb "gitlab.com/gitlab-org/gitaly-proto/go"
...@@ -181,17 +182,26 @@ func finalizeCachedArchive(tempFile *os.File, archivePath string) error { ...@@ -181,17 +182,26 @@ func finalizeCachedArchive(tempFile *os.File, archivePath string) error {
return nil return nil
} }
var (
patternZip = regexp.MustCompile(`\.zip$`)
patternTar = regexp.MustCompile(`\.tar$`)
patternTarGz = regexp.MustCompile(`\.(tar\.gz|tgz|gz)$`)
patternTarBz2 = regexp.MustCompile(`\.(tar\.bz2|tbz|tbz2|tb2|bz2)$`)
)
func parseBasename(basename string) (pb.GetArchiveRequest_Format, bool) { func parseBasename(basename string) (pb.GetArchiveRequest_Format, bool) {
var format pb.GetArchiveRequest_Format var format pb.GetArchiveRequest_Format
switch basename { switch {
case "archive.zip": case (basename == "archive"):
format = pb.GetArchiveRequest_TAR_GZ
case patternZip.MatchString(basename):
format = pb.GetArchiveRequest_ZIP format = pb.GetArchiveRequest_ZIP
case "archive.tar": case patternTar.MatchString(basename):
format = pb.GetArchiveRequest_TAR format = pb.GetArchiveRequest_TAR
case "archive", "archive.tar.gz", "archive.tgz", "archive.gz": case patternTarGz.MatchString(basename):
format = pb.GetArchiveRequest_TAR_GZ format = pb.GetArchiveRequest_TAR_GZ
case "archive.tar.bz2", "archive.tbz", "archive.tbz2", "archive.tb2", "archive.bz2": case patternTarBz2.MatchString(basename):
format = pb.GetArchiveRequest_TAR_BZ2 format = pb.GetArchiveRequest_TAR_BZ2
default: default:
return format, false return format, false
......
...@@ -14,17 +14,17 @@ func TestParseBasename(t *testing.T) { ...@@ -14,17 +14,17 @@ func TestParseBasename(t *testing.T) {
in string in string
out pb.GetArchiveRequest_Format out pb.GetArchiveRequest_Format
}{ }{
{"", pb.GetArchiveRequest_TAR_GZ}, {"archive", pb.GetArchiveRequest_TAR_GZ},
{".tar.gz", pb.GetArchiveRequest_TAR_GZ}, {"master.tar.gz", pb.GetArchiveRequest_TAR_GZ},
{".tgz", pb.GetArchiveRequest_TAR_GZ}, {"foo-master.tgz", pb.GetArchiveRequest_TAR_GZ},
{".gz", pb.GetArchiveRequest_TAR_GZ}, {"foo-v1.2.1.gz", pb.GetArchiveRequest_TAR_GZ},
{".tar.bz2", pb.GetArchiveRequest_TAR_BZ2}, {"foo.tar.bz2", pb.GetArchiveRequest_TAR_BZ2},
{".tbz", pb.GetArchiveRequest_TAR_BZ2}, {"archive.tbz", pb.GetArchiveRequest_TAR_BZ2},
{".tbz2", pb.GetArchiveRequest_TAR_BZ2}, {"archive.tbz2", pb.GetArchiveRequest_TAR_BZ2},
{".tb2", pb.GetArchiveRequest_TAR_BZ2}, {"archive.tb2", pb.GetArchiveRequest_TAR_BZ2},
{".bz2", pb.GetArchiveRequest_TAR_BZ2}, {"archive.bz2", pb.GetArchiveRequest_TAR_BZ2},
} { } {
basename := "archive" + testCase.in basename := testCase.in
out, ok := parseBasename(basename) out, ok := parseBasename(basename)
if !ok { if !ok {
t.Fatalf("parseBasename did not recognize %q", basename) t.Fatalf("parseBasename did not recognize %q", basename)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment