Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d4b72f8

Browse files
committedJan 23, 2022
adds logic to upload compressed files with right mime type and right content encoding
1 parent ee2495d commit d4b72f8

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed
 

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ Headers may be specified globally for all files in the bucket by adding a `name`
211211

212212
Headers with more specificity will take precedence over more general ones. For instance, if 'Cache-Control' was set to 'max-age=100' in `ALL_OBJECTS` and to 'max-age=500' in `my/folder/`, the files in `my/folder/` would get a header of 'Cache-Control: max-age=500'.
213213

214+
'Content-Type' is autmomatically inferred from the file extension and added to the header collection. If the file is compressed and has the additional extension `.gz` or `br`, a 'Content-Encoding' header will be added as well to describe the object as gzip or brotli encoded.
215+
214216
---
215217

216218
**redirectAllRequestsTo**

‎lib/upload.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function uploadFile(aws, bucketName, filePath, fileKey, headers, sse) {
6262
Bucket: bucketName,
6363
Key: fileKey,
6464
Body: fileBuffer,
65-
ContentType: mime.getType(filePath)
65+
...getMimeTypeAndContentEncoding(filePath)
6666
};
6767

6868
if (sse) {
@@ -180,4 +180,24 @@ function groupFilesByOrder(files, orderSpec) {
180180
return [unmatchedFiles].concat(matchedFiles);
181181
}
182182

183+
const ContentEncodingMap = {
184+
gz: 'gzip',
185+
br: 'br'
186+
};
187+
188+
function getMimeTypeAndContentEncoding(filePath) {
189+
const match = /(.+\..+)\.(gz|br)$/.exec(filePath);
190+
191+
if (match) {
192+
const [fullMatch, strippedFilePath, encodingFileEnding] = match;
193+
194+
return {
195+
ContentType: mime.getType(strippedFilePath),
196+
ContentEncoding: ContentEncodingMap[encodingFileEnding]
197+
};
198+
}
199+
200+
return { ContentType: mime.getType(filePath) };
201+
}
202+
183203
module.exports = uploadDirectory;

0 commit comments

Comments
 (0)
Please sign in to comment.