From c55014b8a475b3e3d786ae346b9a406ad5615a00 Mon Sep 17 00:00:00 2001 From: ikbear Date: Tue, 15 Jan 2013 00:21:51 +0800 Subject: [PATCH] fix crc32 and action string bug --- lib/auth.js | 16 ++++++++++++---- lib/rs.js | 13 +++++++------ lib/util.js | 6 +++--- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/lib/auth.js b/lib/auth.js index a7715090..b6961762 100644 --- a/lib/auth.js +++ b/lib/auth.js @@ -12,12 +12,20 @@ function UploadToken(opts) { UploadToken.prototype.generateSignature = function() { var params = { - "scope": this.scope, "deadline": this.expires + Math.floor(Date.now() / 1000), - "callbackUrl": this.callbackUrl, - "callbackBodyType": this.callbackBodyType, - "customer": this.customer, }; + if (this.scope !== null) { + params["scope"] = this.scope; + } + if (this.callbackUrl !== null) { + params["callbackurl"] = this.callbackUrl; + } + if (this.callbackBodyType !== null) { + params["callbackBodyType"] = this.callbackBodyType; + } + if (this.customer !== null) { + params["customer"] = this.customer; + } var paramsString = JSON.stringify(params) return util.encode(paramsString); }; diff --git a/lib/rs.js b/lib/rs.js index 647c630d..bb4756ce 100644 --- a/lib/rs.js +++ b/lib/rs.js @@ -117,9 +117,9 @@ Service.prototype.uploadFile = function(upToken, key, mimeType, localFile, onret }); }; -Service.prototype.uploadWithToken = function(uploadToken, stream, key, mimeType, customMeta, callbackParams, enableCrc32Check, onret) { +Service.prototype.uploadWithToken = function(uploadToken, stream, key, mimeType, customMeta, callbackParams, crc32, onret) { /* - * func UploadWithToken(uploadToken, stream, key, mimeType, customMeta, callbackParams, enableCrc32Check, onret) => (data PutRet, code int, err Error) + * func UploadWithToken(uploadToken, stream, key, mimeType, customMeta, callbackParams, crc32, onret) => (data PutRet, code int, err Error) * 使用upload_token以multipart/form-data形式上传ReadStream流 **/ var bucket = this.bucket; @@ -127,7 +127,7 @@ Service.prototype.uploadWithToken = function(uploadToken, stream, key, mimeType, mimeType = "application/octet-stream"; } - var actionString = util.generateActionString(bucket, key, mimeType, customMeta, enableCrc32Check); + var actionString = util.generateActionString(bucket, key, mimeType, customMeta, crc32); if (callbackParams === null) { callbackParams = { "bucket": bucket, @@ -165,7 +165,8 @@ Service.prototype.uploadFileWithToken = function(uploadToken, localFile, key, mi onret({code: -1, error: err.toString(), detail: err}); return; } - var stream = fs.createReadStream(localFile); + var fileCrc32 = null + , stream = fs.createReadStream(localFile); if (enableCrc32Check) { var fileStat = fs.statSync(localFile) @@ -175,10 +176,10 @@ Service.prototype.uploadFileWithToken = function(uploadToken, localFile, key, mi fs.readSync(fd, buf, 0, fileSize, 0); fs.closeSync(fd); - enableCrc32Check = parseInt("0x" + crc32(buf)).toString(); + fileCrc32 = parseInt("0x" + crc32(buf)).toString(); } - self.uploadWithToken(uploadToken, stream, key, mimeType, customMeta, callbackParams, enableCrc32Check, onret); + self.uploadWithToken(uploadToken, stream, key, mimeType, customMeta, callbackParams, fileCrc32, onret); }); }; diff --git a/lib/util.js b/lib/util.js index a6a086c3..bc81a9dc 100644 --- a/lib/util.js +++ b/lib/util.js @@ -15,7 +15,7 @@ exports.encode = function(v) { return exports.base64ToUrlsafe(encoded); }; -exports.generateActionString = function(bucket, key, mimeType, customMeta, enableCrc32Check) { +exports.generateActionString = function(bucket, key, mimeType, customMeta, crc32) { if (!key) { console.error("Please specify your key!"); return; @@ -28,8 +28,8 @@ exports.generateActionString = function(bucket, key, mimeType, customMeta, enabl if (customMeta !== "") { actionParams += '/meta/' + this.encode(customMeta); } - if (enableCrc32Check) { - actionParams += '/crc32/' + enableCrc32Check; + if ((crc32 !== undefined) && (crc32 !== null) && (crc32 !== "")) { + actionParams += '/crc32/' + crc32; } return actionParams; }