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

Sync with upstream #3

Open
wants to merge 83 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
f33936a
Enable 200 or 201 for signing success.
absoludity Aug 7, 2017
05b0c9a
Merge pull request #135 from absoludity/allow-201-on-signing
seanadkinson Aug 11, 2017
38d4754
Add optional paramter to s3 router function to use middleware on sign…
bkneis Aug 13, 2017
a12060c
Merge pull request #137 from bkneis/feature/add-support-for-middleware
seanadkinson Aug 17, 2017
f3e803a
Version 4.3.0
Aug 17, 2017
f5fd0ce
Add Param getS3 to allow custom S3 configuration and mocks
techwes Aug 25, 2017
d284f8d
Added s3path prop. Receive the path where the file should be uploaded.
WilliamGrah Aug 28, 2017
a30160e
Merge pull request #139 from techwes/pass-s3
seanadkinson Aug 29, 2017
cb7efb4
Merge pull request #140 from WilliamGrah/s3path
seanadkinson Aug 29, 2017
eee7898
4.4.0
Aug 29, 2017
1f86f08
Removed React and ReactDOM peer dependencies.
iansu Aug 29, 2017
2fc6603
Merge pull request #141 from iansu/master
seanadkinson Aug 30, 2017
c435953
Updated changelog
Aug 30, 2017
192ab4c
4.5.0
Aug 30, 2017
0d6cdc3
add micro server example
Sep 11, 2017
48c375b
Merge pull request #143 from babenzele/patch-1
seanadkinson Sep 17, 2017
f3c9ab1
addded `inputRef` prop which allows setting a `ref` prop on the `<inp…
Nov 16, 2017
6f056af
Missing semicolon, mixup with `innerRef`
Nov 16, 2017
ec62b6b
PR review change requests
Nov 16, 2017
b755ae7
Merge pull request #153 from denizs/feat-input-ref
seanadkinson Nov 16, 2017
4300efc
Version 4.5.1
Nov 19, 2017
19144e4
Included auto upload boolean property
LKozakewycz Dec 4, 2017
e34ccef
Merge pull request #155 from LKozakewycz/auto-upload-prop
seanadkinson Dec 9, 2017
4d007ef
Updating to v4.6.0
Dec 9, 2017
e088789
Updates to README for recent props
Dec 9, 2017
b99f2a8
Move uniquePrefix to be AFTER the s3path
jharris-code Dec 24, 2017
0c39e88
Merge pull request #156 from sagelytics/master
seanadkinson Dec 28, 2017
8c4f8c2
Version 4.6.1
Dec 29, 2017
542f152
Version 4.6.2: Fix undefined in paths when not providing s3path prop
Jan 3, 2018
e0c52a4
Adds onSignedUrl handler
Mar 12, 2018
3b9b135
Added onSignedUrl to default props in README
Mar 12, 2018
382c5e0
Make logging in default onSignedUrl handler conform to style
Mar 12, 2018
cc58e17
Merge pull request #170 from charltoons/master
seanadkinson Mar 14, 2018
b345b25
Changelog
Mar 14, 2018
dd54a38
4.7.0
Mar 14, 2018
5871022
Add default implementation of onSignedUrl
Mar 19, 2018
e3548e4
4.7.1
Mar 19, 2018
af848f9
Enable 201 response from cloud provider
absoludity Mar 25, 2018
64dea83
Merge pull request #174 from absoludity/enable-201-PUT
seanadkinson Mar 26, 2018
ce30ae4
Update changelog for 4.8.0
Mar 26, 2018
22e6c19
4.8.0
Mar 26, 2018
643e80e
In case of multiple file upload how to know that which percentage is …
Sep 4, 2018
e372cf5
Merge pull request #187 from naxir/patch-1
seanadkinson Sep 10, 2018
9d86be1
add Typescript definitions
jakemmarsh Jan 18, 2019
e2b15e2
update
jakemmarsh Jan 18, 2019
56794f7
inputRef should support React.createRef #176
mihaimartalogu Feb 7, 2019
18d81e7
Find file mime type if file.type is empty
May 13, 2019
ae0c7ea
mime-types for all files
May 14, 2019
b2c9f7e
Add request context to onError callback
techwes Oct 8, 2019
44f5b6a
Add option to change when the signed upload URL expires
techwes Oct 8, 2019
39e1ea9
Merge pull request #207 from MayflowerVenues/additional-error-info
seanadkinson Nov 19, 2019
e545b13
Merge pull request #208 from MayflowerVenues/router-expiration
seanadkinson Nov 19, 2019
d3914f4
4.9 changelog
Nov 19, 2019
a756c78
4.9.0
Nov 19, 2019
471ca1c
Merge pull request #199 from jakemmarsh/patch-1
seanadkinson Jan 17, 2020
719d844
4.9.1
Jan 17, 2020
9a9232f
Update changelog
Jan 17, 2020
de05934
chore: update onFinish with better types
jarretmoses Jan 22, 2020
47ff275
Merge pull request #211 from jarretmoses/chore-more-explicit-onfinish…
seanadkinson Jan 22, 2020
692f3f6
Update changelog
Jan 22, 2020
ef4e823
4.9.2
Jan 22, 2020
b51a842
Use S3Response type for getSignedUrl
schpet Feb 22, 2020
e75d53c
template logs
mortezashojaei Feb 28, 2020
e39de72
Update index.d.ts
mrjones2014 May 8, 2020
be59d69
Merge pull request #1 from mrjones2014/mrjones2014-patch-1
mrjones2014 May 8, 2020
ecdb250
Merge pull request #216 from mrjones2014/master
seanadkinson May 19, 2020
efa19fb
4.9.3
May 19, 2020
eefdc07
Changelot for 4.9.3
May 19, 2020
a085c67
Add missing S3Response import to type definition
dennisv Jun 1, 2020
1b64eac
Custom headers fixes
dobesv Oct 27, 2020
3b89d97
TypeScript tweaks and fixes
dobesv Jan 30, 2020
ff620e6
Merge pull request #200 from mihaimartalogu/master
seanadkinson Nov 4, 2020
3d1a40d
Merge pull request #218 from wecreatepixels/add-missing-import
seanadkinson Nov 4, 2020
e23f8c8
Merge branch 'master' into ts-fixup
seanadkinson Nov 4, 2020
7bf1a65
Merge pull request #212 from dobesv/ts-fixup
seanadkinson Nov 4, 2020
a9e6997
Bump lodash from 4.17.4 to 4.17.20
dependabot[bot] Nov 4, 2020
ebf61ea
Merge pull request #225 from odysseyscience/dependabot/npm_and_yarn/l…
seanadkinson Nov 4, 2020
26b0c95
Merge pull request #213 from schpet/fix-typings
seanadkinson Nov 4, 2020
b2545c7
Merge pull request #224 from dobesv/content-type-fixup
seanadkinson Nov 4, 2020
d1a6ba8
Merge pull request #214 from mortezashojaei/patch-1
seanadkinson Nov 4, 2020
b1d156f
Merge branch 'master' into master
seanadkinson Nov 4, 2020
ac2780b
Merge pull request #203 from devstarkes/master
seanadkinson Nov 4, 2020
4faf574
Moving typescript to devDependencies, and aws-sdk to optionalDependen…
Nov 4, 2020
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
57 changes: 57 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,61 @@

##### 5.0.0

* Update several dependencies, moving some to `optional` and `dev`
* Update many typescript definitions
* Added `mime-types` fallback when browser doesn't provide `file.type` [#223]

##### 4.9.3

* Add missing typescript definitions [#216]

##### 4.9.2

* Added typescript definitions

##### 4.9.0

* Better error messaging [#207]
* Configurable Expires time [#208]

##### 4.8.0

* Allow 201 response code from PUT [#174]

##### 4.7.0

* Add an `onSignedUrl` lifecycle hook [#170]

##### 4.6.2

* Fix `undefined` in file path when not providing `s3path` prop [#160]

##### 4.6.1

* Fix `s3path` and `uniquePrefix` ordering when applied together [#156]

##### 4.6.0

* Add `autoUpload` prop, which can be set to `false` to disable automatic upload [#95] [#107] [#155]

##### 4.5.1

* Add `inputRef` prop [#153]

##### 4.5.0

* Removed `peerDependencies` on react and react-dom [#136]

##### 4.4.0

* Support `getS3` function in bundled router options [#139]
* Support `s3Path` string prefix prop [#140]

##### 4.3.0

* Support middleware in express module with optional second arg
* Allow 200 or 201 for success on sign request

##### 4.2.0

* Switch to `uuid` instead of `node-uuid` [#115]
Expand Down
61 changes: 58 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ var ReactS3Uploader = require('react-s3-uploader');
signingUrl="/s3/sign"
signingUrlMethod="GET"
accept="image/*"
s3path="/uploads/"
preprocess={this.onUploadStart}
onSignedUrl={this.onSignedUrl}
onProgress={this.onUploadProgress}
onError={this.onUploadError}
onFinish={this.onUploadFinish}
Expand All @@ -30,7 +32,10 @@ var ReactS3Uploader = require('react-s3-uploader');
uploadRequestHeaders={{ 'x-amz-acl': 'public-read' }} // this is the default
contentDisposition="auto"
scrubFilename={(filename) => filename.replace(/[^\w\d_\-.]+/ig, '')}
server="http://cross-origin-server.com" />
server="http://cross-origin-server.com"
inputRef={cmp => this.uploadInput = cmp}
autoUpload={true}
/>
```

The above example shows all supported `props`.
Expand Down Expand Up @@ -58,8 +63,7 @@ The resulting DOM is essentially:
The `preprocess(file, next)` prop provides an opportunity to do something before the file upload begins,
modify the file (scaling the image for example), or abort the upload by not calling `next(file)`.

When a file is chosen, it will immediately be uploaded to S3. You can listen for progress (and
create a status bar, for example) by providing an `onProgress` function to the component.
When a file is chosen, it will immediately be uploaded to S3 (unless `autoUpload` is `false`). You can listen for progress (and create a status bar, for example) by providing an `onProgress` function to the component.

### Extra props
You can pass any extra props to `<ReactS3Uploader />` and these will be passed down to the final `<input />`. which means that if you give the ReactS3Uploader a className or a name prop the input will have those as well.
Expand Down Expand Up @@ -114,6 +118,7 @@ app.use('/s3', require('react-s3-uploader/s3router')({
bucket: "MyS3Bucket",
region: 'us-east-1', //optional
signatureVersion: 'v4', //optional (use for some amazon regions: frankfurt and others)
signatureExpires: 60, //optional, number of seconds the upload signed URL should be valid for (defaults to 60)
headers: {'Access-Control-Allow-Origin': '*'}, // optional
ACL: 'private', // this is default
uniquePrefix: true // (4.0.2 and above) default is true, setting the attribute to false preserves the original filename in S3
Expand All @@ -124,6 +129,9 @@ This also provides another endpoint: `GET /s3/img/(.*)` and `GET /s3/uploads/(.*
that provides access to the uploaded file (which are uploaded privately by default). The
request is then redirected to the URL, so that the image is served to the client.

If you need to use pass more than region and signatureVersion to S3 instead use the `getS3` param. `getS3` accepts a
function that returns a new AWS.S3 instance. This is also useful if you want to mock S3 for testing purposes.

**To use this you will need to include the [express module](https://www.npmjs.com/package/express) in your package.json dependencies.**

#### Access/Secret Keys
Expand Down Expand Up @@ -178,6 +186,53 @@ respond_to do |format|
end
```

#### [Micro](https://github.com/zeit/micro)

```javascript
const aws = require('aws-sdk')
const uuidv4 = require('uuid/v4')
const { createError } = require('micro')

const options = {
bucket: 'S3_BUCKET_NAME',
region: 'S3_REGION',
signatureVersion: 'v4',
ACL: 'public-read'
}

const s3 = new aws.S3(options)

module.exports = (req, res) => {
const originalFilename = req.query.objectName

// custom filename using random uuid + file extension
const fileExtension = originalFilename.split('.').pop()
const filename = `${uuidv4()}.${fileExtension}`

const params = {
Bucket: options.bucket,
Key: filename,
Expires: 60,
ContentType: req.query.contentType,
ACL: options.ACL
}

const signedUrl = s3.getSignedUrl('putObject', params)

if (signedUrl) {
// you may also simply return the signed url, i.e. `return { signedUrl }`
return {
signedUrl,
filename,
originalFilename,
publicUrl: signedUrl.split('?').shift()
}
} else {
throw createError(500, 'Cannot create S3 signed URL')
}
}
```


##### Other Servers

Expand Down
43 changes: 34 additions & 9 deletions ReactS3Uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var ReactS3Uploader = createReactClass({
signingUrl: PropTypes.string,
getSignedUrl: PropTypes.func,
preprocess: PropTypes.func,
onSignedUrl: PropTypes.func,
onProgress: PropTypes.func,
onFinish: PropTypes.func,
onError: PropTypes.func,
Expand All @@ -29,29 +30,40 @@ var ReactS3Uploader = createReactClass({
uploadRequestHeaders: PropTypes.object,
contentDisposition: PropTypes.string,
server: PropTypes.string,
scrubFilename: PropTypes.func
scrubFilename: PropTypes.func,
s3path: PropTypes.string,
inputRef: PropTypes.oneOfType([
PropTypes.object,
PropTypes.func
]),
autoUpload: PropTypes.bool
},

getDefaultProps: function() {
return {
preprocess: function(file, next) {
console.log('Pre-process: ' + file.name);
console.log('Pre-process: ',file.name);
next(file);
},
onProgress: function(percent, message) {
console.log('Upload progress: ' + percent + '% ' + message);
onSignedUrl: function( signingServerResponse ) {
console.log('Signing server response: ', signingServerResponse);
},
onProgress: function(percent, message, file) {
console.log('Upload progress: ',`${percent} % ${message}`);
},
onFinish: function(signResult) {
console.log("Upload finished: " + signResult.publicUrl)
console.log("Upload finished: ",signResult.publicUrl)
},
onError: function(message) {
console.log("Upload error: " + message);
console.log("Upload error: ",message);
},
server: '',
signingUrlMethod: 'GET',
scrubFilename: function(filename) {
return filename.replace(/[^\w\d_\-\.]+/ig, '');
}
},
s3path: '',
autoUpload: true
};
},

Expand All @@ -61,6 +73,7 @@ var ReactS3Uploader = createReactClass({
signingUrl: this.props.signingUrl,
getSignedUrl: this.props.getSignedUrl,
preprocess: this.props.preprocess,
onSignedUrl: this.props.onSignedUrl,
onProgress: this.props.onProgress,
onFinishS3Put: this.props.onFinish,
onError: this.props.onError,
Expand All @@ -71,7 +84,8 @@ var ReactS3Uploader = createReactClass({
uploadRequestHeaders: this.props.uploadRequestHeaders,
contentDisposition: this.props.contentDisposition,
server: this.props.server,
scrubFilename: this.props.scrubFilename
scrubFilename: this.props.scrubFilename,
s3path: this.props.s3path
});
},

Expand All @@ -88,7 +102,18 @@ var ReactS3Uploader = createReactClass({
},

getInputProps: function() {
var temporaryProps = objectAssign({}, this.props, {type: 'file', onChange: this.uploadFile});
// declare ref beforehand and filter out
// `inputRef` by `ReactS3Uploader.propTypes`
var additional = {
type: 'file',
ref: this.props.inputRef
};

if ( this.props.autoUpload ) {
additional.onChange = this.uploadFile;
}

var temporaryProps = objectAssign({}, this.props, additional);
var inputProps = {};

var invalidProps = Object.keys(ReactS3Uploader.propTypes);
Expand Down
77 changes: 77 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
declare module 'react-s3-uploader' {
import { Component } from 'react';

export interface S3Response {
signedUrl: string;
publicUrl: string;
filename: string;
fileKey: string;
}

export interface ReactS3UploaderProps {
signingUrl?: string;
signingUrlMethod?: 'GET' | 'POST';
getSignedUrl?: (file: File, callback: (params: S3Response) => void) => void;
accept?: string;
s3path?: string;
preprocess?: (file: File, next: (file: File) => void) => void;
onSignedUrl?: (response: S3Response) => void;
onProgress?: (percent: number, status: string, file: File) => void;
onError?: (message: string) => void;
onFinish?: (result: S3Response, file: File) => void;
signingUrlHeaders?: {
additional: object;
};
signingUrlQueryParams?: {
additional: object;
};
signingUrlWithCredentials?: boolean;
uploadRequestHeaders?: object;
contentDisposition?: string;
server?: string;
inputRef?: (ref: HTMLInputElement) => any;
autoUpload?: boolean;
scrubFilename?: (filename: string) => string;
[key: string]: any;
}

class ReactS3Uploader extends Component<ReactS3UploaderProps, unknown> { }

export default ReactS3Uploader;
}

declare module 'react-s3-uploader/s3upload' {
import { ReactS3UploaderProps, S3Response } from 'react-s3-uploader';

export interface S3UploadOptions extends Pick<
ReactS3UploaderProps,
| 'contentDisposition'
| 'getSignedUrl'
| 'onProgress'
| 'onError'
| 'onSignedUrl'
| 'preprocess'
| 's3path'
| 'server'
| 'signingUrl'
| 'signingUrlHeaders'
| 'signingUrlMethod'
| 'signingUrlQueryParams'
| 'signingUrlWithCredentials'
| 'uploadRequestHeaders'> {
fileElement?: HTMLInputElement | null;
files?: HTMLInputElement['files'] | null;
onFinishS3Put?: ReactS3UploaderProps['onFinish'];
successResponses?: number[];
scrubFilename?: (filename: string) => string;
}

class S3Upload {
constructor(options: ReactS3UploaderProps);
abortUpload(): void;
uploadFile(file: File): Promise<S3Response>;
uploadToS3(file: File, signResult: S3Response): void;
}

export default S3Upload;
}
Loading