Permalink
Browse files

upload data can be a buffer; fixed binary upload issue

  • Loading branch information...
1 parent beba39b commit e848687ff7a996ab32e6e6b89ae8b807fc21cd1d @ashchan committed Jan 12, 2012
Showing with 22 additions and 4 deletions.
  1. +1 −0 .gitignore
  2. +6 −1 README.md
  3. +7 −3 lib/upyun-core.js
  4. +8 −0 test/upyun.js
View
@@ -1,2 +1,3 @@
node_modules
npm-debug.log
+test.js
View
@@ -50,6 +50,11 @@ or simply upload the file, with the folder created automatically:
client.uploadFile("docs/readme.txt", "gently down the stream", function(err, status, data){});
+It's certainly possible to upload from a buffer:
+
+ var buffer = new Buffer("fill me", "binary");
+ client.uploadFile("test.txt", buffer, function(err, status, data) {});
+
### Download a file (get file content)
client.downloadFile("docs/readme.txt", function(err, status, data) {
@@ -90,7 +95,7 @@ To list files under root (bucket), pass null in as the first parameter:
BUCKET=yourtestbucket USER=yourusername PASSWORD=yourpassword npm test
-yourtestbucket should exist. It's better to create a test only bucket for this.
+NOTE: yourtestbucket should exist and be empty. It's better to create a test only bucket for this.
## Known Issues
View
@@ -30,6 +30,10 @@ var UpyunCore = (function() {
};
var _request = function(method, uri, data, username, password, callback, headerOptions) {
+ if (typeof(data) == "string") {
+ data = new Buffer(data, "binary");
+ }
+
var uri = url.parse(uri)
, ts = new Date()
, contentLength = data ? data.length : 0
@@ -53,13 +57,13 @@ var UpyunCore = (function() {
}
var req = http.request(options, function(res) {
- var data = "";
+ var buffer = "";
res.setEncoding('utf8');
res.on('data', function(chunk) {
- data += chunk;
+ buffer += chunk;
});
res.on('end', function() {
- callback(null, res.statusCode, data);
+ callback(null, res.statusCode, buffer);
});
});
View
@@ -26,6 +26,14 @@ test("upload a file", function(t) {
});
});
+test("upload a file with buffer", function(t) {
+ t.plan(1);
+ var buffer = new Buffer("test uploading " + ts, "binary");
+ client.uploadFile("test.txt", buffer, function(err, status, data) {
+ t.equal(status, 200, "file (with buffer) should be uploaded");
+ });
+});
+
test("download a file", function(t) {
t.plan(2);
client.downloadFile("test.txt", function(err, status, data) {

0 comments on commit e848687

Please sign in to comment.