Skip to content

Commit b0a296b

Browse files
winddiesnighca
winddies
authored andcommitted
当region不设置时自动分析上传区域 (#346)
* 当region不设置时自动分析上传区域 * 直传进度达到100时请求并未完成的bug * demo里关于图像处理的参数部分去除format参数 * config.json.example格式单引号改为双引号 * 增加eslint格式校验 * demo里getUploadUrl改为promise方式,因为之前getUploadUrl是同步的,这里不改会有bug * 加入重试,当出现599状态码错误,上传自动重试三次 * 解决demo中plupload上传的makefile请求的bug
1 parent 5c87513 commit b0a296b

19 files changed

+554
-436
lines changed

.eslintrc.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module.exports = {
2+
"extends": "eslint:recommended",
3+
"env": {
4+
"browser": true,
5+
"node": true,
6+
"commonjs": true,
7+
"amd": true,
8+
"worker":true,
9+
"es6":true,
10+
"mocha":true
11+
},
12+
"parserOptions": {
13+
"sourceType": "module",//module
14+
// 想使用的额外的语言特性:
15+
"ecmaFeatures": {
16+
// 允许在全局作用域下使用 return 语句
17+
"globalReturn":true,
18+
// impliedStric
19+
"impliedStrict":true
20+
}
21+
},
22+
"rules": {
23+
"no-console": "off",
24+
"no-redeclare": 2,
25+
"keyword-spacing": 1,
26+
"indent":[
27+
0,
28+
2
29+
],
30+
"array-bracket-spacing": [1, "never"],
31+
"comma-spacing": [1, { "before": false, "after": true }],
32+
"no-var": 1,
33+
"eqeqeq": 0,
34+
"brace-style": 1,
35+
"camelcase": 0,
36+
"space-infix-ops": 1,
37+
"no-unused-vars": [1, { "vars": "all", "args": "none" }],
38+
"spaced-comment": [1, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!"] }],
39+
"quotes": [
40+
"error",
41+
"double"
42+
],
43+
"no-shadow": 0,
44+
"semi": [
45+
1,
46+
"always"
47+
]
48+
},
49+
}

.jshintrc

Lines changed: 0 additions & 43 deletions
This file was deleted.

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ cache:
77
install:
88
- npm install
99
script:
10-
- npm run build:prod
11-
before_deploy: "npm run build:prod"
10+
- npm run build
11+
before_deploy: "npm run build"
1212
deploy:
1313
provider: npm
1414

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Qiniu-JavaScript-SDK 为客户端 SDK,没有包含 `token` 生成实现,为
3535

3636
Qiniu-JavaScript-SDK 的示例 [Demo](http://jssdk-v2.demo.qiniu.io) 中的服务器端部分是基于[ Node.js 服务器端 SDK ](https://developer.qiniu.com/kodo/sdk/nodejs) 开发的。
3737

38+
- [JavaScript SDK 在线示例](http://jssdk-v2.demo.qiniu.io/)
3839
<!--
3940
本 SDK 可使开发者忽略上传底层实现细节,而更多的关注 UI 层的展现。
4041
-->
@@ -146,7 +147,7 @@ subscription.unsubscribe() // 上传取消
146147
* error: 上传错误后触发,当不是 xhr 请求错误时,会把当前错误产生原因直接抛出,诸如 JSON 解析异常等;当产生 xhr 请求错误时,参数 err 为一个包含 `code``message``isRequestError` 三个属性的 `object`
147148
* err.isRequestError: 用于区分是否 xhr 请求错误;当 xhr 请求出现错误并且后端通过 HTTP 状态码返回了错误信息时,该参数为 `true`;否则为 `undefined`
148149
* err.reqId: `string`,xhr请求错误的 `X-Reqid`
149-
* err.code: `number`,请求错误状态码,只有在 `err.isRequestError`true 的时候才有效,可查阅码值对应[说明](https://developer.qiniu.com/kodo/api/3928/error-responses)。
150+
* err.code: `number`,请求错误状态码,只有在 `err.isRequestError`true 的时候才有效,当出现 `599` 错误上传会自动重试,整个上传过程中出现 `599` 次数最多6次,否则会停止当前上传并输出错误信息。可查阅码值对应[说明](https://developer.qiniu.com/kodo/api/3928/error-responses)。
150151
* err.message: `string`,错误信息,包含错误码,当后端返回提示信息时也会有相应的错误信息。
151152

152153
* complete: 接收上传完成后的后端返回信息,具体返回结构取决于后端sdk的配置,可参考[上传策略](https://developer.qiniu.com/kodo/manual/1206/put-policy)。
@@ -167,7 +168,8 @@ subscription.unsubscribe() // 上传取消
167168

168169
* config.useCdnDomain: 表示是否使用 cdn 加速域名,为布尔值,`true` 表示使用,默认为 `false`
169170
* config.disableStatisticsReport: 是否禁用日志报告,为布尔值,默认为 `false`
170-
* config.region: 选择上传域名区域,默认为(z0)华东。
171+
* config.region: 选择上传域名区域;当为 `null``undefined` 时,自动分析上传域名区域。
172+
* config.retryCount: 当上传过程中出现 `599` 内部错误时,上传自动重试次数,默认三次。
171173

172174
* **putExtra**:
173175

@@ -210,12 +212,12 @@ subscription.unsubscribe() // 上传取消
210212
* **qiniu.region.na0**: 代表北美区域
211213
* **qiniu.region.as0**: 代表新加坡区域
212214

213-
### qiniu.getUploadUrl(config: object): string
215+
### qiniu.getUploadUrl(config: object, token: string): Promise
214216

215217
接收参数为 `config` 对象,返回根据 `config` 里所配置信息的上传域名
216218

217219
```JavaScript
218-
var requestUrl = qiniu.getUploadUrl(config)
220+
qiniu.getUploadUrl(config, token).then(res => {}) // res 即为上传的 url
219221
```
220222

221223
### qiniu.getHeadersForChunkUpload(token: string): object
@@ -424,7 +426,7 @@ subscription.unsubscribe() // 上传取消
424426
"Domain": "<Your Bucket Domain>" // Bucket 的外链默认域名,在 Bucket 的内容管理里查看,如:'http://xxx.bkt.clouddn.com/'
425427
}
426428
```
427-
2. 进入项目根目录,执行 `npm install` 安装依赖库,然后打开两个终端,一个执行 `npm run serve` 跑 server, 一个执行 `npm run dev` 运行服务 demo1; demo2 为测试es6语法的 demo,进入 demo2 目录,执行 `npm install`,然后 `npm start` 运行 demo2,demo1 和 demo2 都共用一个 server,请注意 server 文件里的 `region` 设置跟 `config` 里的` region`设置要保持一致。
429+
2. 进入项目根目录,执行 `npm install` 安装依赖库,然后打开两个终端,一个执行 `npm run serve` 跑 server, 一个执行 `npm run dev` 运行服务 demo1; demo2 为测试es6语法的 demo,进入 demo2 目录,执行 `npm install`,然后 `npm start` 运行 demo2,demo1 和 demo2 都共用一个 server,请注意 server 文件里的 `region` 设置跟 `config` 里的` region` 设置要保持一致。
428430

429431

430432
<a id="note"></a>

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"name": "qiniu-js",
33
"jsName": "qiniu",
4-
"version": "2.1.2",
4+
"version": "2.1.3",
55
"private": false,
66
"description": "Javascript SDK for Qiniu Resource (Cloud) Storage AP",
77
"main": "dist/qiniu.min.js",
88
"scripts": {
9-
"build:prod": "webpack --optimize-minimize --config webpack.prod.js",
10-
"dev": "webpack-dev-server --open --config webpack.dev.js",
9+
"build": "eslint src && webpack --optimize-minimize --config webpack.prod.js",
10+
"dev": "eslint src && webpack-dev-server --open --config webpack.dev.js",
1111
"serve": "node test/server.js"
1212
},
1313
"repository": {
@@ -38,6 +38,7 @@
3838
"body-parser": "^1.18.2",
3939
"connect-multiparty": "^2.1.0",
4040
"es3ify-loader": "^0.2.0",
41+
"eslint": "^4.18.2",
4142
"express": "^4.16.2",
4243
"multiparty": "^4.1.3",
4344
"open-browser-webpack-plugin": "0.0.5",

src/base64.js

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ function base64Encode(data) {
8686
// * returns 1: 'S2V2aW4gdmFuIFpvbm5ldmVsZA=='
8787
// mozilla has this native
8888
// - but breaks in 2.0.0.12!
89-
//if (typeof this.window['atob'] == 'function') {
89+
// if (typeof this.window['atob'] == 'function') {
9090
// return atob(data);
91-
//}
91+
// }
9292
let b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
9393
let o1,
9494
o2,
@@ -141,7 +141,68 @@ function base64Encode(data) {
141141
return enc;
142142
}
143143

144+
function base64Decode(data) {
145+
// http://kevin.vanzonneveld.net
146+
// + original by: Tyler Akins (http://rumkin.com)
147+
// + improved by: Thunder.m
148+
// + input by: Aman Gupta
149+
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
150+
// + bugfixed by: Onno Marsman
151+
// + bugfixed by: Pellentesque Malesuada
152+
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
153+
// + input by: Brett Zamir (http://brett-zamir.me)
154+
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
155+
// * example 1: base64_decode('S2V2aW4gdmFuIFpvbm5ldmVsZA==');
156+
// * returns 1: 'Kevin van Zonneveld'
157+
// mozilla has this native
158+
// - but breaks in 2.0.0.12!
159+
// if (typeof this.window['atob'] == 'function') {
160+
// return atob(data);
161+
// }
162+
let b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
163+
let o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
164+
ac = 0,
165+
dec = "",
166+
tmp_arr = [];
167+
168+
if (!data) {
169+
return data;
170+
}
171+
172+
data += "";
173+
174+
do { // unpack four hexets into three octets using index points in b64
175+
h1 = b64.indexOf(data.charAt(i++));
176+
h2 = b64.indexOf(data.charAt(i++));
177+
h3 = b64.indexOf(data.charAt(i++));
178+
h4 = b64.indexOf(data.charAt(i++));
179+
180+
bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;
181+
182+
o1 = bits >> 16 & 0xff;
183+
o2 = bits >> 8 & 0xff;
184+
o3 = bits & 0xff;
185+
186+
if (h3 === 64) {
187+
tmp_arr[ac++] = String.fromCharCode(o1);
188+
} else if (h4 === 64) {
189+
tmp_arr[ac++] = String.fromCharCode(o1, o2);
190+
} else {
191+
tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
192+
}
193+
} while (i < data.length);
194+
195+
dec = tmp_arr.join("");
196+
197+
return dec;
198+
}
199+
144200
export function urlSafeBase64Encode(v) {
145201
v = base64Encode(v);
146202
return v.replace(/\//g, "_").replace(/\+/g, "-");
147203
}
204+
205+
export function urlSafeBase64Decode(v){
206+
v = v.replace(/_/g, "/").replace(/-/g, "+");
207+
return base64Decode(v);
208+
}

src/image.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createXHR, request } from "./utils";
1+
import { request } from "./utils";
22
import { urlSafeBase64Encode } from "./base64";
33

44
function getImageUrl(key, domain) {

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
import { UploadManager } from "./upload";
1010
import { imageMogr2, watermark, imageInfo, exif, pipeline } from "./image";
1111
import { Observable } from "./observable";
12-
import { StatisticsLogger } from './statisticsLog'
12+
import { StatisticsLogger } from "./statisticsLog";
1313

1414
let statisticsLogger = new StatisticsLogger();
1515

src/statisticsLog.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {createXHR} from './utils'
1+
import { createXHR } from "./utils";
22

33
export class StatisticsLogger{
44

@@ -11,14 +11,14 @@ export class StatisticsLogger{
1111
send(logString, token){
1212
let xhr = createXHR();
1313
let count = 0;
14-
xhr.open('POST', "https://uplog.qbox.me/log/3");
15-
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
16-
xhr.setRequestHeader('Authorization', 'UpToken ' + token);
14+
xhr.open("POST", "https://uplog.qbox.me/log/3");
15+
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
16+
xhr.setRequestHeader("Authorization", "UpToken " + token);
1717
xhr.onreadystatechange = function () {
1818
if (xhr.readyState === 4) {
1919
if (xhr.status !== 200) {
2020
count++;
21-
count <=3 ? xhr.send(logString) : "";
21+
count <= 3 ? xhr.send(logString) : "";
2222
}
2323
}
2424
};

0 commit comments

Comments
 (0)