Skip to content

Commit

Permalink
Merge pull requests from DamonOehlman and amdstorm, bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanoehlman committed Mar 7, 2013
2 parents a7c1cae + 6ec4355 commit 1214e78
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .jshintrc
@@ -0,0 +1,3 @@
{
"indent": 4
}
25 changes: 14 additions & 11 deletions lib/multipartupload.js
Expand Up @@ -29,6 +29,9 @@ function MultiPartUpload(opts, callback) {
this.partSize = opts.partSize || 5242880; // 5MB default
this.uploadId = null;
this.uploads = new Batch();

// initialise the tmp directory based on opts (fallback to os.tmpDir())
this.tmpDir = opts.tmpDir || os.tmpDir();

if (opts.stream) {
this._putStream(opts.stream, callback);
Expand Down Expand Up @@ -59,7 +62,7 @@ MultiPartUpload.prototype._initiate = function(callback) {
});

req.end();
}
};

/**
* Streams a file to S3 using a multipart form upload
Expand All @@ -79,7 +82,7 @@ MultiPartUpload.prototype._putFile = function(file, callback) {
var stream = fs.createReadStream(file);
mpu._putStream(stream, callback);
});
}
};

/**
* Streams a stream to S3 using a multipart form upload.
Expand All @@ -101,7 +104,7 @@ MultiPartUpload.prototype._putStream = function(stream, callback) {
}
// Start handling the stream straight away
mpu._handleStream(stream, callback);
}
};

/**
Handles an incoming stream, divides it into parts, and uploads it to S3
Expand All @@ -115,7 +118,7 @@ MultiPartUpload.prototype._handleStream = function(stream, callback) {
// Create a new part
function newPart() {
var partId = parts.length + 1,
partFileName = path.resolve(path.join(os.tmpDir(), 'mpu-' + this.objectName + '-' + random_seed() + '-' + (mpu.uploadId || Date.now()) + '-' + partId)),
partFileName = path.resolve(path.join(mpu.tmpDir, 'mpu-' + this.objectName + '-' + random_seed() + '-' + (mpu.uploadId || Date.now()) + '-' + partId)),
partFile = fs.createWriteStream(partFileName),
part = {
id: partId,
Expand All @@ -129,7 +132,7 @@ MultiPartUpload.prototype._handleStream = function(stream, callback) {
}

function partReady(part) {
if (!part) return
if (!part) return;

// Ensure the stream is closed
if (part.stream.writable) {
Expand Down Expand Up @@ -169,7 +172,7 @@ MultiPartUpload.prototype._handleStream = function(stream, callback) {
// Clean up
return callback(err);
});
}
};

/**
Uploads a part, or if we are not ready yet, waits for the upload to be initiated
Expand All @@ -179,7 +182,7 @@ MultiPartUpload.prototype._uploadPart = function(part, callback) {

// If we haven't started the upload yet, wait for the initialization
if (!this.uploadId) {
return this.on('initiated', this._uploadPart.bind(this, part, callback));
return this.on('initiated', this._uploadPart.bind(this, part, callback));
}

var url = this.objectName + '?partNumber=' + part.id + '&uploadId=' + this.uploadId,
Expand All @@ -193,7 +196,7 @@ MultiPartUpload.prototype._uploadPart = function(part, callback) {
if (res.statusCode != 200) return callback({part: part.id, message: 'Upload failed'});

// Grab the etag and return it
var etag = res.headers['etag'],
var etag = res.headers.etag,
result = {part: part.id, etag: etag, size: part.length};

mpu.emit('uploaded', result);
Expand All @@ -213,7 +216,7 @@ MultiPartUpload.prototype._uploadPart = function(part, callback) {

partStream.pipe(req);
mpu.emit('uploading', part.id);
}
};

/**
Indicates that all uploads have been started and that we should wait for completion
Expand Down Expand Up @@ -247,10 +250,10 @@ MultiPartUpload.prototype._completeUploads = function(callback) {
req.write('<CompleteMultipartUpload>' + parts + '</CompleteMultipartUpload>');
req.end();
});
}
};

module.exports = MultiPartUpload;

function random_seed(){
return 'xxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);});
}
}
11 changes: 6 additions & 5 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "knox-mpu",
"version": "0.0.4",
"version": "0.1.0",
"description": "Provide multi part upload functionality to Amazon S3 using the knox library",
"keywords": ["aws", "amazon", "s3", "knox", "multi", "part", "upload"],
"main": "index.js",
Expand All @@ -14,12 +14,13 @@
"author": "Nathan Oehlman",
"license": "BSD",
"dependencies": {
"batch": "~0.2.0",
"knox": "~0.4.0",
"xml2js": "~0.2.0",
"lodash": "~0.9.0"
"batch": "0.2.x",
"xml2js": "0.2.x",
"lodash": "1.0.x"
},
"devDependencies": {
"knox": "0.5.x",
"mocha": "1.8.x",
"mockstream": "0.0.0"
}
}

0 comments on commit 1214e78

Please sign in to comment.