Skip to content

Commit

Permalink
token -> that.token ,add a uptoken option when init uploader
Browse files Browse the repository at this point in the history
  • Loading branch information
SunLn committed Mar 31, 2014
1 parent 6c33b97 commit 9a8f96f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 34 deletions.
33 changes: 19 additions & 14 deletions demo/js/qiniu.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ function QiniuJsSDK() {
}


var token = '';
that.token = '';
var ctx = '';

plupload.extend(option, op, {
Expand All @@ -260,17 +260,22 @@ function QiniuJsSDK() {
});

var uploader = new plupload.Uploader(option);

var getUpToken = function() {
var ajax = that.createAjax();
ajax.open('GET', uptoken_url, true);
ajax.setRequestHeader("If-Modified-Since", "0");
ajax.onreadystatechange = function() {
if (ajax.readyState === 4 && ajax.status === 200) {
var res = that.parseJSON(ajax.responseText);
token = res.uptoken;
}
};
ajax.send();
if (!op.uptoken) {
var ajax = that.createAjax();
ajax.open('GET', uptoken_url, true);
ajax.setRequestHeader("If-Modified-Since", "0");
ajax.onreadystatechange = function() {
if (ajax.readyState === 4 && ajax.status === 200) {
var res = that.parseJSON(ajax.responseText);
that.token = res.uptoken;
}
};
ajax.send();
} else {
that.token = op.uptoken;
}
};

uploader.bind('Init', function(up, params) {
Expand All @@ -297,7 +302,7 @@ function QiniuJsSDK() {
'multipart': true,
'chunk_size': undefined,
'multipart_params': {
'token': token,
'token': that.token,
'key': file.name
}
});
Expand All @@ -315,7 +320,7 @@ function QiniuJsSDK() {
'multipart': false,
'chunk_size': chunk_size,
'headers': {
'Authorization': 'UpToken ' + token
'Authorization': 'UpToken ' + that.token
},
'multipart_params': {}
});
Expand Down Expand Up @@ -426,7 +431,7 @@ function QiniuJsSDK() {
var ajax = that.createAjax();
ajax.open('POST', url, true);
ajax.setRequestHeader('Content-Type', 'text/plain;charset=UTF-8');
ajax.setRequestHeader('Authorization', 'UpToken ' + token);
ajax.setRequestHeader('Authorization', 'UpToken ' + that.token);
ajax.send(ctx);
ajax.onreadystatechange = function() {
if (ajax.readyState === 4) {
Expand Down
59 changes: 39 additions & 20 deletions src/qiniu.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ function QiniuJsSDK() {
}


var token = '';
that.token = '';
var ctx = '';

plupload.extend(option, op, {
Expand All @@ -260,17 +260,22 @@ function QiniuJsSDK() {
});

var uploader = new plupload.Uploader(option);

var getUpToken = function() {
var ajax = that.createAjax();
ajax.open('GET', uptoken_url, true);
ajax.setRequestHeader("If-Modified-Since", "0");
ajax.onreadystatechange = function() {
if (ajax.readyState === 4 && ajax.status === 200) {
var res = that.parseJSON(ajax.responseText);
token = res.uptoken;
}
};
ajax.send();
if (!op.uptoken) {
var ajax = that.createAjax();
ajax.open('GET', uptoken_url, true);
ajax.setRequestHeader("If-Modified-Since", "0");
ajax.onreadystatechange = function() {
if (ajax.readyState === 4 && ajax.status === 200) {
var res = that.parseJSON(ajax.responseText);
that.token = res.uptoken;
}
};
ajax.send();
} else {
that.token = op.uptoken;
}
};

uploader.bind('Init', function(up, params) {
Expand All @@ -297,7 +302,7 @@ function QiniuJsSDK() {
'multipart': true,
'chunk_size': undefined,
'multipart_params': {
'token': token,
'token': that.token,
'key': file.name
}
});
Expand All @@ -315,7 +320,7 @@ function QiniuJsSDK() {
'multipart': false,
'chunk_size': chunk_size,
'headers': {
'Authorization': 'UpToken ' + token
'Authorization': 'UpToken ' + that.token
},
'multipart_params': {}
});
Expand Down Expand Up @@ -355,6 +360,8 @@ function QiniuJsSDK() {
errTip = '文件验证失败。请稍后重试。';
break;
case plupload.HTTP_ERROR:
var errorObj = that.parseJSON(err.response);
var errorText = errorObj.error;
switch (err.status) {
case 400:
errTip = "请求报文格式错误。";
Expand All @@ -373,6 +380,8 @@ function QiniuJsSDK() {
break;
case 614:
errTip = "文件已存在。";
errorObj = that.parseJSON(errorObj.error);
errorText = errorObj.error || 'file exists';
break;
case 631:
errTip = "指定空间不存在。";
Expand All @@ -384,8 +393,7 @@ function QiniuJsSDK() {
errTip = "未知错误。";
break;
}
var errorObj = that.parseJSON(err.response);
errTip = errTip + '(' + err.status + ':' + errorObj.error + ')';
errTip = errTip + '(' + err.status + ':' + errorText + ')';
break;
case plupload.SECURITY_ERROR:
errTip = '安全配置错误。请联系网站管理员。';
Expand All @@ -407,6 +415,8 @@ function QiniuJsSDK() {
if (Error_Handler) {
Error_Handler(up, err, errTip);
}
} else {

}
up.refresh(); // Reposition Flash/Silverlight
};
Expand All @@ -421,13 +431,22 @@ function QiniuJsSDK() {
var ajax = that.createAjax();
ajax.open('POST', url, true);
ajax.setRequestHeader('Content-Type', 'text/plain;charset=UTF-8');
ajax.setRequestHeader('Authorization', 'UpToken ' + token);
ajax.setRequestHeader('Authorization', 'UpToken ' + that.token);
ajax.send(ctx);
ajax.onreadystatechange = function() {
if (ajax.readyState === 4 && ajax.status === 200) {
var info = ajax.responseText;
if (FileUploaded_Handler) {
FileUploaded_Handler(up, file, info);
if (ajax.readyState === 4) {
if (ajax.status === 200) {
var info = ajax.responseText;
if (FileUploaded_Handler) {
FileUploaded_Handler(up, file, info);
}
} else {
uploader.trigger('Error', {
status: ajax.status,
response: ajax.responseText,
file: file,
code: -200
});
}
}
};
Expand Down

0 comments on commit 9a8f96f

Please sign in to comment.