From b1be2081eb0599df432c58329a4446aa28bb4f96 Mon Sep 17 00:00:00 2001 From: DaneEveritt Date: Mon, 30 May 2022 18:42:31 -0400 Subject: [PATCH] Better archive detection logic; try to use reflection as last ditch effort if unmatched closes pterodactyl/panel#4101 --- server/filesystem/compress.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/server/filesystem/compress.go b/server/filesystem/compress.go index cabbfdd..8ae36af 100644 --- a/server/filesystem/compress.go +++ b/server/filesystem/compress.go @@ -5,9 +5,12 @@ import ( "archive/zip" "compress/gzip" "fmt" + gzip2 "github.com/klauspost/compress/gzip" + zip2 "github.com/klauspost/compress/zip" "os" "path" "path/filepath" + "reflect" "strings" "sync/atomic" "time" @@ -172,13 +175,26 @@ func ExtractNameFromArchive(f archiver.File) string { return f.Name() } switch s := sys.(type) { + case *zip.FileHeader: + return s.Name + case *zip2.FileHeader: + return s.Name case *tar.Header: return s.Name case *gzip.Header: return s.Name - case *zip.FileHeader: + case *gzip2.Header: return s.Name default: + // At this point we cannot figure out what type of archive this might be so + // just try to find the name field in the struct. If it is found return it. + field := reflect.Indirect(reflect.ValueOf(sys)).FieldByName("Name") + if field.IsValid() { + return field.String() + } + // Fallback to the basename of the file at this point. There is nothing we can really + // do to try and figure out what the underlying directory of the file is supposed to + // be since it didn't implement a name field. return f.Name() } }