Skip to content

Commit

Permalink
adds logic to upload compressed files with right mime type and right …
Browse files Browse the repository at this point in the history
…content encoding
  • Loading branch information
sprockow committed Jan 23, 2022
1 parent ee2495d commit d4b72f8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ Headers may be specified globally for all files in the bucket by adding a `name`

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'.

'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.

---

**redirectAllRequestsTo**
Expand Down
22 changes: 21 additions & 1 deletion lib/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function uploadFile(aws, bucketName, filePath, fileKey, headers, sse) {
Bucket: bucketName,
Key: fileKey,
Body: fileBuffer,
ContentType: mime.getType(filePath)
...getMimeTypeAndContentEncoding(filePath)
};

if (sse) {
Expand Down Expand Up @@ -180,4 +180,24 @@ function groupFilesByOrder(files, orderSpec) {
return [unmatchedFiles].concat(matchedFiles);
}

const ContentEncodingMap = {
gz: 'gzip',
br: 'br'
};

function getMimeTypeAndContentEncoding(filePath) {
const match = /(.+\..+)\.(gz|br)$/.exec(filePath);

if (match) {
const [fullMatch, strippedFilePath, encodingFileEnding] = match;

return {
ContentType: mime.getType(strippedFilePath),
ContentEncoding: ContentEncodingMap[encodingFileEnding]
};
}

return { ContentType: mime.getType(filePath) };
}

module.exports = uploadDirectory;

0 comments on commit d4b72f8

Please sign in to comment.