Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add enableCrc32Check option to stream upload

  • Loading branch information...
commit 702ac42a5935fa8fc0a52adf2dc6bd8bfd1c92ba 1 parent df3ee62
@ikbear authored
Showing with 42 additions and 61 deletions.
  1. +2 −4 demo/rs_demo.js
  2. +31 −36 lib/rs.js
  3. +9 −21 lib/util.js
View
6 demo/rs_demo.js
@@ -36,10 +36,8 @@ qiniu.rs.mkbucket(conn, bucket, function(resp) {
callbackParams = {},
enableCrc32Check = true;
- var stream = fs.createReadStream(localFile);
- var filename = path.basename(localFile);
- rs.uploadWithToken(uploadToken, filename, stream, key, mimeType, customMeta, callbackParams, function(resp){
- console.log("\n===> Upload Stream with Token result: ", resp);
+ rs.uploadFileWithToken(uploadToken, localFile, key, mimeType, customMeta, callbackParams, enableCrc32Check, function(resp){
+ console.log("\n===> Upload File with Token result: ", resp);
if (resp.code != 200) {
clear(rs);
return;
View
67 lib/rs.js
@@ -1,6 +1,6 @@
var fs = require('fs');
var path = require('path');
-var crc = require('crc');
+var crc32 = require('crc32');
var mime = require('mime');
var formstream = require('formstream');
var config = require('./conf.js');
@@ -84,7 +84,7 @@ Service.prototype.upload = function(upToken, key, mimeType, filename, stream, on
**/
var self = this;
if (!mimeType) {
- mimeType = mime.lookup(filename);
+ mimeType = "application/octet-stream";
}
var entryURI = this.bucket + ':' + key;
entryURI = '/rs-put/' + util.encode(entryURI) + '/mimeType/' + util.encode(mimeType);
@@ -117,16 +117,16 @@ Service.prototype.uploadFile = function(upToken, key, mimeType, localFile, onret
});
};
-Service.prototype.uploadWithToken = function(uploadToken, filename, stream, key, mimeType, customMeta, callbackParams, onret) {
+Service.prototype.uploadWithToken = function(uploadToken, stream, key, mimeType, customMeta, callbackParams, enableCrc32Check, fileCrc32, onret) {
/*
- * func UploadWithToken(uploadToken, filename, stream, key, mimeType, customMeta, callbackParams, enableCrc32Check, onret) => (data PutRet, code int, err Error)
+ * func UploadWithToken(uploadToken, stream, key, mimeType, customMeta, callbackParams, enableCrc32Check, fileCrc32, onret) => (data PutRet, code int, err Error)
* 使用upload_token以multipart/form-data形式上传ReadStream流
**/
var bucket = this.bucket;
- if (mimeType === null) {
- mimeType = mime.lookup(filename);
+ if (!mimeType) {
+ mimeType = "application/octet-stream";
}
- var actionString = util.generateActionString('', bucket, key, mimeType, customMeta, false);
+ var actionString = util.generateActionString(bucket, key, mimeType, customMeta, enableCrc32Check, fileCrc32);
if (callbackParams === null) {
callbackParams = {
"bucket": bucket,
@@ -137,6 +137,7 @@ Service.prototype.uploadWithToken = function(uploadToken, filename, stream, key,
var callbackQueryString = util.generateQueryString(callbackParams);
var url = config.UP_HOST + "/upload";
+ var filename = path.basename(key);
var form = formstream();
form.field('action', actionString);
form.field('params', callbackQueryString);
@@ -153,37 +154,31 @@ Service.prototype.uploadFileWithToken = function(uploadToken, localFile, key, mi
* func UploadFileWithToken(uploadToken, localFile, key, mimeType, customMeta, callbackParams, enableCrc32Check, onret) => (data PutRet, code int, err Error)
* 使用upload_token以multipart/form-data形式上传文件
**/
- var self = this;
+ var self = this
+ , bucket = self.bucket;
if (!mimeType) {
- mimeType = mime.lookup(localFile);
+ mimeType = mime.lookup(localFile);
+ }
+ fs.stat(localFile, function(err, fi) {
+ if (err) {
+ onret({code: -1, error: err.toString(), detail: err});
+ return;
}
- fs.stat(localFile, function(err, fi) {
- if (err) {
- onret({code: -1, error: err.toString(), detail: err});
- return;
- }
- var bucket = self.bucket;
- var filename = path.basename(localFile);
- var stream = fs.createReadStream(localFile);
- var actionString = util.generateActionString(localFile, bucket, key, mimeType, customMeta, enableCrc32Check);
- if (callbackParams === null) {
- callbackParams = {
- "bucket": bucket,
- "key": key,
- "mime_type": mimeType
- };
- }
- var callbackQueryString = util.generateQueryString(callbackParams);
- var form = formstream();
- form.field('action', actionString);
- form.field('params', callbackQueryString);
- form.field('multipart', true);
- form.field('auth', uploadToken);
- form.stream('file', stream, filename, mimeType);
- form = new util.Form(form, form.headers()['Content-Type']);
- var url = config.UP_HOST + "/upload";
- return self.conn.callWithToken(uploadToken, url, form, onret);
- });
+ var fileCrc32 = ''
+ , stream = fs.createReadStream(localFile);
+
+ if (enableCrc32Check) {
+ var fileStat = fs.statSync(localFile)
+ , fileSize = fileStat.size
+ , buf = new Buffer(fileSize)
+ , fd = fs.openSync(localFile, 'r');
+
+ fs.readSync(fd, buf, 0, fileSize, 0);
+ fileCrc32 = parseInt("0x" + crc32(buf)).toString();
+ }
+
+ return self.uploadWithToken(uploadToken, stream, key, mimeType, customMeta, callbackParams, enableCrc32Check, fileCrc32, onret);
+ });
};
Service.prototype.get = function(key, attName, onret) {
View
30 lib/util.js
@@ -2,7 +2,6 @@ var fs = require('fs');
var path = require('path');
var mime = require('mime');
var crypto = require('crypto');
-var crc32 = require('crc32');
// ------------------------------------------------------------------------------------------
// func encode
@@ -16,34 +15,23 @@ exports.encode = function(v) {
return exports.base64ToUrlsafe(encoded);
};
-exports.generateActionString = function(localFile, bucket, key, mimeType, customMeta, enableCrc32Check) {
- var filename = path.basename(localFile);
- if (key == null) {
- var today = new Date();
- key = crypto.createHash('sha1').update(filename+ today.toString()).digest('hex');
+exports.generateActionString = function(bucket, key, mimeType, customMeta, enableCrc32Check, fileCrc32) {
+ if (!key) {
+ console.error("Please specify your key!");
+ return;
}
var entryUri = bucket + ":" + key;
- if (mimeType === "") {
- mimeType = mime.lookup(filename);
- if (mimeType === "") {
- mimeType = "application/octet-stream";
- }
+ if (!mimeType) {
+ mimeType = "application/octet-stream";
}
var actionParams = '/rs-put/' + this.encode(entryUri) + '/mimeType/' + this.encode(mimeType);
if (customMeta !== "") {
actionParams += '/meta/' + this.encode(customMeta);
}
if (enableCrc32Check) {
- if (!fs.existsSync(localFile)) {
- return;
- }
- var fileStat = fs.statSync(localFile);
- var fileSize = fileStat.size;
- var buf = new Buffer(fileSize);
- var fd = fs.openSync(localFile, 'r');
- fs.readSync(fd, buf, 0, fileSize, 0);
- var fileCrc32 = parseInt("0x" + crc32(buf)).toString();
- actionParams += '/crc32/' + fileCrc32;
+ if ((fileCrc32 !== undefined) && (fileCrc32 !== null)) {
+ actionParams += '/crc32/' + fileCrc32;
+ }
}
return actionParams;
}
Please sign in to comment.
Something went wrong with that request. Please try again.