Created
April 15, 2015 06:40
-
-
Save alex-dixon/8d24a0dfa24c444d27f6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var Joi = require('joi'); | |
var fs = require('fs'); | |
var Hoek = require('hoek'); | |
var cloudinary = require('cloudinary'); | |
var Cookie = require('cookie'); | |
exports.register = function (server, options, next) { | |
options = Hoek.applyToDefaults({basePath: ''}, options); | |
server.route({ | |
method: 'POST', | |
path: options.basePath + '/addapet', | |
config: { | |
payload: { | |
maxBytes: 209715200, | |
output: 'stream', | |
parse: true | |
}, | |
handler: function (request, reply) { | |
cloudinary.config({ | |
cloud_name: 'xxx', | |
api_key: 'xxx', | |
api_secret: 'xxx' | |
}); | |
console.log(request.payload['imageFile']); | |
request.payload['imageFile'].pipe(fs.createWriteStream('test')); | |
var stream = cloudinary.uploader.upload_stream(function(result) { console.log(result); }); | |
var upload = request.payload.image.pipe(fs.createWriteStream("upload")); | |
upload.on('data', function(data) { stream.write(data) }) | |
.on('end', function() { stream.end() }); | |
var file_reader = fs.createReadStream(request.payload.path, {encoding: 'binary'}) | |
.on('data', function(data) { stream.write(data) }) | |
.on('end', function() { stream.end() }); | |
} | |
} | |
}); | |
next(); | |
}; |
This file contains hidden or 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
onDrop: function(e) { | |
e.preventDefault(); | |
this.setState({ | |
isDragActive: false | |
}); | |
var files; | |
if (e.dataTransfer) { | |
files = e.dataTransfer.files; | |
} else if (e.target) { | |
files = e.target.files; | |
} | |
if (this.props.onDrop) { | |
files = Array.prototype.slice.call(files); | |
this.props.onDrop(files); | |
} | |
}, | |
[...] | |
return ( | |
<div className={className} style={style} onClick={this.onClick} onDragLeave={this.onDragLeave} onDragOver={this.onDragOver} onDrop={this.onDrop}> | |
<input style={{display: 'none' }} type='file' multiple ref='fileInput' name="imageFile" onChange={this.onDrop} /> | |
{this.props.children} | |
</div> | |
); | |
} |
This file contains hidden or 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
onDrop: function(files) { | |
var cookie = Cookie.parse(document.cookie); | |
var xhr = new XMLHttpRequest(); | |
xhr.open('POST', '/api/addapet'); | |
xhr.setRequestHeader('X-CSRF-Token', cookie.crumb); | |
xhr.send(files); | |
}, | |
[...] | |
var formElements; | |
if (!this.state.success) { | |
formElements = | |
<div> | |
<Dropzone onDrop={this.onDrop} size={150} > | |
<div> Drop files here, or click and select multiple files to upload.</div> | |
</Dropzone> | |
</div>; | |
} | |
return ( | |
<section> | |
<h1 className="page-header">Sign up</h1> | |
<form ref="imageForm" method="POST" action="api/addapet" encType="multipart/form-data"> | |
{alerts} | |
{formElements} | |
</form> | |
</section> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment