Skip to content

Commit

Permalink
fix crc32 and action string bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ikbear committed Jan 14, 2013
1 parent b46acbf commit c55014b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
16 changes: 12 additions & 4 deletions lib/auth.js
Expand Up @@ -12,12 +12,20 @@ function UploadToken(opts) {


UploadToken.prototype.generateSignature = function() { UploadToken.prototype.generateSignature = function() {
var params = { var params = {
"scope": this.scope,
"deadline": this.expires + Math.floor(Date.now() / 1000), "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) var paramsString = JSON.stringify(params)
return util.encode(paramsString); return util.encode(paramsString);
}; };
Expand Down
13 changes: 7 additions & 6 deletions lib/rs.js
Expand Up @@ -117,17 +117,17 @@ 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流 * 使用upload_token以multipart/form-data形式上传ReadStream流
**/ **/
var bucket = this.bucket; var bucket = this.bucket;
if (!mimeType) { if (!mimeType) {
mimeType = "application/octet-stream"; 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) { if (callbackParams === null) {
callbackParams = { callbackParams = {
"bucket": bucket, "bucket": bucket,
Expand Down Expand Up @@ -165,7 +165,8 @@ Service.prototype.uploadFileWithToken = function(uploadToken, localFile, key, mi
onret({code: -1, error: err.toString(), detail: err}); onret({code: -1, error: err.toString(), detail: err});
return; return;
} }
var stream = fs.createReadStream(localFile); var fileCrc32 = null
, stream = fs.createReadStream(localFile);


if (enableCrc32Check) { if (enableCrc32Check) {
var fileStat = fs.statSync(localFile) var fileStat = fs.statSync(localFile)
Expand All @@ -175,10 +176,10 @@ Service.prototype.uploadFileWithToken = function(uploadToken, localFile, key, mi


fs.readSync(fd, buf, 0, fileSize, 0); fs.readSync(fd, buf, 0, fileSize, 0);
fs.closeSync(fd); 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);
}); });
}; };


Expand Down
6 changes: 3 additions & 3 deletions lib/util.js
Expand Up @@ -15,7 +15,7 @@ exports.encode = function(v) {
return exports.base64ToUrlsafe(encoded); return exports.base64ToUrlsafe(encoded);
}; };


exports.generateActionString = function(bucket, key, mimeType, customMeta, enableCrc32Check) { exports.generateActionString = function(bucket, key, mimeType, customMeta, crc32) {
if (!key) { if (!key) {
console.error("Please specify your key!"); console.error("Please specify your key!");
return; return;
Expand All @@ -28,8 +28,8 @@ exports.generateActionString = function(bucket, key, mimeType, customMeta, enabl
if (customMeta !== "") { if (customMeta !== "") {
actionParams += '/meta/' + this.encode(customMeta); actionParams += '/meta/' + this.encode(customMeta);
} }
if (enableCrc32Check) { if ((crc32 !== undefined) && (crc32 !== null) && (crc32 !== "")) {
actionParams += '/crc32/' + enableCrc32Check; actionParams += '/crc32/' + crc32;
} }
return actionParams; return actionParams;
} }
Expand Down

0 comments on commit c55014b

Please sign in to comment.