From 83a898276a6beac89bc7238656dca3c65675a3c5 Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Mon, 30 Nov 2020 14:51:49 +0100 Subject: [PATCH] compression: let algorithms register a MIME type Signed-off-by: Giuseppe Scrivano --- manifest/common.go | 2 +- pkg/compression/compression.go | 8 ++++---- pkg/compression/internal/types.go | 9 ++++++++- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/manifest/common.go b/manifest/common.go index 3ece948a05..6008812ccd 100644 --- a/manifest/common.go +++ b/manifest/common.go @@ -68,7 +68,7 @@ func compressionVariantMIMEType(variantTable []compressionMIMETypeSet, mimeType if mt == mimeType { // Found the variant name := mtsUncompressed if algorithm != nil { - name = algorithm.Name() + name = algorithm.MIME() } if res, ok := variants[name]; ok { if res != mtsUnsupportedMIMEType { diff --git a/pkg/compression/compression.go b/pkg/compression/compression.go index 12fc204a96..8672b243dd 100644 --- a/pkg/compression/compression.go +++ b/pkg/compression/compression.go @@ -20,13 +20,13 @@ type Algorithm = types.Algorithm var ( // Gzip compression. - Gzip = internal.NewAlgorithm("gzip", []byte{0x1F, 0x8B, 0x08}, GzipDecompressor, gzipCompressor) + Gzip = internal.NewAlgorithm("gzip", "gzip", []byte{0x1F, 0x8B, 0x08}, GzipDecompressor, gzipCompressor) // Bzip2 compression. - Bzip2 = internal.NewAlgorithm("bzip2", []byte{0x42, 0x5A, 0x68}, Bzip2Decompressor, bzip2Compressor) + Bzip2 = internal.NewAlgorithm("bzip2", "bzip2", []byte{0x42, 0x5A, 0x68}, Bzip2Decompressor, bzip2Compressor) // Xz compression. - Xz = internal.NewAlgorithm("Xz", []byte{0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00}, XzDecompressor, xzCompressor) + Xz = internal.NewAlgorithm("Xz", "xz", []byte{0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00}, XzDecompressor, xzCompressor) // Zstd compression. - Zstd = internal.NewAlgorithm("zstd", []byte{0x28, 0xb5, 0x2f, 0xfd}, ZstdDecompressor, zstdCompressor) + Zstd = internal.NewAlgorithm("zstd", "zstd", []byte{0x28, 0xb5, 0x2f, 0xfd}, ZstdDecompressor, zstdCompressor) compressionAlgorithms = map[string]Algorithm{ Gzip.Name(): Gzip, diff --git a/pkg/compression/internal/types.go b/pkg/compression/internal/types.go index d2b023c9b4..8b16e527a5 100644 --- a/pkg/compression/internal/types.go +++ b/pkg/compression/internal/types.go @@ -13,6 +13,7 @@ type DecompressorFunc func(io.Reader) (io.ReadCloser, error) // Algorithm is a compression algorithm that can be used for CompressStream. type Algorithm struct { name string + mime string prefix []byte decompressor DecompressorFunc compressor CompressorFunc @@ -21,9 +22,10 @@ type Algorithm struct { // NewAlgorithm creates an Algorithm instance. // This function exists so that Algorithm instances can only be created by code that // is allowed to import this internal subpackage. -func NewAlgorithm(name string, prefix []byte, decompressor DecompressorFunc, compressor CompressorFunc) Algorithm { +func NewAlgorithm(name, mime string, prefix []byte, decompressor DecompressorFunc, compressor CompressorFunc) Algorithm { return Algorithm{ name: name, + mime: mime, prefix: prefix, decompressor: decompressor, compressor: compressor, @@ -35,6 +37,11 @@ func (c Algorithm) Name() string { return c.name } +// Name returns the MIME type to use for the compression algorithm. +func (c Algorithm) MIME() string { + return c.mime +} + // AlgorithmCompressor returns the compressor field of algo. // This is a function instead of a public method so that it is only callable from by code // that is allowed to import this internal subpackage.