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 (
"os"
"path"
"path/filepath"
"regexp"
"time"
pb "gitlab.com/gitlab-org/gitaly-proto/go"
......@@ -181,17 +182,26 @@ func finalizeCachedArchive(tempFile *os.File, archivePath string) error {
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) {
var format pb.GetArchiveRequest_Format
switch basename {
case "archive.zip":
switch {
case (basename == "archive"):
format = pb.GetArchiveRequest_TAR_GZ
case patternZip.MatchString(basename):
format = pb.GetArchiveRequest_ZIP
case "archive.tar":
case patternTar.MatchString(basename):
format = pb.GetArchiveRequest_TAR
case "archive", "archive.tar.gz", "archive.tgz", "archive.gz":
case patternTarGz.MatchString(basename):
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
default:
return format, false
......
......@@ -14,17 +14,17 @@ func TestParseBasename(t *testing.T) {
in string
out pb.GetArchiveRequest_Format
}{
{"", pb.GetArchiveRequest_TAR_GZ},
{".tar.gz", pb.GetArchiveRequest_TAR_GZ},
{".tgz", pb.GetArchiveRequest_TAR_GZ},
{".gz", pb.GetArchiveRequest_TAR_GZ},
{".tar.bz2", pb.GetArchiveRequest_TAR_BZ2},
{".tbz", pb.GetArchiveRequest_TAR_BZ2},
{".tbz2", pb.GetArchiveRequest_TAR_BZ2},
{".tb2", pb.GetArchiveRequest_TAR_BZ2},
{".bz2", pb.GetArchiveRequest_TAR_BZ2},
{"archive", pb.GetArchiveRequest_TAR_GZ},
{"master.tar.gz", pb.GetArchiveRequest_TAR_GZ},
{"foo-master.tgz", pb.GetArchiveRequest_TAR_GZ},
{"foo-v1.2.1.gz", pb.GetArchiveRequest_TAR_GZ},
{"foo.tar.bz2", pb.GetArchiveRequest_TAR_BZ2},
{"archive.tbz", pb.GetArchiveRequest_TAR_BZ2},
{"archive.tbz2", pb.GetArchiveRequest_TAR_BZ2},
{"archive.tb2", pb.GetArchiveRequest_TAR_BZ2},
{"archive.bz2", pb.GetArchiveRequest_TAR_BZ2},
} {
basename := "archive" + testCase.in
basename := testCase.in
out, ok := parseBasename(basename)
if !ok {
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