-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Issue #212 - Implement S3 signatures for S3 direct upload functionality (#273) * S3 class lets you generate a new signature for direct upload * S3 class lets you generate signed URL for access to a S3 object * S3Signature#show endpoint provides a signature used for direct upload * ResourcePathBuilder is a helper class for build object paths used for upload file to S3 #212 * Issue 212 resource form (#287) * Issue #212 - Implement S3 signatures for S3 direct upload functionality * S3 class lets you generate a new signature for direct upload * S3 class lets you generate signed URL for access to a S3 object * S3Signature#show endpoint provides a signature used for direct upload * ResourcePathBuilder is a helper class for build object paths used for upload file to S3 #212 * Issue #212 - Implement S3 signatures for S3 direct upload functionality (#273) * S3 class lets you generate a new signature for direct upload * S3 class lets you generate signed URL for access to a S3 object * S3Signature#show endpoint provides a signature used for direct upload * ResourcePathBuilder is a helper class for build object paths used for upload file to S3 #212 * Issue #212 - Implemented resource form with file upload. file content processing done. * A user can upload a PDF file as part of a resource * The user can add metadata on edit, but cannot change the url or file * When the user uploads PDF a background job extract the content and beings indexed * S3 can fetch files. * New queue created in Sidekiq: file. #212
- Loading branch information
Showing
32 changed files
with
742 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
110 changes: 110 additions & 0 deletions
110
app/assets/javascripts/components/file_picker/direct_upload_input.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
var DirectUploadInput = React.createClass({ | ||
propTypes: { | ||
name: React.PropTypes.string, | ||
id: React.PropTypes.string, | ||
onUploadSuccess: React.PropTypes.func | ||
}, | ||
|
||
getInitialState: function() { | ||
return { | ||
s3Url: [location.protocol, "//", window.greencommons.S3.bucket, ".s3.amazonaws.com/"].join(''), | ||
errorMesssage: null, | ||
uploadMesssage: null | ||
}; | ||
}, | ||
|
||
buildFormData: function(signature, file){ | ||
var formData = new FormData(); | ||
formData.append('acl', signature.acl); | ||
formData.append('AWSAccessKeyId', window.greencommons.S3.accessKeyId); | ||
formData.append('success_action_status', 200); | ||
formData.append('key', signature.key); | ||
formData.append('policy', signature.policy); | ||
formData.append('signature', signature.signature); | ||
formData.append('Content-Type', signature.mime_type); | ||
formData.append('file', file); | ||
return formData; | ||
}, | ||
|
||
requestSignature: function(file, filename){ | ||
var url = new URL("/s3_signature", location.origin); | ||
var _this = this; | ||
url.searchParams.append('filename', filename); | ||
return fetch(url).then(function(response){ | ||
if (response.ok) { | ||
return response.json(); | ||
} else { | ||
_this.setState({ | ||
errorMesssage: 'The file was not uploaded correctly. Please try again', | ||
uploadMesssage: null | ||
}); | ||
throw new Error(response.statusText); | ||
} | ||
}); | ||
}, | ||
|
||
uploadToS3: function(signature, file){ | ||
var formData = this.buildFormData(signature, file); | ||
var _this = this; | ||
var headers = new Headers(); | ||
headers.append('Access-Control-Allow-Origin', '*'); | ||
return fetch( | ||
this.state.s3Url, | ||
{method: 'POST', body: formData, mode: 'cors', headers: headers} | ||
).then(function(response){ | ||
if (response.ok) { | ||
return response; | ||
} else { | ||
_this.setState({ | ||
errorMesssage: 'The file was not uploaded correctly. Please try again', | ||
uploadMesssage: null | ||
}); | ||
throw new Error(response.statusText); | ||
} | ||
}).then(function(response){ | ||
_this.props.onUploadSuccess(file, _this.state.s3Url + signature.key); | ||
}); | ||
}, | ||
|
||
handleChange: function(event) { | ||
var _this = this; | ||
var file = event.target.files[0]; | ||
this.setState({errorMesssage: null, uploadMesssage: 'Uploading...'}); | ||
_this.requestSignature(file, file.name) | ||
.then(function(json){ | ||
return _this.uploadToS3(json.s3_signature, file); | ||
}).then(function(){ _this.setState({uploadMesssage: null}) }); | ||
}, | ||
|
||
displayErrorMessage: function(){ | ||
if (this.state.errorMesssage){ | ||
return (<div className="alert-warning">{this.state.errorMesssage}</div>) | ||
} | ||
}, | ||
|
||
displayUploadingMessage: function(){ | ||
if (this.state.uploadMesssage){ | ||
return (<div className="alert-notice">{this.state.uploadMesssage}</div>) | ||
} | ||
}, | ||
|
||
render: function() { | ||
return ( | ||
<div> | ||
<FileInput | ||
id={this.props.id} | ||
name={this.props.name} | ||
accept={this.props.accept} | ||
placeholder={this.props.placeholder} | ||
onChange={this.handleChange} | ||
className="form-control" | ||
/> | ||
<div> | ||
{this.displayUploadingMessage()} | ||
{this.displayErrorMessage()} | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
}); |
75 changes: 75 additions & 0 deletions
75
app/assets/javascripts/components/file_picker/file_input.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
var FileInput = React.createClass({ | ||
propTypes: { | ||
name: React.PropTypes.string, | ||
id: React.PropTypes.string | ||
}, | ||
|
||
randomString: function(){ | ||
return Math.random().toString(36); | ||
}, | ||
|
||
getInitialState: function() { | ||
return { | ||
value: '', | ||
styles: { | ||
parent: { | ||
position: 'relative' | ||
}, | ||
file: { | ||
position: 'absolute', | ||
top: 0, | ||
left: 0, | ||
opacity: 0, | ||
width: '100%', | ||
zIndex: 1 | ||
}, | ||
text: { | ||
zIndex: -1 | ||
} | ||
} | ||
}; | ||
}, | ||
|
||
handleChange: function(e) { | ||
this.setState({ | ||
value: e.target.value.split(/(\\|\/)/g).pop() | ||
}); | ||
if (this.props.onChange) this.props.onChange(e); | ||
}, | ||
|
||
reset: function(){ | ||
this.setState({value: '', fileInputKey: this.randomString()}) | ||
}, | ||
|
||
render: function() { | ||
return React.DOM.div({ | ||
style: this.state.styles.parent | ||
}, | ||
|
||
// Actual file input | ||
React.DOM.input({ | ||
type: 'file', | ||
id: this.props.id, | ||
name: this.props.name, | ||
className: this.props.className, | ||
onChange: this.handleChange, | ||
disabled: this.props.disabled, | ||
accept: this.props.accept, | ||
style: this.state.styles.file, | ||
key: (this.state.theInputKey || '') | ||
}), | ||
|
||
// Emulated file input | ||
React.DOM.input({ | ||
type: 'text', | ||
tabIndex: -1, | ||
name: '_filename', | ||
value: this.state.value, | ||
className: this.props.className, | ||
onChange: function() {}, | ||
placeholder: this.props.placeholder, | ||
disabled: this.props.disabled, | ||
style: this.state.styles.text | ||
})); | ||
} | ||
}); |
41 changes: 41 additions & 0 deletions
41
app/assets/javascripts/components/file_picker/pdf_file_input.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
var PdfFileInput = React.createClass({ | ||
propTypes: { | ||
name: React.PropTypes.string, | ||
id: React.PropTypes.string, | ||
onUploadSuccess: React.PropTypes.oneOfType([ | ||
React.PropTypes.string, | ||
React.PropTypes.func, | ||
]) | ||
}, | ||
|
||
getInitialState: function() { | ||
return {}; | ||
}, | ||
|
||
_wrapOnUploadSuccess: function(file, fileUrl){ | ||
if(typeof this.props.onUploadSuccess === 'string'){ | ||
window[this.props.onUploadSuccess](file, fileUrl) | ||
}else{ | ||
this.props.onUploadSuccess(file, fileUrl) | ||
} | ||
}, | ||
|
||
|
||
handleOnUploadSuccess: function(file, fileUrl) { | ||
this._wrapOnUploadSuccess(file, fileUrl); | ||
}, | ||
|
||
|
||
render: function() { | ||
return ( | ||
<DirectUploadInput | ||
id={this.props.id} | ||
name={this.props.name} | ||
accept=".pdf" | ||
placeholder="Select a PDF file." | ||
onUploadSuccess={this.handleOnUploadSuccess} | ||
/> | ||
); | ||
} | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
window.processResourceFile = function(file, fileUrl){ | ||
$('input#resource_url').val(fileUrl); | ||
$('input#resource_content_download_link').val(fileUrl); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.