Skip to content

Commit

Permalink
fix error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Junho Kyung committed Jan 26, 2012
1 parent 6217ba6 commit 119ea23
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 35 deletions.
75 changes: 42 additions & 33 deletions lib/swift.js
Expand Up @@ -37,7 +37,7 @@ function Swift(options, callback) {
, 'X-Storage-Pass': this.options.pass , 'X-Storage-Pass': this.options.pass
} }
}, function(err, res) { }, function(err, res) {
if (res.headers['x-storage-url'] && res.headers['x-auth-token']) { if (!err && res.headers['x-storage-url'] && res.headers['x-auth-token']) {
this.account = res.headers['x-storage-url'].split('v1/')[1]; this.account = res.headers['x-storage-url'].split('v1/')[1];
this.token = res.headers['x-auth-token']; this.token = res.headers['x-auth-token'];
} }
Expand Down Expand Up @@ -91,7 +91,13 @@ function request(options, callback, pipe) {
} }


res.on('end', function(err){ res.on('end', function(err){
clog.info(res.statusCode, options.path, res.headers); //clog.info(res.statusCode, options.path, res.headers);
if (res.statusCode >= 400) {
callback && callback({
statusCode: res.statusCode,
body: buffers.toString()
});
}
}); });
}); });


Expand All @@ -104,7 +110,7 @@ function request(options, callback, pipe) {


var bytesReceived = 0 var bytesReceived = 0
, contentLength = 76 , contentLength = 76
, parser = options.boundary ? multipart.call(extend(options, { , parser = options.boundary ? multipart(extend(options, {
onHeadersEnd: function(part) { onHeadersEnd: function(part) {
//options.contentLength -= contentLength + options.boundary.length * 2 + part.name.length + part.filename.length + part.mime.length + 8; //options.contentLength -= contentLength + options.boundary.length * 2 + part.name.length + part.filename.length + part.mime.length + 8;
}, },
Expand Down Expand Up @@ -137,14 +143,13 @@ function fileName(headerValue) {
return filename; return filename;
} }


function multipart() { function multipart(options) {
var parser = new MultipartParser() var parser = new MultipartParser()
, self = this
, headerField , headerField
, headerValue , headerValue
, part = {}; , part = {};


parser.initWithBoundary(this.boundary); parser.initWithBoundary(options.boundary);


parser.onPartBegin = function() { parser.onPartBegin = function() {
part.headers = {}; part.headers = {};
Expand All @@ -156,11 +161,11 @@ function multipart() {
}; };


parser.onHeaderField = function(b, start, end) { parser.onHeaderField = function(b, start, end) {
headerField += b.toString(self.encoding, start, end); headerField += b.toString(options.encoding, start, end);
}; };


parser.onHeaderValue = function(b, start, end) { parser.onHeaderValue = function(b, start, end) {
headerValue += b.toString(self.encoding, start, end); headerValue += b.toString(options.encoding, start, end);
}; };


parser.onHeaderEnd = function() { parser.onHeaderEnd = function() {
Expand All @@ -180,26 +185,16 @@ function multipart() {
}; };


parser.onHeadersEnd = function() { parser.onHeadersEnd = function() {
self.onHeadersEnd && self.onHeadersEnd(part); options.onHeadersEnd && options.onHeadersEnd(part);
}; };


parser.onPartData = function(b, start, end) { parser.onPartData = function(b, start, end) {
self.onPartData && self.onPartData(b.slice(start, end)); options.onPartData && options.onPartData(b.slice(start, end));
}; };


return parser; return parser;
} }



Swift.prototype.getFile = function(container, object, callback, res) {
request.call(this, {
path: '/v1.0/' + this.account + '/' + container + '/' + object
}, callback, {
res: res
});
};


/** /**
* Storage Account Services. * Storage Account Services.
*/ */
Expand All @@ -211,7 +206,7 @@ Swift.prototype.listContainers = function(callback) {
}, callback); }, callback);
}; };


// Retrieve Account Metadata // Retrieve Account Metadata *
Swift.prototype.retrieveAccountMetadata = function(callback) { Swift.prototype.retrieveAccountMetadata = function(callback) {
request.call(this, { request.call(this, {
path: '/v1.0/' + this.account path: '/v1.0/' + this.account
Expand Down Expand Up @@ -251,7 +246,6 @@ Swift.prototype.deleteContainer = function(container, callback) {
}, callback); }, callback);
}; };



this.listObjects(container, function(err, result) { this.listObjects(container, function(err, result) {
try { try {
objects = JSON.parse(result.body); objects = JSON.parse(result.body);
Expand All @@ -266,7 +260,7 @@ Swift.prototype.deleteContainer = function(container, callback) {
}); });
}; };


// Retrieve Container Metadata // Retrieve Container Metadata *
Swift.prototype.retrieveContainerMetadata = function(container, callback) { Swift.prototype.retrieveContainerMetadata = function(container, callback) {
request.call(this, { request.call(this, {
path: '/v1.0/' + this.account + '/' + container path: '/v1.0/' + this.account + '/' + container
Expand All @@ -279,7 +273,16 @@ Swift.prototype.retrieveContainerMetadata = function(container, callback) {
* Storage Object Services. * Storage Object Services.
*/ */


// Retrieve Object // Object stream on pipe
Swift.prototype.getFile = function(container, object, callback, res) {
request.call(this, {
path: '/v1.0/' + this.account + '/' + container + '/' + object
}, callback, {
res: res
});
};

// Retrieve Object *
Swift.prototype.retrieveObject = function(container, object, callback) { Swift.prototype.retrieveObject = function(container, object, callback) {
request.call(this, { request.call(this, {
path: '/v1.0/' + this.account + '/' + container + '/' + object path: '/v1.0/' + this.account + '/' + container + '/' + object
Expand All @@ -301,20 +304,15 @@ Swift.prototype.createObject = Swift.prototype.updateObject = function(container
}; };


if (req.xhr) { if (req.xhr) {
extend(options.headers, { options.headers['Content-Length'] = req.headers['content-length'];
'Content-Length': req.headers['content-length']
});
} else { } else {
var boundary = req.headers['content-type'].match(/boundary=(?:"([^"]+)"|([^;]+))/i); var boundary = req.headers['content-type'].match(/boundary=(?:"([^"]+)"|([^;]+))/i);
extend(options, { extend(options, {
contentLength: req.headers['content-length'] contentLength: req.headers['content-length']
, encoding: 'utf-8' , encoding: 'utf-8'
, boundary: boundary[1] || boundary[2] , boundary: boundary[1] || boundary[2]
}); });

options.headers['Transfer-Encoding'] = 'chunked';
extend(options.headers, {
'Transfer-Encoding': 'chunked'
});
} }


request.call(this, options, callback, {req: req}); request.call(this, options, callback, {req: req});
Expand All @@ -328,15 +326,15 @@ Swift.prototype.deleteObject = function(container, object, callback) {
}, callback); }, callback);
}; };


// Retrieve Object Metadata // Retrieve Object Metadata *
Swift.prototype.retrieveObjectMetadata = function(container, object, callback) { Swift.prototype.retrieveObjectMetadata = function(container, object, callback) {
request.call(this, { request.call(this, {
path: '/v1.0/' + this.account + '/' + container + '/' + object path: '/v1.0/' + this.account + '/' + container + '/' + object
, method: 'HEAD' , method: 'HEAD'
}, callback); }, callback);
}; };


// Update Object Metadata // Update Object Metadata *
Swift.prototype.updateObjectMetadata = function(container, object, callback) { Swift.prototype.updateObjectMetadata = function(container, object, callback) {
request.call(this, { request.call(this, {
path: '/v1.0/' + this.account + '/' + container + '/' + object path: '/v1.0/' + this.account + '/' + container + '/' + object
Expand All @@ -356,4 +354,15 @@ Swift.prototype.copyObject = function(container, destObject, fromContainer, sour
}, callback); }, callback);
}; };


// Move Object
Swift.prototype.moveObject = function(container, destObject, fromContainer, sourceObject, callback) {
var self = this;
//if (container == fromContainer) return callback('move error');
self.copyObject(container, destObject, fromContainer, sourceObject, function(err, result) {
self.deleteObject(fromContainer, sourceObject, function(err, result) {
callback(err, result);
});
});
};

module.exports = Swift; module.exports = Swift;
4 changes: 2 additions & 2 deletions package.json
@@ -1,7 +1,7 @@
{ {
"name": "swift", "name": "swift",
"author": "Firejune (http://firejune.com/)", "author": "Firejune (http://firejune.com/)",
"version": "0.1.7", "version": "0.1.8",
"description": "OpenStack Object Storage(Swift) REST client API for Node.JS", "description": "OpenStack Object Storage(Swift) REST client API for Node.JS",
"homepage": "http://github.com/firejune/swift", "homepage": "http://github.com/firejune/swift",
"keywords": ["Swift", "OpenStack", "Object Storage"], "keywords": ["Swift", "OpenStack", "Object Storage"],
Expand All @@ -19,7 +19,7 @@
}, },
"licenses": [{ "licenses": [{
"type": "MIT", "type": "MIT",
"url": "http://www.opensource.org/licenses/mit-license.php" "url": "http://firejune.mit-license.org/"
}], }],
"bugs" : { "bugs" : {
"url" : "http://github.com/firejune/swift/issues" "url" : "http://github.com/firejune/swift/issues"
Expand Down

0 comments on commit 119ea23

Please sign in to comment.