storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/config/compress/compress.go (about) 1 /* 2 * MinIO Cloud Storage, (C) 2019 MinIO, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package compress 18 19 import ( 20 "fmt" 21 "strings" 22 23 "storj.io/minio/cmd/config" 24 "storj.io/minio/pkg/env" 25 ) 26 27 // Config represents the compression settings. 28 type Config struct { 29 Enabled bool `json:"enabled"` 30 AllowEncrypted bool `json:"allow_encryption"` 31 Extensions []string `json:"extensions"` 32 MimeTypes []string `json:"mime-types"` 33 } 34 35 // Compression environment variables 36 const ( 37 Extensions = "extensions" 38 AllowEncrypted = "allow_encryption" 39 MimeTypes = "mime_types" 40 41 EnvCompressState = "MINIO_COMPRESS_ENABLE" 42 EnvCompressAllowEncryption = "MINIO_COMPRESS_ALLOW_ENCRYPTION" 43 EnvCompressExtensions = "MINIO_COMPRESS_EXTENSIONS" 44 EnvCompressMimeTypes = "MINIO_COMPRESS_MIME_TYPES" 45 46 // Include-list for compression. 47 DefaultExtensions = ".txt,.log,.csv,.json,.tar,.xml,.bin" 48 DefaultMimeTypes = "text/*,application/json,application/xml,binary/octet-stream" 49 ) 50 51 // DefaultKVS - default KV config for compression settings 52 var ( 53 DefaultKVS = config.KVS{ 54 config.KV{ 55 Key: config.Enable, 56 Value: config.EnableOff, 57 }, 58 config.KV{ 59 Key: AllowEncrypted, 60 Value: config.EnableOff, 61 }, 62 config.KV{ 63 Key: Extensions, 64 Value: DefaultExtensions, 65 }, 66 config.KV{ 67 Key: MimeTypes, 68 Value: DefaultMimeTypes, 69 }, 70 } 71 ) 72 73 // Parses the given compression exclude list `extensions` or `content-types`. 74 func parseCompressIncludes(include string) ([]string, error) { 75 includes := strings.Split(include, config.ValueSeparator) 76 for _, e := range includes { 77 if len(e) == 0 { 78 return nil, config.ErrInvalidCompressionIncludesValue(nil).Msg("extension/mime-type cannot be empty") 79 } 80 if e == "/" { 81 return nil, config.ErrInvalidCompressionIncludesValue(nil).Msg("extension/mime-type cannot be '/'") 82 } 83 } 84 return includes, nil 85 } 86 87 // LookupConfig - lookup compression config. 88 func LookupConfig(kvs config.KVS) (Config, error) { 89 var err error 90 cfg := Config{} 91 if err = config.CheckValidKeys(config.CompressionSubSys, kvs, DefaultKVS); err != nil { 92 return cfg, err 93 } 94 95 compress := env.Get(EnvCompress, "") 96 if compress == "" { 97 compress = env.Get(EnvCompressState, kvs.Get(config.Enable)) 98 } 99 cfg.Enabled, err = config.ParseBool(compress) 100 if err != nil { 101 // Parsing failures happen due to empty KVS, ignore it. 102 if kvs.Empty() { 103 return cfg, nil 104 } 105 return cfg, err 106 } 107 if !cfg.Enabled { 108 return cfg, nil 109 } 110 111 allowEnc := env.Get(EnvCompressAllowEncryption, kvs.Get(AllowEncrypted)) 112 cfg.AllowEncrypted, err = config.ParseBool(allowEnc) 113 if err != nil { 114 return cfg, err 115 } 116 117 compressExtensions := env.Get(EnvCompressExtensions, kvs.Get(Extensions)) 118 compressMimeTypes := env.Get(EnvCompressMimeTypes, kvs.Get(MimeTypes)) 119 compressMimeTypesLegacy := env.Get(EnvCompressMimeTypesLegacy, kvs.Get(MimeTypes)) 120 if compressExtensions != "" || compressMimeTypes != "" || compressMimeTypesLegacy != "" { 121 if compressExtensions != "" { 122 extensions, err := parseCompressIncludes(compressExtensions) 123 if err != nil { 124 return cfg, fmt.Errorf("%s: Invalid MINIO_COMPRESS_EXTENSIONS value (`%s`)", err, extensions) 125 } 126 cfg.Extensions = extensions 127 } 128 if compressMimeTypes != "" { 129 mimeTypes, err := parseCompressIncludes(compressMimeTypes) 130 if err != nil { 131 return cfg, fmt.Errorf("%s: Invalid MINIO_COMPRESS_MIME_TYPES value (`%s`)", err, mimeTypes) 132 } 133 cfg.MimeTypes = mimeTypes 134 } 135 if compressMimeTypesLegacy != "" { 136 mimeTypes, err := parseCompressIncludes(compressMimeTypesLegacy) 137 if err != nil { 138 return cfg, fmt.Errorf("%s: Invalid MINIO_COMPRESS_MIME_TYPES value (`%s`)", err, mimeTypes) 139 } 140 cfg.MimeTypes = mimeTypes 141 } 142 } 143 144 return cfg, nil 145 }