Skip to content

当region不设置时自动分析上传区域 #346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Mar 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module.exports = {
"extends": "eslint:recommended",
"env": {
"browser": true,
"node": true,
"commonjs": true,
"amd": true,
"worker":true,
"es6":true,
"mocha":true
},
"parserOptions": {
"sourceType": "module",//module
// 想使用的额外的语言特性:
"ecmaFeatures": {
// 允许在全局作用域下使用 return 语句
"globalReturn":true,
// impliedStric
"impliedStrict":true
}
},
"rules": {
"no-console": "off",
"no-redeclare": 2,
"keyword-spacing": 1,
"indent":[
0,
2
],
"array-bracket-spacing": [1, "never"],
"comma-spacing": [1, { "before": false, "after": true }],
"no-var": 1,
"eqeqeq": 0,
"brace-style": 1,
"camelcase": 0,
"space-infix-ops": 1,
"no-unused-vars": [1, { "vars": "all", "args": "none" }],
"spaced-comment": [1, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!"] }],
"quotes": [
"error",
"double"
],
"no-shadow": 0,
"semi": [
1,
"always"
]
},
}
43 changes: 0 additions & 43 deletions .jshintrc

This file was deleted.

4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ cache:
install:
- npm install
script:
- npm run build:prod
before_deploy: "npm run build:prod"
- npm run build
before_deploy: "npm run build"
deploy:
provider: npm
email: sdk@qiniu.com
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Qiniu-JavaScript-SDK 为客户端 SDK,没有包含 `token` 生成实现,为

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

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

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

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

* **putExtra**:

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

### qiniu.getUploadUrl(config: object): string
### qiniu.getUploadUrl(config: object, token: string): Promise

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

```JavaScript
var requestUrl = qiniu.getUploadUrl(config)
qiniu.getUploadUrl(config, token).then(res => {}) // res 即为上传的 url
```

### qiniu.getHeadersForChunkUpload(token: string): object
Expand Down Expand Up @@ -424,7 +426,7 @@ subscription.unsubscribe() // 上传取消
"Domain": "<Your Bucket Domain>" // Bucket 的外链默认域名,在 Bucket 的内容管理里查看,如:'http://xxx.bkt.clouddn.com/'
}
```
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`设置要保持一致。
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` 设置要保持一致。


<a id="note"></a>
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "qiniu-js",
"jsName": "qiniu",
"version": "2.1.2",
"version": "2.1.3",
"private": false,
"description": "Javascript SDK for Qiniu Resource (Cloud) Storage AP",
"main": "dist/qiniu.min.js",
"scripts": {
"build:prod": "webpack --optimize-minimize --config webpack.prod.js",
"dev": "webpack-dev-server --open --config webpack.dev.js",
"build": "eslint src && webpack --optimize-minimize --config webpack.prod.js",
"dev": "eslint src && webpack-dev-server --open --config webpack.dev.js",
"serve": "node test/server.js"
},
"repository": {
Expand Down Expand Up @@ -38,6 +38,7 @@
"body-parser": "^1.18.2",
"connect-multiparty": "^2.1.0",
"es3ify-loader": "^0.2.0",
"eslint": "^4.18.2",
"express": "^4.16.2",
"multiparty": "^4.1.3",
"open-browser-webpack-plugin": "0.0.5",
Expand Down
65 changes: 63 additions & 2 deletions src/base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ function base64Encode(data) {
// * returns 1: 'S2V2aW4gdmFuIFpvbm5ldmVsZA=='
// mozilla has this native
// - but breaks in 2.0.0.12!
//if (typeof this.window['atob'] == 'function') {
// if (typeof this.window['atob'] == 'function') {
// return atob(data);
//}
// }
let b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
let o1,
o2,
Expand Down Expand Up @@ -141,7 +141,68 @@ function base64Encode(data) {
return enc;
}

function base64Decode(data) {
// http://kevin.vanzonneveld.net
// + original by: Tyler Akins (http://rumkin.com)
// + improved by: Thunder.m
// + input by: Aman Gupta
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: Onno Marsman
// + bugfixed by: Pellentesque Malesuada
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + input by: Brett Zamir (http://brett-zamir.me)
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: base64_decode('S2V2aW4gdmFuIFpvbm5ldmVsZA==');
// * returns 1: 'Kevin van Zonneveld'
// mozilla has this native
// - but breaks in 2.0.0.12!
// if (typeof this.window['atob'] == 'function') {
// return atob(data);
// }
let b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
let o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
ac = 0,
dec = "",
tmp_arr = [];

if (!data) {
return data;
}

data += "";

do { // unpack four hexets into three octets using index points in b64
h1 = b64.indexOf(data.charAt(i++));
h2 = b64.indexOf(data.charAt(i++));
h3 = b64.indexOf(data.charAt(i++));
h4 = b64.indexOf(data.charAt(i++));

bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;

o1 = bits >> 16 & 0xff;
o2 = bits >> 8 & 0xff;
o3 = bits & 0xff;

if (h3 === 64) {
tmp_arr[ac++] = String.fromCharCode(o1);
} else if (h4 === 64) {
tmp_arr[ac++] = String.fromCharCode(o1, o2);
} else {
tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
}
} while (i < data.length);

dec = tmp_arr.join("");

return dec;
}

export function urlSafeBase64Encode(v) {
v = base64Encode(v);
return v.replace(/\//g, "_").replace(/\+/g, "-");
}

export function urlSafeBase64Decode(v){
v = v.replace(/_/g, "/").replace(/-/g, "+");
return base64Decode(v);
}
2 changes: 1 addition & 1 deletion src/image.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createXHR, request } from "./utils";
import { request } from "./utils";
import { urlSafeBase64Encode } from "./base64";

function getImageUrl(key, domain) {
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { UploadManager } from "./upload";
import { imageMogr2, watermark, imageInfo, exif, pipeline } from "./image";
import { Observable } from "./observable";
import { StatisticsLogger } from './statisticsLog'
import { StatisticsLogger } from "./statisticsLog";

let statisticsLogger = new StatisticsLogger();

Expand Down
10 changes: 5 additions & 5 deletions src/statisticsLog.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {createXHR} from './utils'
import { createXHR } from "./utils";

export class StatisticsLogger{

Expand All @@ -11,14 +11,14 @@ export class StatisticsLogger{
send(logString, token){
let xhr = createXHR();
let count = 0;
xhr.open('POST', "https://uplog.qbox.me/log/3");
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.setRequestHeader('Authorization', 'UpToken ' + token);
xhr.open("POST", "https://uplog.qbox.me/log/3");
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Authorization", "UpToken " + token);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status !== 200) {
count++;
count <=3 ? xhr.send(logString) : "";
count <= 3 ? xhr.send(logString) : "";
}
}
};
Expand Down
Loading