From 8efe7c55a7174a30a140d89014e8a99df356e980 Mon Sep 17 00:00:00 2001 From: SunLn Date: Tue, 1 Apr 2014 18:15:23 +0800 Subject: [PATCH 01/19] jshint --- demo/js/main.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/demo/js/main.js b/demo/js/main.js index 5ab255ef..2b8b9494 100644 --- a/demo/js/main.js +++ b/demo/js/main.js @@ -1,3 +1,8 @@ +/*global Qiniu */ +/*global plupload */ +/*global FileProgress */ +/*global hljs */ + var uploader = Qiniu.uploader({ runtimes: 'html5,flash,html4', browse_button: 'pickfiles', @@ -9,6 +14,7 @@ var uploader = Qiniu.uploader({ chunk_size: '4mb', uptoken_url: '/token', domain: 'http://qiniu-plupload.qiniudn.com/', + unique_names: true, auto_start: true, init: { 'FilesAdded': function(up, files) { From be02fc0e01d35385ca38953b28aa577a67cd6644 Mon Sep 17 00:00:00 2001 From: SunLn Date: Tue, 1 Apr 2014 18:18:15 +0800 Subject: [PATCH 02/19] getFileExtension func , getFileKey func --- demo/js/qiniu.js | 91 ++++++++++++++++++++++++++++++------------------ 1 file changed, 57 insertions(+), 34 deletions(-) diff --git a/demo/js/qiniu.js b/demo/js/qiniu.js index fb24bf4e..5fbd4fbb 100644 --- a/demo/js/qiniu.js +++ b/demo/js/qiniu.js @@ -35,6 +35,17 @@ function QiniuJsSDK() { return false; }; + this.getFileExtension = function(filename) { + var tempArr = file.name.split("."); + var ext; + if (tempArr.length === 1 || (tempArr[0] === "" && tempArr.length === 2)) { + ext = ""; + } else { + ext = tempArr.pop().toLowerCase(); //get the extension and make it lower-case + } + return ext; + }; + this.utf8_encode = function(argString) { // http://kevin.vanzonneveld.net // + original by: Webtoolkit.info (http://www.webtoolkit.info/) @@ -277,7 +288,7 @@ function QiniuJsSDK() { that.token = op.uptoken; } }; - + uploader.bind('Init', function(up, params) { getUpToken(); }); @@ -297,46 +308,59 @@ function QiniuJsSDK() { ctx = ''; /////////unique_names with postfix - var filename = file.name; //default key - var tmp = file.name.split("."); //reversal name - var postfix; - if (tmp.length === 1 || (a[0] === "" && a.length === 2)) { - postfix = ""; - } else { - postfix = "." + tmp.pop().toLowerCase(); //get the postfix and make it lower-case - } - var filetype = 'FILE'; //default filetype - var filetypes = { //filetypes list - 'IMG': 'jpg,png,gif,jpeg,bmp', - 'SOUND': 'mp3,mid,wav,flac,ape,mp3pro,wma', - 'VIDEO': 'rmvb,rm,mp4,avi,mov,wmv,mkv,flv,f4v,mpeg-1,mpeg-2,mpeg-4,asf' - } - if (filetypes.IMG.indexOf(postfix) >= 0) { - filetype = 'IMG'; - } else if (filetypes.SOUND.indexOf(postfix) >= 0) { - filetype = 'SOUND'; - } else if (filetypes.VIDEO.indexOf(postfix) >= 0) { - filetype = 'VIDEO'; - } - if (up.getOption('unique_names_postfix')) { - var rand = Math.random(); - rand = Math.round(rand*100000); - var time = new Date(); - filename = filetype + "_" + time.getTime() + rand + postfix; - } + // var filename = file.name; //default key + // var tmp = file.name.split("."); //reversal name + // var postfix; + // if (tmp.length === 1 || (tmp[0] === "" && tmp.length === 2)) { + // postfix = ""; + // } else { + // postfix = "." + tmp.pop().toLowerCase(); //get the postfix and make it lower-case + // } + // var filetype = 'FILE'; //default filetype + // var filetypes = { //filetypes list + // 'IMG': 'jpg,png,gif,jpeg,bmp', + // 'SOUND': 'mp3,mid,wav,flac,ape,mp3pro,wma', + // 'VIDEO': 'rmvb,rm,mp4,avi,mov,wmv,mkv,flv,f4v,mpeg-1,mpeg-2,mpeg-4,asf' + // }; + // if (filetypes.IMG.indexOf(postfix) >= 0) { + // filetype = 'IMG'; + // } else if (filetypes.SOUND.indexOf(postfix) >= 0) { + // filetype = 'SOUND'; + // } else if (filetypes.VIDEO.indexOf(postfix) >= 0) { + // filetype = 'VIDEO'; + // } + // if (up.getOption('unique_names_postfix')) { + // var rand = Math.random(); + // rand = Math.round(rand * 100000); + // var time = new Date(); + // filename = filetype + "_" + time.getTime() + rand + postfix; + // } /////////unique_names with postfix - + function directUpload() { up.setOption({ 'url': 'http://up.qiniu.com/', 'multipart': true, 'chunk_size': undefined, 'multipart_params': { - 'key': filename - 'token': that.token, + 'key': filename, + 'token': that.token } }); } + + var getFileKey = function(up, file, func) { + if (up.getOption('unique_names_postfix')) { + var ext = that.getFileExtension(file); + key = ext ? file.id + '.' + ext : file.id; + } else if (typeof func === 'function') { + key = func(up, file); + } else { + key = file.name; + } + return key; + }; + var chunk_size = up.getOption('chunk_size'); if (uploader.runtime === 'html5' && chunk_size) { @@ -445,8 +469,6 @@ function QiniuJsSDK() { if (Error_Handler) { Error_Handler(up, err, errTip); } - } else { - } up.refresh(); // Reposition Flash/Silverlight }; @@ -457,7 +479,8 @@ function QiniuJsSDK() { var res = that.parseJSON(info.response); ctx = ctx ? ctx : res.ctx; if (ctx) { - var url = 'http://up.qiniu.com/mkfile/' + file.size + '/key/' + that.URLSafeBase64Encode(file.name); + // var url = 'http://up.qiniu.com/mkfile/' + file.size + '/key/' + that.URLSafeBase64Encode(file.name); + var url = 'http://up.qiniu.com/mkfile/' + file.size; var ajax = that.createAjax(); ajax.open('POST', url, true); ajax.setRequestHeader('Content-Type', 'text/plain;charset=UTF-8'); From 5ef453544d1fe66677a6025b7b355e4419d4850e Mon Sep 17 00:00:00 2001 From: SunLn Date: Tue, 1 Apr 2014 18:37:25 +0800 Subject: [PATCH 03/19] multipart_params_obj define in directUpload func --- demo/js/qiniu.js | 49 ++++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/demo/js/qiniu.js b/demo/js/qiniu.js index 5fbd4fbb..dc944326 100644 --- a/demo/js/qiniu.js +++ b/demo/js/qiniu.js @@ -336,36 +336,49 @@ function QiniuJsSDK() { // filename = filetype + "_" + time.getTime() + rand + postfix; // } /////////unique_names with postfix + var getFileKey = function(up, file, func) { + var key = ''; + if (!op.save_key) { + if (up.getOption('unique_names_postfix')) { + var ext = that.getFileExtension(file); + key = ext ? file.id + '.' + ext : file.id; + } else if (typeof func === 'function') { + key = func(up, file); + } else { + key = file.name; + } + } + return key; + }; + + var directUpload = function(up, file, func) { + + var multipart_params_obj; + if (op.save_key) { + multipart_params_obj = { + 'token': that.token + } + } else { + multipart_params_obj = { + 'key': getFileKey(up, file, func), + 'token': that.token + } + } - function directUpload() { up.setOption({ 'url': 'http://up.qiniu.com/', 'multipart': true, 'chunk_size': undefined, - 'multipart_params': { - 'key': filename, - 'token': that.token - } + 'multipart_params': multipart_params_obj }); } - var getFileKey = function(up, file, func) { - if (up.getOption('unique_names_postfix')) { - var ext = that.getFileExtension(file); - key = ext ? file.id + '.' + ext : file.id; - } else if (typeof func === 'function') { - key = func(up, file); - } else { - key = file.name; - } - return key; - }; var chunk_size = up.getOption('chunk_size'); if (uploader.runtime === 'html5' && chunk_size) { if (file.size < chunk_size) { - directUpload(); + directUpload(up, file, func); } else { var blockSize = chunk_size; ctx = ''; @@ -380,7 +393,7 @@ function QiniuJsSDK() { }); } } else { - directUpload(); + directUpload(up, file, func); } }); From 60730220411678817fc1f501c4034ad58256d469 Mon Sep 17 00:00:00 2001 From: SunLn Date: Wed, 2 Apr 2014 13:50:06 +0800 Subject: [PATCH 04/19] update the http request url --- demo/js/qiniu.js | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/demo/js/qiniu.js b/demo/js/qiniu.js index dc944326..8ad25679 100644 --- a/demo/js/qiniu.js +++ b/demo/js/qiniu.js @@ -36,7 +36,7 @@ function QiniuJsSDK() { }; this.getFileExtension = function(filename) { - var tempArr = file.name.split("."); + var tempArr = filename.split("."); var ext; if (tempArr.length === 1 || (tempArr[0] === "" && tempArr.length === 2)) { ext = ""; @@ -288,7 +288,20 @@ function QiniuJsSDK() { that.token = op.uptoken; } }; - + var getFileKey = function(up, file, func) { + var key = ''; + if (!op.save_key) { + if (up.getOption('unique_names_postfix')) { + var ext = that.getFileExtension(file); + key = ext ? file.id + '.' + ext : file.id; + } else if (typeof func === 'function') { + key = func(up, file); + } else { + key = file.name; + } + } + return key; + }; uploader.bind('Init', function(up, params) { getUpToken(); }); @@ -336,20 +349,6 @@ function QiniuJsSDK() { // filename = filetype + "_" + time.getTime() + rand + postfix; // } /////////unique_names with postfix - var getFileKey = function(up, file, func) { - var key = ''; - if (!op.save_key) { - if (up.getOption('unique_names_postfix')) { - var ext = that.getFileExtension(file); - key = ext ? file.id + '.' + ext : file.id; - } else if (typeof func === 'function') { - key = func(up, file); - } else { - key = file.name; - } - } - return key; - }; var directUpload = function(up, file, func) { @@ -357,12 +356,12 @@ function QiniuJsSDK() { if (op.save_key) { multipart_params_obj = { 'token': that.token - } + }; } else { multipart_params_obj = { 'key': getFileKey(up, file, func), 'token': that.token - } + }; } up.setOption({ @@ -371,7 +370,7 @@ function QiniuJsSDK() { 'chunk_size': undefined, 'multipart_params': multipart_params_obj }); - } + }; var chunk_size = up.getOption('chunk_size'); @@ -493,7 +492,13 @@ function QiniuJsSDK() { ctx = ctx ? ctx : res.ctx; if (ctx) { // var url = 'http://up.qiniu.com/mkfile/' + file.size + '/key/' + that.URLSafeBase64Encode(file.name); - var url = 'http://up.qiniu.com/mkfile/' + file.size; + + if (!op.save_key) { + var key = getFileKey(up, file, func); + key = key ? '/key/' + key : ''; + } + + var url = 'http://up.qiniu.com/mkfile/' + file.size + key; var ajax = that.createAjax(); ajax.open('POST', url, true); ajax.setRequestHeader('Content-Type', 'text/plain;charset=UTF-8'); From 6da371c17156d610e5b233e15ba56937e9901bf9 Mon Sep 17 00:00:00 2001 From: SunLn Date: Wed, 2 Apr 2014 22:36:53 +0800 Subject: [PATCH 05/19] remove comment and other --- .DS_Store | Bin 0 -> 6148 bytes demo/js/qiniu.js | 40 +++++----------------------------------- 2 files changed, 5 insertions(+), 35 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..ed0981fee367937bb10ffbea9ad7213e2e617030 GIT binary patch literal 6148 zcmeHKU5nE|6uq;pHZh`E5Y)H4xerCEmR)?>SX)6v5G(G3D>d0f8#ZQ2lUkt`^XTWh z|3LJoh(BP#A5+hKRJ*N$Pa^JIxcA)5+%w4>l9>ztW{4}* z;W!_JsjU7M4$>qq8jSC_&{9U35tHG^Lw19 zKk^{FpTy&yd*(XNiXh42p(H0!jFR1*B#ZdC#Yb6`?{-z=P7)P;IjC;1EZees?%Bzt zdAV8lHm^?W-eha5QTLjgjp@{VX(E7Tqb0^yjK0XIP)kpg0U@ZbV{jio}lbRfth0I-T?X~@M-gWz}?eT}6;^uU;+0u`0%BL-7+w0pYF z*H|i4bYlAO!StP(KA|vqcZ~1Ja$>$h6O95!fx`+^)TJx-|IY8v|A$HD$|ztI_^%XT z)pobt!j$yhx-vPj*Lv_raABgZR46G3dOMbh*ox1=rJ>Ig3DDPADntv+{0K-HOfU-k HRRz8SyOEcx literal 0 HcmV?d00001 diff --git a/demo/js/qiniu.js b/demo/js/qiniu.js index 8ad25679..e27e1fdf 100644 --- a/demo/js/qiniu.js +++ b/demo/js/qiniu.js @@ -221,7 +221,7 @@ function QiniuJsSDK() { var that = this; this.uploader = function(op) { - if (!op.uptoken_url || !op.domain) { + if (!op.domain) { throw 'uptoken_url or domain is required!'; } @@ -237,7 +237,7 @@ function QiniuJsSDK() { op.init.Error = function() {}; op.init.FileUploaded = function() {}; - var uptoken_url = op.uptoken_url; + that.uptoken_url = op.uptoken_url; this.domain = op.domain; var ie = that.detectIEVersion(); if (ie && ie <= 9 && op.chunk_size && op.runtimes.indexOf('flash') >= 0) { @@ -275,7 +275,7 @@ function QiniuJsSDK() { var getUpToken = function() { if (!op.uptoken) { var ajax = that.createAjax(); - ajax.open('GET', uptoken_url, true); + ajax.open('GET', that.uptoken_url, true); ajax.setRequestHeader("If-Modified-Since", "0"); ajax.onreadystatechange = function() { if (ajax.readyState === 4 && ajax.status === 200) { @@ -288,6 +288,7 @@ function QiniuJsSDK() { that.token = op.uptoken; } }; + var getFileKey = function(up, file, func) { var key = ''; if (!op.save_key) { @@ -302,6 +303,7 @@ function QiniuJsSDK() { } return key; }; + uploader.bind('Init', function(up, params) { getUpToken(); }); @@ -320,36 +322,6 @@ function QiniuJsSDK() { ctx = ''; - /////////unique_names with postfix - // var filename = file.name; //default key - // var tmp = file.name.split("."); //reversal name - // var postfix; - // if (tmp.length === 1 || (tmp[0] === "" && tmp.length === 2)) { - // postfix = ""; - // } else { - // postfix = "." + tmp.pop().toLowerCase(); //get the postfix and make it lower-case - // } - // var filetype = 'FILE'; //default filetype - // var filetypes = { //filetypes list - // 'IMG': 'jpg,png,gif,jpeg,bmp', - // 'SOUND': 'mp3,mid,wav,flac,ape,mp3pro,wma', - // 'VIDEO': 'rmvb,rm,mp4,avi,mov,wmv,mkv,flv,f4v,mpeg-1,mpeg-2,mpeg-4,asf' - // }; - // if (filetypes.IMG.indexOf(postfix) >= 0) { - // filetype = 'IMG'; - // } else if (filetypes.SOUND.indexOf(postfix) >= 0) { - // filetype = 'SOUND'; - // } else if (filetypes.VIDEO.indexOf(postfix) >= 0) { - // filetype = 'VIDEO'; - // } - // if (up.getOption('unique_names_postfix')) { - // var rand = Math.random(); - // rand = Math.round(rand * 100000); - // var time = new Date(); - // filename = filetype + "_" + time.getTime() + rand + postfix; - // } - /////////unique_names with postfix - var directUpload = function(up, file, func) { var multipart_params_obj; @@ -491,8 +463,6 @@ function QiniuJsSDK() { var res = that.parseJSON(info.response); ctx = ctx ? ctx : res.ctx; if (ctx) { - // var url = 'http://up.qiniu.com/mkfile/' + file.size + '/key/' + that.URLSafeBase64Encode(file.name); - if (!op.save_key) { var key = getFileKey(up, file, func); key = key ? '/key/' + key : ''; From 0893be3c80e484c6cd44ea8e28a053332cd3a6d4 Mon Sep 17 00:00:00 2001 From: SunLn Date: Wed, 2 Apr 2014 22:42:23 +0800 Subject: [PATCH 06/19] add a func named reset_chunk_size --- demo/js/qiniu.js | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/demo/js/qiniu.js b/demo/js/qiniu.js index e27e1fdf..c2b207d2 100644 --- a/demo/js/qiniu.js +++ b/demo/js/qiniu.js @@ -220,6 +220,7 @@ function QiniuJsSDK() { //Todo ie7 handler / this.parseJSON bug; var that = this; + this.uploader = function(op) { if (!op.domain) { throw 'uptoken_url or domain is required!'; @@ -238,30 +239,32 @@ function QiniuJsSDK() { op.init.FileUploaded = function() {}; that.uptoken_url = op.uptoken_url; + that.token = ''; this.domain = op.domain; - var ie = that.detectIEVersion(); - if (ie && ie <= 9 && op.chunk_size && op.runtimes.indexOf('flash') >= 0) { - /* + var ctx = ''; + + var reset_chunk_size = function() { + var ie = that.detectIEVersion(); + if (ie && ie <= 9 && op.chunk_size && op.runtimes.indexOf('flash') >= 0) { + /* link: http://www.plupload.com/docs/Frequently-Asked-Questions#when-to-use-chunking-and-when-not when plupload chunk_size setting is't null ,it cause bug in ie8/9 which runs flash runtimes (not support html5) . */ - op.chunk_size = 0; + op.chunk_size = 0; - } else { - var BLOCK_BITS = 20; - var MAX_CHUNK_SIZE = 4 << BLOCK_BITS; //4M + } else { + var BLOCK_BITS = 20; + var MAX_CHUNK_SIZE = 4 << BLOCK_BITS; //4M - var chunk_size = plupload.parseSize(op.chunk_size); - if (chunk_size > MAX_CHUNK_SIZE) { - op.chunk_size = MAX_CHUNK_SIZE; + var chunk_size = plupload.parseSize(op.chunk_size); + if (chunk_size > MAX_CHUNK_SIZE) { + op.chunk_size = MAX_CHUNK_SIZE; + } + //qiniu service max_chunk_size is 4m + //reset chunk_size to max_chunk_size(4m) when chunk_size > 4m } - //qiniu service max_chunk_size is 4m - //reset chunk_size to max_chunk_size(4m) when chunk_size > 4m } - - - that.token = ''; - var ctx = ''; + reset_chunk_size(); plupload.extend(option, op, { url: 'http://up.qiniu.com', From 7a8cde2e8867f66ee36a692305d53ecc53e76f48 Mon Sep 17 00:00:00 2001 From: SunLn Date: Wed, 2 Apr 2014 22:50:17 +0800 Subject: [PATCH 07/19] get key_handler form op --- demo/js/qiniu.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/demo/js/qiniu.js b/demo/js/qiniu.js index c2b207d2..d551b97a 100644 --- a/demo/js/qiniu.js +++ b/demo/js/qiniu.js @@ -240,6 +240,7 @@ function QiniuJsSDK() { that.uptoken_url = op.uptoken_url; that.token = ''; + that.key_handler = typeof op.key_handler === 'function' ? op.key_handler : ''; this.domain = op.domain; var ctx = ''; @@ -266,15 +267,6 @@ function QiniuJsSDK() { } reset_chunk_size(); - plupload.extend(option, op, { - url: 'http://up.qiniu.com', - multipart_params: { - token: '' - } - }); - - var uploader = new plupload.Uploader(option); - var getUpToken = function() { if (!op.uptoken) { var ajax = that.createAjax(); @@ -307,6 +299,15 @@ function QiniuJsSDK() { return key; }; + plupload.extend(option, op, { + url: 'http://up.qiniu.com', + multipart_params: { + token: '' + } + }); + + var uploader = new plupload.Uploader(option); + uploader.bind('Init', function(up, params) { getUpToken(); }); @@ -352,7 +353,7 @@ function QiniuJsSDK() { if (uploader.runtime === 'html5' && chunk_size) { if (file.size < chunk_size) { - directUpload(up, file, func); + directUpload(up, file, that.key_handler); } else { var blockSize = chunk_size; ctx = ''; @@ -367,7 +368,7 @@ function QiniuJsSDK() { }); } } else { - directUpload(up, file, func); + directUpload(up, file, that.key_handler); } }); From 1e6259b064687e3bdba5e7834374f43232ae4a5e Mon Sep 17 00:00:00 2001 From: SunLn Date: Wed, 2 Apr 2014 22:53:46 +0800 Subject: [PATCH 08/19] add try-catch when parseJSON in service 614 becasue of json format is not always same --- demo/js/qiniu.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/demo/js/qiniu.js b/demo/js/qiniu.js index d551b97a..4fd63ab4 100644 --- a/demo/js/qiniu.js +++ b/demo/js/qiniu.js @@ -422,8 +422,12 @@ function QiniuJsSDK() { break; case 614: errTip = "文件已存在。"; - errorObj = that.parseJSON(errorObj.error); - errorText = errorObj.error || 'file exists'; + try { + errorObj = that.parseJSON(errorObj.error); + errorText = errorObj.error || 'file exists'; + } catch (e) { + console.log(e); + } break; case 631: errTip = "指定空间不存在。"; From 00f1ae2d10b2d8cc17b179c937928ba8f922aa00 Mon Sep 17 00:00:00 2001 From: SunLn Date: Wed, 2 Apr 2014 22:57:24 +0800 Subject: [PATCH 09/19] asyc qiniu.js in src folder --- src/qiniu.js | 160 +++++++++++++++++++++++++++++---------------------- 1 file changed, 92 insertions(+), 68 deletions(-) diff --git a/src/qiniu.js b/src/qiniu.js index 33147ad9..298ff400 100644 --- a/src/qiniu.js +++ b/src/qiniu.js @@ -35,6 +35,17 @@ function QiniuJsSDK() { return false; }; + this.getFileExtension = function(filename) { + var tempArr = filename.split("."); + var ext; + if (tempArr.length === 1 || (tempArr[0] === "" && tempArr.length === 2)) { + ext = ""; + } else { + ext = tempArr.pop().toLowerCase(); //get the extension and make it lower-case + } + return ext; + }; + this.utf8_encode = function(argString) { // http://kevin.vanzonneveld.net // + original by: Webtoolkit.info (http://www.webtoolkit.info/) @@ -209,8 +220,9 @@ function QiniuJsSDK() { //Todo ie7 handler / this.parseJSON bug; var that = this; + this.uploader = function(op) { - if (!op.uptoken_url || !op.domain) { + if (!op.domain) { throw 'uptoken_url or domain is required!'; } @@ -226,45 +238,39 @@ function QiniuJsSDK() { op.init.Error = function() {}; op.init.FileUploaded = function() {}; - var uptoken_url = op.uptoken_url; + that.uptoken_url = op.uptoken_url; + that.token = ''; + that.key_handler = typeof op.key_handler === 'function' ? op.key_handler : ''; this.domain = op.domain; - var ie = that.detectIEVersion(); - if (ie && ie <= 9 && op.chunk_size && op.runtimes.indexOf('flash') >= 0) { - /* + var ctx = ''; + + var reset_chunk_size = function() { + var ie = that.detectIEVersion(); + if (ie && ie <= 9 && op.chunk_size && op.runtimes.indexOf('flash') >= 0) { + /* link: http://www.plupload.com/docs/Frequently-Asked-Questions#when-to-use-chunking-and-when-not when plupload chunk_size setting is't null ,it cause bug in ie8/9 which runs flash runtimes (not support html5) . */ - op.chunk_size = 0; + op.chunk_size = 0; - } else { - var BLOCK_BITS = 20; - var MAX_CHUNK_SIZE = 4 << BLOCK_BITS; //4M + } else { + var BLOCK_BITS = 20; + var MAX_CHUNK_SIZE = 4 << BLOCK_BITS; //4M - var chunk_size = plupload.parseSize(op.chunk_size); - if (chunk_size > MAX_CHUNK_SIZE) { - op.chunk_size = MAX_CHUNK_SIZE; + var chunk_size = plupload.parseSize(op.chunk_size); + if (chunk_size > MAX_CHUNK_SIZE) { + op.chunk_size = MAX_CHUNK_SIZE; + } + //qiniu service max_chunk_size is 4m + //reset chunk_size to max_chunk_size(4m) when chunk_size > 4m } - //qiniu service max_chunk_size is 4m - //reset chunk_size to max_chunk_size(4m) when chunk_size > 4m } - - - that.token = ''; - var ctx = ''; - - plupload.extend(option, op, { - url: 'http://up.qiniu.com', - multipart_params: { - token: '' - } - }); - - var uploader = new plupload.Uploader(option); + reset_chunk_size(); var getUpToken = function() { if (!op.uptoken) { var ajax = that.createAjax(); - ajax.open('GET', uptoken_url, true); + ajax.open('GET', that.uptoken_url, true); ajax.setRequestHeader("If-Modified-Since", "0"); ajax.onreadystatechange = function() { if (ajax.readyState === 4 && ajax.status === 200) { @@ -277,7 +283,31 @@ function QiniuJsSDK() { that.token = op.uptoken; } }; - + + var getFileKey = function(up, file, func) { + var key = ''; + if (!op.save_key) { + if (up.getOption('unique_names_postfix')) { + var ext = that.getFileExtension(file); + key = ext ? file.id + '.' + ext : file.id; + } else if (typeof func === 'function') { + key = func(up, file); + } else { + key = file.name; + } + } + return key; + }; + + plupload.extend(option, op, { + url: 'http://up.qiniu.com', + multipart_params: { + token: '' + } + }); + + var uploader = new plupload.Uploader(option); + uploader.bind('Init', function(up, params) { getUpToken(); }); @@ -296,47 +326,34 @@ function QiniuJsSDK() { ctx = ''; - /////////unique_names with postfix - var filename = file.name; //default key - var tmp = file.name.split("").reverse().join(""); //reversal name - var postfix = tmp.split('.')[0].split("").reverse().join("").toLowerCase(); //get the postfix and make it lower-case - var filetype = 'FILE'; //default filetype - var filetypes = { //filetypes list - 'IMG': 'jpg,png,gif,jpeg,bmp', - 'SOUND': 'mp3,mid,wav,flac,ape,mp3pro,wma', - 'VIDEO': 'rmvb,rm,mp4,avi,mov,wmv,mkv,flv,f4v,mpeg-1,mpeg-2,mpeg-4,asf' - } - if (filetypes.IMG.indexOf(postfix) >= 0) { - filetype = 'IMG'; - } else if (filetypes.SOUND.indexOf(postfix) >= 0) { - filetype = 'SOUND'; - } else if (filetypes.VIDEO.indexOf(postfix) >= 0) { - filetype = 'VIDEO'; - } - if (up.getOption('unique_names_postfix')) { - rand = Math.random(); - rand = Math.round(rand*100000); - var time = new Date(); - filename = filetype + "_" + time.getTime() + rand + "." + postfix; - } - /////////unique_names with postfix - - function directUpload() { + var directUpload = function(up, file, func) { + + var multipart_params_obj; + if (op.save_key) { + multipart_params_obj = { + 'token': that.token + }; + } else { + multipart_params_obj = { + 'key': getFileKey(up, file, func), + 'token': that.token + }; + } + up.setOption({ 'url': 'http://up.qiniu.com/', 'multipart': true, 'chunk_size': undefined, - 'multipart_params': { - 'key': filename - 'token': that.token, - } + 'multipart_params': multipart_params_obj }); - } + }; + + var chunk_size = up.getOption('chunk_size'); if (uploader.runtime === 'html5' && chunk_size) { if (file.size < chunk_size) { - directUpload(); + directUpload(up, file, that.key_handler); } else { var blockSize = chunk_size; ctx = ''; @@ -351,7 +368,7 @@ function QiniuJsSDK() { }); } } else { - directUpload(); + directUpload(up, file, that.key_handler); } }); @@ -405,8 +422,12 @@ function QiniuJsSDK() { break; case 614: errTip = "文件已存在。"; - errorObj = that.parseJSON(errorObj.error); - errorText = errorObj.error || 'file exists'; + try { + errorObj = that.parseJSON(errorObj.error); + errorText = errorObj.error || 'file exists'; + } catch (e) { + console.log(e); + } break; case 631: errTip = "指定空间不存在。"; @@ -440,8 +461,6 @@ function QiniuJsSDK() { if (Error_Handler) { Error_Handler(up, err, errTip); } - } else { - } up.refresh(); // Reposition Flash/Silverlight }; @@ -452,7 +471,12 @@ function QiniuJsSDK() { var res = that.parseJSON(info.response); ctx = ctx ? ctx : res.ctx; if (ctx) { - var url = 'http://up.qiniu.com/mkfile/' + file.size + '/key/' + that.URLSafeBase64Encode(file.name); + if (!op.save_key) { + var key = getFileKey(up, file, func); + key = key ? '/key/' + key : ''; + } + + var url = 'http://up.qiniu.com/mkfile/' + file.size + key; var ajax = that.createAjax(); ajax.open('POST', url, true); ajax.setRequestHeader('Content-Type', 'text/plain;charset=UTF-8'); @@ -695,4 +719,4 @@ function QiniuJsSDK() { } -var Qiniu = new QiniuJsSDK(); +var Qiniu = new QiniuJsSDK(); \ No newline at end of file From 05b274547bd738cbd387b316baa0330eb7a54655 Mon Sep 17 00:00:00 2001 From: SunLn Date: Thu, 3 Apr 2014 15:46:23 +0800 Subject: [PATCH 10/19] update useage --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index a54dd633..e10bbbf5 100644 --- a/README.md +++ b/README.md @@ -278,6 +278,10 @@ qiniu-js-sdk * [安装nodejs](http://nodejs.org/download/) +* [安装npm](https://www.npmjs.org/) + +* 安装七牛 Node.js SDK、express `npm install` + * 获取源代码: `git clone git@github.com:qiniupd/qiniu-js-sdk.git` * 进入`demo`目录,修改`config.js`,`Access Key`和`Secret Key` 按如下方式获取 From 9a7a4a5ff8cc104f2f21bc5b4358fd50268740a0 Mon Sep 17 00:00:00 2001 From: SunLn Date: Thu, 3 Apr 2014 15:58:20 +0800 Subject: [PATCH 11/19] URLSafeBase64Encode key when uploaded a file --- demo/js/qiniu.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/demo/js/qiniu.js b/demo/js/qiniu.js index 4fd63ab4..fd45440b 100644 --- a/demo/js/qiniu.js +++ b/demo/js/qiniu.js @@ -472,8 +472,9 @@ function QiniuJsSDK() { ctx = ctx ? ctx : res.ctx; if (ctx) { if (!op.save_key) { - var key = getFileKey(up, file, func); - key = key ? '/key/' + key : ''; + var key = getFileKey(up, file, that.key_handler); + console.log(key); + key = key ? '/key/' + that.URLSafeBase64Encode(key) : ''; } var url = 'http://up.qiniu.com/mkfile/' + file.size + key; From 30c9f8efcc1d03ca96d743468ac2da949bb58272 Mon Sep 17 00:00:00 2001 From: SunLn Date: Thu, 3 Apr 2014 16:02:04 +0800 Subject: [PATCH 12/19] unique_names_postfix -> unique_names --- demo/js/qiniu.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demo/js/qiniu.js b/demo/js/qiniu.js index fd45440b..5c7c4bb6 100644 --- a/demo/js/qiniu.js +++ b/demo/js/qiniu.js @@ -287,7 +287,7 @@ function QiniuJsSDK() { var getFileKey = function(up, file, func) { var key = ''; if (!op.save_key) { - if (up.getOption('unique_names_postfix')) { + if (up.getOption('unique_names')) { var ext = that.getFileExtension(file); key = ext ? file.id + '.' + ext : file.id; } else if (typeof func === 'function') { From a231012a5c7e0f7b47ff7f04ccf8a93878fb477d Mon Sep 17 00:00:00 2001 From: SunLn Date: Thu, 3 Apr 2014 16:02:59 +0800 Subject: [PATCH 13/19] file -> file.name --- demo/js/qiniu.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demo/js/qiniu.js b/demo/js/qiniu.js index 5c7c4bb6..be0b4755 100644 --- a/demo/js/qiniu.js +++ b/demo/js/qiniu.js @@ -288,7 +288,7 @@ function QiniuJsSDK() { var key = ''; if (!op.save_key) { if (up.getOption('unique_names')) { - var ext = that.getFileExtension(file); + var ext = that.getFileExtension(file.name); key = ext ? file.id + '.' + ext : file.id; } else if (typeof func === 'function') { key = func(up, file); From 18eebc3e96cf08e76c80db6626ac91e5ccb27424 Mon Sep 17 00:00:00 2001 From: SunLn Date: Thu, 3 Apr 2014 16:48:39 +0800 Subject: [PATCH 14/19] get key handler from op.init --- demo/js/qiniu.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/demo/js/qiniu.js b/demo/js/qiniu.js index be0b4755..9af885fa 100644 --- a/demo/js/qiniu.js +++ b/demo/js/qiniu.js @@ -240,29 +240,28 @@ function QiniuJsSDK() { that.uptoken_url = op.uptoken_url; that.token = ''; - that.key_handler = typeof op.key_handler === 'function' ? op.key_handler : ''; + that.key_handler = typeof op.init.KeyHandler === 'function' ? op.init.KeyHandler : ''; this.domain = op.domain; var ctx = ''; var reset_chunk_size = function() { var ie = that.detectIEVersion(); + var BLOCK_BITS, MAX_CHUNK_SIZE, chunk_size; if (ie && ie <= 9 && op.chunk_size && op.runtimes.indexOf('flash') >= 0) { - /* - link: http://www.plupload.com/docs/Frequently-Asked-Questions#when-to-use-chunking-and-when-not - when plupload chunk_size setting is't null ,it cause bug in ie8/9 which runs flash runtimes (not support html5) . - */ + // link: http://www.plupload.com/docs/Frequently-Asked-Questions#when-to-use-chunking-and-when-not + // when plupload chunk_size setting is't null ,it cause bug in ie8/9 which runs flash runtimes (not support html5) . op.chunk_size = 0; } else { - var BLOCK_BITS = 20; - var MAX_CHUNK_SIZE = 4 << BLOCK_BITS; //4M + BLOCK_BITS = 20; + MAX_CHUNK_SIZE = 4 << BLOCK_BITS; //4M - var chunk_size = plupload.parseSize(op.chunk_size); + chunk_size = plupload.parseSize(op.chunk_size); if (chunk_size > MAX_CHUNK_SIZE) { op.chunk_size = MAX_CHUNK_SIZE; } - //qiniu service max_chunk_size is 4m - //reset chunk_size to max_chunk_size(4m) when chunk_size > 4m + // qiniu service max_chunk_size is 4m + // reset chunk_size to max_chunk_size(4m) when chunk_size > 4m } } reset_chunk_size(); @@ -471,9 +470,10 @@ function QiniuJsSDK() { var res = that.parseJSON(info.response); ctx = ctx ? ctx : res.ctx; if (ctx) { + var key = ''; + if (!op.save_key) { - var key = getFileKey(up, file, that.key_handler); - console.log(key); + key = getFileKey(up, file, that.key_handler); key = key ? '/key/' + that.URLSafeBase64Encode(key) : ''; } @@ -720,4 +720,4 @@ function QiniuJsSDK() { } -var Qiniu = new QiniuJsSDK(); +var Qiniu = new QiniuJsSDK(); \ No newline at end of file From 213e62668cd4ae3cf1ed4cd8a129592e6a7ad7e8 Mon Sep 17 00:00:00 2001 From: SunLn Date: Thu, 3 Apr 2014 17:15:27 +0800 Subject: [PATCH 15/19] update the usage of sdk --- README.md | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e10bbbf5..55f5c140 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,8 @@ qiniu-js-sdk browse_button: 'pickfiles', //上传选择的点选按钮,**必需** uptoken_url: '/token', //Ajax请求upToken的Url,**强烈建议设置**(服务端提供) // uptoken : '', //若你未指定uptoken_url,则必须指定 uptoken ,uptoken由其他程序生成 + // unique_names: true, // 默认 false,key为文件名。若开启该选项,SDK为自动生成上传成功后的key(文件名)。 + // save_key: true, // 默认 false。若在服务端生成uptoken的上传策略中指定了 `sava_key`,则开启,SDK会忽略对key的处理 domain: 'http://qiniu-plupload.qiniudn.com/', //bucket 域名,下载资源时用到,**必需** container: 'container', //上传区域DOM ID,默认是browser_button的父元素, max_file_size: '100mb', //最大文件体积限制 @@ -73,18 +75,18 @@ qiniu-js-sdk init: { 'FilesAdded': function(up, files) { plupload.each(files, function(file) { - //文件添加进队列后,处理相关的事情 + // 文件添加进队列后,处理相关的事情 }); }, 'BeforeUpload': function(up, file) { - //每个文件上传前,处理相关的事情 + // 每个文件上传前,处理相关的事情 }, 'UploadProgress': function(up, file) { - //每个文件上传时,处理相关的事情 + // 每个文件上传时,处理相关的事情 }, 'FileUploaded': function(up, file, info) { - //每个文件上传成功后,处理相关的事情 - //其中 info 是文件上传成功后,服务端返回的json,形式如 + // 每个文件上传成功后,处理相关的事情 + // 其中 info 是文件上传成功后,服务端返回的json,形式如 // { // "hash": "Fh8xVqod2MQ1mocfI4S4KpRL6D98", // "key": "gogopher.jpg" @@ -100,6 +102,14 @@ qiniu-js-sdk }, 'UploadComplete': function() { //队列文件处理完毕后,处理相关的事情 + }, + 'Key': function(up, file) { + // 若想在前端对每个文件的key进行个性化处理,可以配置该函数 + // 该配置必须要在 unique_names: false , save_key: false 时才生效 + + var key = ""; + // do something with key here + return key } } }); @@ -307,7 +317,7 @@ qiniu-js-sdk 1. 本SDK依赖Plupload,初始化之前请引入Plupload插件 -2. 本SDK依赖服务端颁发uptoken,必须构建后端服务 +2. 本SDK依赖服务端颁发uptoken,可以提供Ajax请求地址 `uptoken_url` 或者直接设置 `uptoken` 实现,建议前者。 3. 如果您想了解更多七牛的上传策略,建议您仔细阅读 [七牛官方文档-上传](http://developer.qiniu.com/docs/v6/api/reference/up/) From 81cb7de40e39b4ed3fe89f041f2d17688a843ae0 Mon Sep 17 00:00:00 2001 From: SunLn Date: Thu, 3 Apr 2014 17:16:04 +0800 Subject: [PATCH 16/19] KeyHandler -> Key --- demo/js/qiniu.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demo/js/qiniu.js b/demo/js/qiniu.js index 9af885fa..4d2f98b3 100644 --- a/demo/js/qiniu.js +++ b/demo/js/qiniu.js @@ -240,7 +240,7 @@ function QiniuJsSDK() { that.uptoken_url = op.uptoken_url; that.token = ''; - that.key_handler = typeof op.init.KeyHandler === 'function' ? op.init.KeyHandler : ''; + that.key_handler = typeof op.init.Key === 'function' ? op.init.Key : ''; this.domain = op.domain; var ctx = ''; From 51faac76ac109679f5f1abdc0b9bb9f7cd282a73 Mon Sep 17 00:00:00 2001 From: SunLn Date: Thu, 3 Apr 2014 17:20:15 +0800 Subject: [PATCH 17/19] usage in main.js update --- demo/js/main.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/demo/js/main.js b/demo/js/main.js index 2b8b9494..a80861ca 100644 --- a/demo/js/main.js +++ b/demo/js/main.js @@ -14,7 +14,8 @@ var uploader = Qiniu.uploader({ chunk_size: '4mb', uptoken_url: '/token', domain: 'http://qiniu-plupload.qiniudn.com/', - unique_names: true, + // unique_names: true, + // save_key: true, auto_start: true, init: { 'FilesAdded': function(up, files) { @@ -51,6 +52,12 @@ var uploader = Qiniu.uploader({ progress.setError(); progress.setStatus(errTip); } + // , + // 'Key': function(up, file) { + // var key = ""; + // // do something with key + // return key + // } } }); @@ -209,4 +216,4 @@ $(function() { return false; }); -}); +}); \ No newline at end of file From bd151e28c0827419dbc283b18084a9cb48073dec Mon Sep 17 00:00:00 2001 From: SunLn Date: Thu, 3 Apr 2014 17:23:47 +0800 Subject: [PATCH 18/19] update the demo code in index.html --- README.md | 2 +- demo/index.html | 35 +++++++++++++++++------------------ 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 55f5c140..27d8e283 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ qiniu-js-sdk runtimes: 'html5,flash,html4', //上传模式,依次退化 browse_button: 'pickfiles', //上传选择的点选按钮,**必需** uptoken_url: '/token', //Ajax请求upToken的Url,**强烈建议设置**(服务端提供) - // uptoken : '', //若你未指定uptoken_url,则必须指定 uptoken ,uptoken由其他程序生成 + // uptoken : '', //若未指定uptoken_url,则必须指定 uptoken ,uptoken由其他程序生成 // unique_names: true, // 默认 false,key为文件名。若开启该选项,SDK为自动生成上传成功后的key(文件名)。 // save_key: true, // 默认 false。若在服务端生成uptoken的上传策略中指定了 `sava_key`,则开启,SDK会忽略对key的处理 domain: 'http://qiniu-plupload.qiniudn.com/', //bucket 域名,下载资源时用到,**必需** diff --git a/demo/index.html b/demo/index.html index 17b7a76c..198c751d 100644 --- a/demo/index.html +++ b/demo/index.html @@ -70,18 +70,6 @@

- @@ -100,7 +88,10 @@

var uploader = Qiniu.uploader({ runtimes: 'html5,flash,html4', //上传模式,依次退化 browse_button: 'pickfiles', //上传选择的点选按钮,**必需** - uptoken_url: '/token', //Ajax请求upToken的Url,**必需**(服务端提供) + uptoken_url: '/token', //Ajax请求upToken的Url,**强烈建议设置**(服务端提供) + // uptoken : '', //若未指定uptoken_url,则必须指定 uptoken ,uptoken由其他程序生成 + // unique_names: true, // 默认 false,key为文件名。若开启该选项,SDK为自动生成上传成功后的key(文件名)。 + // save_key: true, // 默认 false。若在服务端生成uptoken的上传策略中指定了 `sava_key`,则开启,SDK会忽略对key的处理 domain: 'http://qiniu-plupload.qiniudn.com/', //bucket 域名,下载资源时用到,**必需** container: 'container', //上传区域DOM ID,默认是browser_button的父元素, max_file_size: '100mb', //最大文件体积限制 @@ -113,18 +104,18 @@ init: { 'FilesAdded': function(up, files) { plupload.each(files, function(file) { - //文件添加进队列后,处理相关的事情 + // 文件添加进队列后,处理相关的事情 }); }, 'BeforeUpload': function(up, file) { - //每个文件上传前,处理相关的事情 + // 每个文件上传前,处理相关的事情 }, 'UploadProgress': function(up, file) { - //每个文件上传时,处理相关的事情 + // 每个文件上传时,处理相关的事情 }, 'FileUploaded': function(up, file, info) { - //每个文件上传成功后,处理相关的事情 - //其中 info 是文件上传成功后,服务端返回的json,形式如 + // 每个文件上传成功后,处理相关的事情 + // 其中 info 是文件上传成功后,服务端返回的json,形式如 // { // "hash": "Fh8xVqod2MQ1mocfI4S4KpRL6D98", // "key": "gogopher.jpg" @@ -140,6 +131,14 @@ }, 'UploadComplete': function() { //队列文件处理完毕后,处理相关的事情 + }, + 'Key': function(up, file) { + // 若想在前端对每个文件的key进行个性化处理,可以配置该函数 + // 该配置必须要在 unique_names: false , save_key: false 时才生效 + + var key = ""; + // do something with key here + return key } } }); From 5e395b987cd38262b1a50081916546a7d8606505 Mon Sep 17 00:00:00 2001 From: SunLn Date: Thu, 3 Apr 2014 17:25:18 +0800 Subject: [PATCH 19/19] sync qiniu.js in src folder --- src/qiniu.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/qiniu.js b/src/qiniu.js index 298ff400..4d2f98b3 100644 --- a/src/qiniu.js +++ b/src/qiniu.js @@ -240,29 +240,28 @@ function QiniuJsSDK() { that.uptoken_url = op.uptoken_url; that.token = ''; - that.key_handler = typeof op.key_handler === 'function' ? op.key_handler : ''; + that.key_handler = typeof op.init.Key === 'function' ? op.init.Key : ''; this.domain = op.domain; var ctx = ''; var reset_chunk_size = function() { var ie = that.detectIEVersion(); + var BLOCK_BITS, MAX_CHUNK_SIZE, chunk_size; if (ie && ie <= 9 && op.chunk_size && op.runtimes.indexOf('flash') >= 0) { - /* - link: http://www.plupload.com/docs/Frequently-Asked-Questions#when-to-use-chunking-and-when-not - when plupload chunk_size setting is't null ,it cause bug in ie8/9 which runs flash runtimes (not support html5) . - */ + // link: http://www.plupload.com/docs/Frequently-Asked-Questions#when-to-use-chunking-and-when-not + // when plupload chunk_size setting is't null ,it cause bug in ie8/9 which runs flash runtimes (not support html5) . op.chunk_size = 0; } else { - var BLOCK_BITS = 20; - var MAX_CHUNK_SIZE = 4 << BLOCK_BITS; //4M + BLOCK_BITS = 20; + MAX_CHUNK_SIZE = 4 << BLOCK_BITS; //4M - var chunk_size = plupload.parseSize(op.chunk_size); + chunk_size = plupload.parseSize(op.chunk_size); if (chunk_size > MAX_CHUNK_SIZE) { op.chunk_size = MAX_CHUNK_SIZE; } - //qiniu service max_chunk_size is 4m - //reset chunk_size to max_chunk_size(4m) when chunk_size > 4m + // qiniu service max_chunk_size is 4m + // reset chunk_size to max_chunk_size(4m) when chunk_size > 4m } } reset_chunk_size(); @@ -287,8 +286,8 @@ function QiniuJsSDK() { var getFileKey = function(up, file, func) { var key = ''; if (!op.save_key) { - if (up.getOption('unique_names_postfix')) { - var ext = that.getFileExtension(file); + if (up.getOption('unique_names')) { + var ext = that.getFileExtension(file.name); key = ext ? file.id + '.' + ext : file.id; } else if (typeof func === 'function') { key = func(up, file); @@ -471,9 +470,11 @@ function QiniuJsSDK() { var res = that.parseJSON(info.response); ctx = ctx ? ctx : res.ctx; if (ctx) { + var key = ''; + if (!op.save_key) { - var key = getFileKey(up, file, func); - key = key ? '/key/' + key : ''; + key = getFileKey(up, file, that.key_handler); + key = key ? '/key/' + that.URLSafeBase64Encode(key) : ''; } var url = 'http://up.qiniu.com/mkfile/' + file.size + key;