Skip to content

Commit

Permalink
feat(s3): guess content type for s3 write operations by default
Browse files Browse the repository at this point in the history
  • Loading branch information
bericp1 committed Apr 12, 2019
1 parent 010f4c1 commit 1c95023
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,15 @@ A disk that uses a remote AWS S3 bucket.

#### S3 Disk Options

| Name | Type | Description |
| -------------- | ------ | ------------------------------------------------------------------------------------------------------------------------- |
| `bucket` | string | Required; The S3 bucket to use. |
| `root` | string | Optional; The prefix to use for storing objects in the bucket. Defaults to the root |
| `pagingLimit` | number | Optional; Num of max results to fetch when paging through an S3 `listObjectsV2` call. Defaults to `1000`, the max. |
| `expires` | number | Optional; Number of seconds from time of upload to set `Expires` and `Cache-Control` for `putObject` calls. |
| `putParams` | object | Optional; Additional params to merge into all `putObject` calls |
| `clientConfig` | object | Optional; Options to pass to the `AWS.S3` client constructor. Can be used to pass credentials if not using env variables. |
| Name | Type | Description |
| ----------------- | ------ | ------------------------------------------------------------------------------------------------------------------------- |
| `bucket` | string | Required; The S3 bucket to use. |
| `root` | string | Optional; The prefix to use for storing objects in the bucket. Defaults to the root |
| `pagingLimit` | number | Optional; Num of max results to fetch when paging through an S3 `listObjectsV2` call. Defaults to `1000`, the max. |
| `expires` | number | Optional; Number of seconds from time of upload to set `Expires` and `Cache-Control` for `putObject` calls. |
| `putParams` | object | Optional; Additional params to merge into all `putObject` calls |
| `clientConfig` | object | Optional; Options to pass to the `AWS.S3` client constructor. Can be used to pass credentials if not using env variables. |
| `autoContentType` | bool | Optional; Use the `mime` library to guess `ContentType` for write operations. Defaults to `true` |

## API

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"dependencies": {
"aws-sdk": "^2.431.0",
"fs-extra": "^7.0.1",
"mime": "^2.4.2",
"ramda": "0.25.0",
"stream-to-array": "^2.3.0",
"tmp": "^0.1.0",
Expand All @@ -52,6 +53,7 @@
"@commitlint/config-conventional": "^7.5.0",
"@types/fs-extra": "^5.0.5",
"@types/jest": "^24.0.11",
"@types/mime": "^2.0.1",
"@types/ramda": "types/npm-ramda#dist",
"@types/stream-to-array": "^2.3.0",
"@types/tmp": "^0.1.0",
Expand Down
15 changes: 15 additions & 0 deletions src/drivers/s3/S3Disk.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as AWS from 'aws-sdk';
import { Readable, Writable } from 'stream';
import { getType as getMimeType } from 'mime';
import { Disk, DiskListingObject, DiskObjectType, streamToBuffer } from '../..';
import {
NotAFileError,
Expand Down Expand Up @@ -111,6 +112,15 @@ export class S3Disk extends Disk {
};
}

/**
* Generate a putObject params partial object containing ContentType if the mime type of the path is guessable.
* @param path
*/
public static getContentTypeParams(path: string): { ContentType?: string } {
const mimeType = getMimeType(path);
return mimeType ? { ContentType: mimeType } : {};
}

/**
* Get the putObject params for putting a file to a specific object path on the disk.
*
Expand All @@ -120,6 +130,7 @@ export class S3Disk extends Disk {
const {
putParams: extraPutParams = null,
expires: rawExpires = null,
autoContentType = true,
} = this.config;
const objectParams = this.getObjectParams(path);
const expires = rawExpires ? parseInt(rawExpires as string, 10) : null;
Expand All @@ -130,8 +141,12 @@ export class S3Disk extends Disk {
CacheControl: `max-age=${expires}`,
}
: {};
const contentTypeParams = autoContentType
? S3Disk.getContentTypeParams(path)
: {};
return {
...expiresParams,
...contentTypeParams,
...(extraPutParams || {}),
...objectParams,
};
Expand Down
5 changes: 5 additions & 0 deletions src/drivers/s3/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ export interface S3DiskConfig extends DiskConfig {
* If you're not using environment variables to authenticate the AWS client, you can provide credentials here.
*/
clientConfig?: {};

/**
* Should we use the `mime` library to automatically determine `ContentType` for write operations?
*/
autoContentType?: boolean;
}

/**
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,11 @@
dependencies:
"@types/jest-diff" "*"

"@types/mime@^2.0.1":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-2.0.1.tgz#dc488842312a7f075149312905b5e3c0b054c79d"
integrity sha512-FwI9gX75FgVBJ7ywgnq/P7tw+/o1GUbtP0KzbtusLigAOgIgNISRK0ZPl4qertvXSIE8YbsVJueQ90cDt9YYyw==

"@types/minimatch@*":
version "3.0.3"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"
Expand Down Expand Up @@ -4983,6 +4988,11 @@ mime@^2.0.3:
resolved "https://registry.yarnpkg.com/mime/-/mime-2.4.0.tgz#e051fd881358585f3279df333fe694da0bcffdd6"
integrity sha512-ikBcWwyqXQSHKtciCcctu9YfPbFYZ4+gbHEmE0Q8jzcTYQg5dHCr3g2wwAZjPoJfQVXZq6KXAjpXOTf5/cjT7w==

mime@^2.4.2:
version "2.4.2"
resolved "https://registry.yarnpkg.com/mime/-/mime-2.4.2.tgz#ce5229a5e99ffc313abac806b482c10e7ba6ac78"
integrity sha512-zJBfZDkwRu+j3Pdd2aHsR5GfH2jIWhmL1ZzBoc+X+3JEti2hbArWcyJ+1laC1D2/U/W1a/+Cegj0/OnEU2ybjg==

mimic-fn@^1.0.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-1.2.0.tgz#820c86a39334640e99516928bd03fca88057d022"
Expand Down

0 comments on commit 1c95023

Please sign in to comment.