Skip to content

Commit aa85ffc

Browse files
committed
Make localStorage fallback with cookie
- Use cookie to store upload file info when browser is not support localStorage - Fix get_new_token mistake in Init Event
1 parent a3f2388 commit aa85ffc

File tree

4 files changed

+87
-187
lines changed

4 files changed

+87
-187
lines changed

dist/qiniu.js

Lines changed: 43 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* GitHub: http://github.com/qiniu/js-sdk
88
*
9-
* Date: 2015-12-10
9+
* Date: 2015-12-28
1010
*/
1111

1212
/*global plupload ,mOxie*/
@@ -16,106 +16,49 @@
1616

1717
;(function( global ){
1818

19-
// var store = function store(key, value) {
20-
// var lsSupport = false,
21-
// data = null;
22-
23-
// // Check for native support
24-
// if (localStorage) {
25-
// lsSupport = true;
26-
// }
27-
28-
// // If value is detected, set new or modify store
29-
// if (typeof value !== "undefined" && value !== null) {
30-
// // Convert object values to JSON
31-
// if ( typeof value === 'object' ) {
32-
// value = JSON.stringify(value);
33-
// }
34-
// // Set the store
35-
// if (lsSupport) { // Native support
36-
// localStorage.setItem(key, value);
37-
// } else { // Use Cookie
38-
// createCookie(key, value, 30);
39-
// }
40-
// }
41-
42-
// // No value supplied, return value
43-
// if (typeof value === "undefined") {
44-
// // Get value
45-
// if (lsSupport) { // Native support
46-
// data = localStorage.getItem(key);
47-
// } else { // Use cookie
48-
// data = readCookie(key);
49-
// }
50-
51-
// // Try to parse JSON...
52-
// try {
53-
// data = JSON.parse(data);
54-
// }
55-
// catch(e) {
56-
// data = data;
57-
// }
58-
59-
// return data;
60-
61-
// }
62-
63-
// // Null specified, remove store
64-
// if (value === null) {
65-
// if (lsSupport) { // Native support
66-
// localStorage.removeItem(key);
67-
// } else { // Use cookie
68-
// createCookie(key, '', -1);
69-
// }
70-
// }
71-
72-
// /**
73-
// * Creates new cookie or removes cookie with negative expiration
74-
// * @param key The key or identifier for the store
75-
// * @param value Contents of the store
76-
// * @param exp Expiration - creation defaults to 30 days
77-
// */
78-
79-
// function createCookie(key, value, exp) {
80-
// var date = new Date();
81-
// date.setTime(date.getTime() + (exp * 24 * 60 * 60 * 1000));
82-
// var expires = "; expires=" + date.toGMTString();
83-
// document.cookie = key + "=" + value + expires + "; path=/";
84-
// }
85-
86-
// /**
87-
// * Returns contents of cookie
88-
// * @param key The key or identifier for the store
89-
// */
90-
91-
// function readCookie(key) {
92-
// var nameEQ = key + "=";
93-
// var ca = document.cookie.split(';');
94-
// for (var i = 0, max = ca.length; i < max; i++) {
95-
// var c = ca[i];
96-
// while (c.charAt(0) === ' ') {
97-
// c = c.substring(1, c.length);
98-
// }
99-
// if (c.indexOf(nameEQ) === 0) {
100-
// return c.substring(nameEQ.length, c.length);
101-
// }
102-
// }
103-
// return null;
104-
// }
105-
106-
// };
19+
/**
20+
* Creates new cookie or removes cookie with negative expiration
21+
* @param key The key or identifier for the store
22+
* @param value Contents of the store
23+
* @param exp Expiration - creation defaults to 30 days
24+
*/
25+
function createCookie(key, value, exp) {
26+
var date = new Date();
27+
date.setTime(date.getTime() + (exp * 24 * 60 * 60 * 1000));
28+
var expires = "; expires=" + date.toGMTString();
29+
document.cookie = key + "=" + value + expires + "; path=/";
30+
}
31+
32+
/**
33+
* Returns contents of cookie
34+
* @param key The key or identifier for the store
35+
*/
36+
function readCookie(key) {
37+
var nameEQ = key + "=";
38+
var ca = document.cookie.split(';');
39+
for (var i = 0, max = ca.length; i < max; i++) {
40+
var c = ca[i];
41+
while (c.charAt(0) === ' ') {
42+
c = c.substring(1, c.length);
43+
}
44+
if (c.indexOf(nameEQ) === 0) {
45+
return c.substring(nameEQ.length, c.length);
46+
}
47+
}
48+
return null;
49+
}
50+
10751

10852
if ( !window.localStorage ) {
10953
window.localStorage = {
110-
data: {},
11154
setItem: function (key, value) {
112-
localStorage.data[key] = value;
55+
createCookie(key, value, 30);
11356
},
11457
getItem: function (key) {
115-
return localStorage.data[key] || null ;
58+
return readCookie(key);
11659
},
11760
removeItem: function (key) {
118-
localStorage.data[key] = null;
61+
createCookie(key, '', -1);
11962
}
12063
};
12164
}
@@ -651,6 +594,13 @@ function QiniuJsSDK() {
651594
// bind getUpToken to 'Init' event
652595
uploader.bind('Init', function(up, params) {
653596
logger.debug("Init event activated");
597+
// if op.get_new_uptoken is not true
598+
// invoke getUptoken when uploader init
599+
// else
600+
// getUptoken everytime before a new file upload
601+
if(!op.get_new_uptoken){
602+
getUpToken();
603+
}
654604
getUpToken();
655605
});
656606

0 commit comments

Comments
 (0)