Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix error handler

  • Loading branch information...
commit 119ea23fdbc19485ad1ffaf2e6aad45ba5af0a71 1 parent 6217ba6
@firejune authored
Showing with 44 additions and 35 deletions.
  1. +42 −33 lib/swift.js
  2. +2 −2 package.json
View
75 lib/swift.js
@@ -37,7 +37,7 @@ function Swift(options, callback) {
, 'X-Storage-Pass': this.options.pass
}
}, 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.token = res.headers['x-auth-token'];
}
@@ -91,7 +91,13 @@ function request(options, callback, pipe) {
}
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()
+ });
+ }
});
});
@@ -104,7 +110,7 @@ function request(options, callback, pipe) {
var bytesReceived = 0
, contentLength = 76
- , parser = options.boundary ? multipart.call(extend(options, {
+ , parser = options.boundary ? multipart(extend(options, {
onHeadersEnd: function(part) {
//options.contentLength -= contentLength + options.boundary.length * 2 + part.name.length + part.filename.length + part.mime.length + 8;
},
@@ -137,14 +143,13 @@ function fileName(headerValue) {
return filename;
}
-function multipart() {
+function multipart(options) {
var parser = new MultipartParser()
- , self = this
, headerField
, headerValue
, part = {};
- parser.initWithBoundary(this.boundary);
+ parser.initWithBoundary(options.boundary);
parser.onPartBegin = function() {
part.headers = {};
@@ -156,11 +161,11 @@ function multipart() {
};
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) {
- headerValue += b.toString(self.encoding, start, end);
+ headerValue += b.toString(options.encoding, start, end);
};
parser.onHeaderEnd = function() {
@@ -180,26 +185,16 @@ function multipart() {
};
parser.onHeadersEnd = function() {
- self.onHeadersEnd && self.onHeadersEnd(part);
+ options.onHeadersEnd && options.onHeadersEnd(part);
};
parser.onPartData = function(b, start, end) {
- self.onPartData && self.onPartData(b.slice(start, end));
+ options.onPartData && options.onPartData(b.slice(start, end));
};
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.
*/
@@ -211,7 +206,7 @@ Swift.prototype.listContainers = function(callback) {
}, callback);
};
-// Retrieve Account Metadata
+// Retrieve Account Metadata *
Swift.prototype.retrieveAccountMetadata = function(callback) {
request.call(this, {
path: '/v1.0/' + this.account
@@ -251,7 +246,6 @@ Swift.prototype.deleteContainer = function(container, callback) {
}, callback);
};
-
this.listObjects(container, function(err, result) {
try {
objects = JSON.parse(result.body);
@@ -266,7 +260,7 @@ Swift.prototype.deleteContainer = function(container, callback) {
});
};
-// Retrieve Container Metadata
+// Retrieve Container Metadata *
Swift.prototype.retrieveContainerMetadata = function(container, callback) {
request.call(this, {
path: '/v1.0/' + this.account + '/' + container
@@ -279,7 +273,16 @@ Swift.prototype.retrieveContainerMetadata = function(container, callback) {
* 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) {
request.call(this, {
path: '/v1.0/' + this.account + '/' + container + '/' + object
@@ -301,9 +304,7 @@ Swift.prototype.createObject = Swift.prototype.updateObject = function(container
};
if (req.xhr) {
- extend(options.headers, {
- 'Content-Length': req.headers['content-length']
- });
+ options.headers['Content-Length'] = req.headers['content-length'];
} else {
var boundary = req.headers['content-type'].match(/boundary=(?:"([^"]+)"|([^;]+))/i);
extend(options, {
@@ -311,10 +312,7 @@ Swift.prototype.createObject = Swift.prototype.updateObject = function(container
, encoding: 'utf-8'
, boundary: boundary[1] || boundary[2]
});
-
- extend(options.headers, {
- 'Transfer-Encoding': 'chunked'
- });
+ options.headers['Transfer-Encoding'] = 'chunked';
}
request.call(this, options, callback, {req: req});
@@ -328,7 +326,7 @@ Swift.prototype.deleteObject = function(container, object, callback) {
}, callback);
};
-// Retrieve Object Metadata
+// Retrieve Object Metadata *
Swift.prototype.retrieveObjectMetadata = function(container, object, callback) {
request.call(this, {
path: '/v1.0/' + this.account + '/' + container + '/' + object
@@ -336,7 +334,7 @@ Swift.prototype.retrieveObjectMetadata = function(container, object, callback) {
}, callback);
};
-// Update Object Metadata
+// Update Object Metadata *
Swift.prototype.updateObjectMetadata = function(container, object, callback) {
request.call(this, {
path: '/v1.0/' + this.account + '/' + container + '/' + object
@@ -356,4 +354,15 @@ Swift.prototype.copyObject = function(container, destObject, fromContainer, sour
}, 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;
View
4 package.json
@@ -1,7 +1,7 @@
{
"name": "swift",
"author": "Firejune (http://firejune.com/)",
- "version": "0.1.7",
+ "version": "0.1.8",
"description": "OpenStack Object Storage(Swift) REST client API for Node.JS",
"homepage": "http://github.com/firejune/swift",
"keywords": ["Swift", "OpenStack", "Object Storage"],
@@ -19,7 +19,7 @@
},
"licenses": [{
"type": "MIT",
- "url": "http://www.opensource.org/licenses/mit-license.php"
+ "url": "http://firejune.mit-license.org/"
}],
"bugs" : {
"url" : "http://github.com/firejune/swift/issues"
Please sign in to comment.
Something went wrong with that request. Please try again.