Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds logic to upload compressed files with right mime type and right content-encoding #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;