Better archive detection logic; try to use reflection as last ditch effort if unmatched
closes pterodactyl/panel#4101
This commit is contained in:
		
							parent
							
								
									203a2091a0
								
							
						
					
					
						commit
						b1be2081eb
					
				|  | @ -5,9 +5,12 @@ import ( | ||||||
| 	"archive/zip" | 	"archive/zip" | ||||||
| 	"compress/gzip" | 	"compress/gzip" | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	gzip2 "github.com/klauspost/compress/gzip" | ||||||
|  | 	zip2 "github.com/klauspost/compress/zip" | ||||||
| 	"os" | 	"os" | ||||||
| 	"path" | 	"path" | ||||||
| 	"path/filepath" | 	"path/filepath" | ||||||
|  | 	"reflect" | ||||||
| 	"strings" | 	"strings" | ||||||
| 	"sync/atomic" | 	"sync/atomic" | ||||||
| 	"time" | 	"time" | ||||||
|  | @ -172,13 +175,26 @@ func ExtractNameFromArchive(f archiver.File) string { | ||||||
| 		return f.Name() | 		return f.Name() | ||||||
| 	} | 	} | ||||||
| 	switch s := sys.(type) { | 	switch s := sys.(type) { | ||||||
|  | 	case *zip.FileHeader: | ||||||
|  | 		return s.Name | ||||||
|  | 	case *zip2.FileHeader: | ||||||
|  | 		return s.Name | ||||||
| 	case *tar.Header: | 	case *tar.Header: | ||||||
| 		return s.Name | 		return s.Name | ||||||
| 	case *gzip.Header: | 	case *gzip.Header: | ||||||
| 		return s.Name | 		return s.Name | ||||||
| 	case *zip.FileHeader: | 	case *gzip2.Header: | ||||||
| 		return s.Name | 		return s.Name | ||||||
| 	default: | 	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() | 		return f.Name() | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user