Permalink
Browse files

[fix] Use cURL for downloading files until GH-849 is fixed in node.js…

… core
  • Loading branch information...
indexzero committed Apr 8, 2011
1 parent df8e34b commit d41508dbab7d8f36c4222cb015bb645a14aab2f8
Showing with 33 additions and 12 deletions.
  1. +1 −0 .gitignore
  2. +4 −9 lib/cloudfiles/core.js
  3. +28 −3 lib/cloudfiles/utils.js
View
@@ -1,3 +1,4 @@
+.DS_Store
.cache/
.cache/*
test/data/test-config.json
View
@@ -234,23 +234,18 @@ Cloudfiles.prototype.getFile = function (container, filename, callback) {
var self = this, containerPath = path.join(this.config.cache.path, container);
utils.statOrMkdir(containerPath, function () {
- var cachePath = path.join(containerPath, filename);
- fileStream = fs.createWriteStream(cachePath);
+ var cacheFile = path.join(containerPath, filename);
var options = {
method: 'GET',
client: self,
uri: self.storageUrl(container, filename),
- responseBodyStream: fileStream
+ filename: cacheFile
};
- utils.rackspace(options, callback, function (body, res) {
+ utils.curlDownload(options, callback, function (body, res) {
var file = {
- content_type: res.headers['content-type'],
- bytes: res.headers['content-length'],
- hash: res.headers['etag'],
- last_modified: res.headers['last-modified'],
- local: cachePath,
+ local: cacheFile,
container: container,
name: filename
};
View
@@ -174,10 +174,11 @@ utils.rackspace = function () {
}
};
-var contentTypeOptions = '-i -H "Content-Type: {{content-type}}" ',
- authTokenOptions = '-i -H "X-AUTH-TOKEN:{{auth-token}}" ',
+var contentTypeOptions = '-H "Content-Type: {{content-type}}" ',
+ authTokenOptions = '-H "X-AUTH-TOKEN:{{auth-token}}" ',
curlOptions = '-X {{method}} {{uri}}',
- fileOptions = '-T {{filename}} ';
+ fileOptions = '-T {{filename}} ',
+ outputOptions = ' -o {{filename}}';
utils.rackspaceCurl = function (options, callback, success) {
var command = 'curl ', error = '', data = '', client = options['client'];
@@ -209,6 +210,30 @@ utils.rackspaceCurl = function (options, callback, success) {
});
};
+utils.curlDownload = function (options, callback, success) {
+ var command = 'curl -s -D - ', client = options['client'];
+
+ command += authTokenOptions.replace('{{auth-token}}', client.config.authToken);
+ command += curlOptions.replace('{{method}}', options.method).replace('{{uri}}', options.uri);
+ command += outputOptions.replace('{{filename}}', options.filename);
+
+ exec(command, function (error, stdout, stderr) {
+ if (error) return callback(error);
+
+ try {
+ var statusCode = stdout.match(/HTTP\/1.1\s(\d+)/)[1];
+ if (Object.keys(failCodes).indexOf(statusCode.toString()) !== -1) {
+ return callback(new Error('Rackspace Error (' + statusCode + '): ' + failCodes[statusCode]));
+ }
+
+ success(null, { statusCode: statusCode });
+ }
+ catch (ex) {
+ callback(ex);
+ }
+ });
+};
+
// Runs a array of functions with callback argument
// And calls finish at the end of execution
utils.runBatch = function(fns, finish) {

0 comments on commit d41508d

Please sign in to comment.