Skip to content

Direct upload #5

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

Open
wants to merge 2 commits 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
4 changes: 2 additions & 2 deletions .npm/package/npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

93 changes: 93 additions & 0 deletions client/lib/s3Direct.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# S3 CORS upload

# https://devcenter.heroku.com/articles/s3-upload-node
# http://docs.amazonwebservices.com/AmazonS3/latest/dev/cors.html#how-do-i-enable-cors
# http://www.ioncannon.net/programming/1539/direct-browser-uploading-amazon-s3-cors-fileapi-xhr2-and-signed-puts/
# https://github.com/carsonmcdonald/direct-browser-s3-upload-example
# http://stackoverflow.com/questions/17397924/amazon-s3-strange-error-sometimes-signaturedoesnotmatch-sometimes-it-does
# http://stackoverflow.com/questions/20709711/amazon-s3-signature-does-not-match-aws-sdk-java


class window.S3DirectUpload
onError: (status) ->
console.log 'base.onError()', status
onProgress: (percent, status) ->
console.log 'base.onProgress()', percent, status
onFinishS3Put: (publicUrl) ->
console.log 'base.onFinishS3Put()', publicUrl

# Don't override these
constructor: (settings) ->
@settings = settings
@uploadFile(settings.file)

createCORSRequest: (method, url) ->
xhr = new XMLHttpRequest()
if xhr.withCredentials?
xhr.open method, url, true
else if typeof XDomainRequest isnt "undefined"
xhr = new XDomainRequest()
xhr.open method, url
else
xhr = null
xhr

# Use a CORS call to upload the given file to S3. Assumes the url
# parameter has been signed and is accessible for upload.
uploadToS3: (file, url, publicUrl) ->
self = this
xhr = @createCORSRequest 'PUT', url
if !xhr
@onError 'CORS not supported'
else
xhr.onload = ->

if xhr.status is 200
self.onProgress 100, 'Upload completed.'
self.onFinishS3Put publicUrl
else
self.onError 'Upload error: ' + xhr.status

xhr.onerror = ->
self.onError 'XHR error.'

xhr.upload.onprogress = (e) ->
if e.lengthComputable
percentLoaded = Math.round (e.loaded / e.total) * 100
self.onProgress percentLoaded, if percentLoaded is 100 then 'Finalizing.' else 'Uploading.'

xhr.setRequestHeader 'Content-Type', file.type
# xhr.setRequestHeader 'x-amz-acl', 'public-read' #need tests

xhr.send file.data

uploadFile: (file) ->
self = this
@onProgress 0, 'Upload started.'
Meteor.call 'uploaderSignedUrl', file.name, file.type, (error, signedUrl) ->
return @onError error if error
publicUrl = signedUrl.substring(0, signedUrl.indexOf('?'));
self.uploadToS3 file, signedUrl, publicUrl



class window.UploaderS3DirectUpload extends S3DirectUpload
onProgress: (percent, message) ->
Session.set "uploader-progress-#{@settings.name}", percent
onFinishS3Put: (publicUrl) ->
completed = Session.get "uploader-completed-#{@settings.name}"
Session.set "uploader-completed-#{@settings.name}", ++completed
result =
url: publicUrl
fileName: @settings.file.name
originalFileName: @settings.file.originalName
uploaderName: @settings.name

if @settings.onUpload
@settings.onUpload(false, result)
onError: (reason) ->
Session.set "uploader-error-#{@settings.name}", reason




4 changes: 4 additions & 0 deletions client/views/uploader.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ dataURLToUA = (dataUrl) ->
i++
ua


uploadFile = (settings, fileData) ->
settings.file = fileData

if settings.directUpload
return new UploaderS3DirectUpload(settings)

Meteor.call "uploaderUpload", settings, (error, result) ->
# Display error
if error then Session.set "uploader-error-#{settings.name}", error.reason
Expand Down
1 change: 1 addition & 0 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Package.onUse(function (api) {

api.addFiles([
"client/views/uploader.html",
"client/lib/s3Direct.coffee",
"client/views/uploader.coffee",
"client/views/uploader.less"
], "client");
Expand Down
12 changes: 12 additions & 0 deletions server/server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,15 @@ Meteor.methods
if response
future.return response
future.wait()


uploaderSignedUrl: (objectName, mimeType) ->
config = Uploader.getConfig()
knox = Knox.createClient config
path = (config.directory or "") + Meteor.userId() + '/' + objectName
signedUrl = knox.signedUrl(path, new Date(Date.now() + 60000 * 5),
verb: "PUT"
contentType: mimeType
'x-amz-acl': 'public-read'
)
signedUrl
34 changes: 21 additions & 13 deletions versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@
"0.1.17"
],
[
"blaze",
"base64",
"1.0.0"
],
[
"blaze",
"2.0.1"
],
[
"coffeescript",
"1.0.2"
"1.0.3"
],
[
"deps",
"1.0.1"
"1.0.4"
],
[
"ejson",
"1.0.0"
"1.0.3"
],
[
"geojson-utils",
Expand All @@ -30,7 +34,7 @@
],
[
"htmljs",
"1.0.0-cordova1"
"1.0.1"
],
[
"id-map",
Expand All @@ -46,19 +50,19 @@
],
[
"less",
"1.0.5"
"1.0.9"
],
[
"meteor",
"1.0.2-cordova1"
"1.1.1"
],
[
"minimongo",
"1.0.0"
"1.0.3"
],
[
"observe-sequence",
"1.0.0"
"1.0.2"
],
[
"ordered-dict",
Expand All @@ -68,20 +72,24 @@
"random",
"1.0.0"
],
[
"reactive-var",
"1.0.2"
],
[
"templating",
"1.0.4"
"1.0.7"
],
[
"ui",
"1.0.0"
"tracker",
"1.0.2"
],
[
"underscore",
"1.0.0"
]
],
"pluginDependencies": [],
"toolVersion": "[email protected].25",
"toolVersion": "[email protected].32",
"format": "1.0"
}