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

Conversation

winddies
Copy link
Contributor

@winddies winddies commented Mar 6, 2018

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

src/utils.js Outdated
return new Promise((resolve, reject) => {
if(config.region != null){
let upHosts = regionUphostMap[config.region];
let protocol = window.location.protocol === 'https:' ? "https" : "http";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为什么不直接去 protocol 而且要把冒号去掉?
而且下面多处用 protocol 应该提取上去

src/utils.js Outdated
getUpHosts(token)
.then(res => {
let hosts = res.data.up.acc.main;
let url = window.location.protocol === 'https:' ? "https://" + hosts[0] : "http://" + hosts[0];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为什么不直接 `${protocol}//${hosts[0]}`

lzfee0227
lzfee0227 previously approved these changes Mar 7, 2018
README.md Outdated

* 通过源码编译

`·git clone git@github.com:qiniu/js-sdk.git`,进入项目根目录执行`npm install`,执行`npm run build`,即可在dist目录生成qiniu.min.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

中英文间空格(执行npm,在dist目录,生成qiniu)

src/upload.js Outdated
@@ -84,7 +86,7 @@ export class UploadManager {
}
this.stop();
})
return upload;
return result
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

行尾分号?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不应该返回这个 result 啊,这个方法叫 putFile,返回的 promise 就应该代表 put file 这个行为,现在返回的 result 代表的是 get upload url 这个行为

src/upload.js Outdated
if(!this.config.disableStatisticsReport){
this.sendLog(res.reqId, 200);
}
}, err => {})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

err => {} 是为什么

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原来的结构不用动它的:

let upload = getUploadUrl(this.config, this.token).then(uploadUrl => {
  this.uploadUrl = res;
  this.uploadAt = new Date().getTime();
  return this.file.size > BLOCK_SIZE ? this.resumeUpload() : this.directUpload();
});
upload.then(...);
return upload;

@@ -4,7 +4,7 @@
var config = {
useCdnDomain: true,
disableStatisticsReport: false,
region: qiniu.region.z2
region: qiniu.region.z2,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

无需逗号

src/utils.js Outdated
let uphosts_url = window.location.protocol + "//api.qiniu.com/v2/query?ak=" + putPolicy.ak + "&bucket=" + putPolicy.bucket;
return request(uphosts_url, { method: "GET" })
}
return Promise.reject(err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

err??

src/upload.js Outdated
@@ -225,8 +229,14 @@ export class UploadManager {
}

updateDirectProgress(loaded, total) {
this.progress = {total: this.getProgressInfoItem(loaded, total)}
this.onData(this.progress)
this.progress = {total: this.getProgressInfoItem(loaded, total + 1)};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里改动是为啥

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是我发现在网络比较慢的时候进行直传,进度达到100,但是请求并未完成,那么这个时候不应该进度为100,所以在这里加了个1

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那就在注释里说明这个情况,不然维护代码的人无法理解;即便你自己维护,将来你自己也会忘的

src/upload.js Outdated
method: "POST",
body: formData,
onProgress: (data) => {
this.updateDirectProgress(data.loaded, data.total);
},
onCreate: this.xhrHandler
});
result.then(() => {this.finishDirectProgress()}, err => {});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么 err => {}

src/utils.js Outdated
let host = config.useCdnDomain ? upHosts.cdnUphost : upHosts.srcUphost;
resolve(`${protocol}//${host}`);
}
getUpHosts(token)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getUpHosts(token)
    .then(res => {
      let hosts = res.data.up.acc.main;
      return `${protocol}//${hosts[0]}`
    })

这个 promise 是可以整个被 resolve 的,不用再去它的后边接 resolve & reject

src/utils.js Outdated
try {
let putPolicy = JSON.parse(urlSafeBase64Decode(segments[2]));
putPolicy.ak = ak;
if (putPolicy.scope.indexOf(":") >= 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里不做判断,直接用

putPolicy.bucket = putPolicy.scope.split(":")[0];

效果是一样的

src/utils.js Outdated
return request(uphosts_url, { method: "GET" })
}
return Promise.reject(err)
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

函数声明后面不用分号

src/upload.js Outdated
method: "POST",
body: formData,
onProgress: (data) => {
this.updateDirectProgress(data.loaded, data.total);
},
onCreate: this.xhrHandler
});
return result.then(res => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let result = (...)
return result.then(...)

return (...).then(...)

有啥区别

src/upload.js Outdated
this.uploadAt = new Date().getTime();

let upload = this.file.size > BLOCK_SIZE ? this.resumeUpload() : this.directUpload();
let upload = getUploadUrl(this.config, this.token).then(res =>{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

空格 res =>{ -> res => {

这种低级问题我会记小本本的...

src/utils.js Outdated
putPolicy.bucket = putPolicy.scope.split(":")[0];
return putPolicy;
} catch(err) {
return err;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getPutPolicy 这个方法是预期返回 policy({ ak, bucket })的,这里返回一个 err(一般是一个 Error 对象),你让外边怎么用呢?

这个地方可以不用 try catch 的,有错就直接抛出去就好

src/utils.js Outdated
let url = window.location.protocol + "//api.qiniu.com/v2/query?ak=" + putPolicy.ak + "&bucket=" + putPolicy.bucket;
return request(url, { method: "GET" })
}
return Promise.reject(putPolicy);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这边其实是想处理上边 getPutPolicy 时的异常,包装成一个 reject 的 promise,那可以在这个方法里 try catch,或者直接把这些行为在 promise 的构造函数里(new Promise(fn) 的话,发生在 fn 执行中的异常会自动被 catch 且导致对应的 promise 的 reject),即:

try {
  let putPolicy = getPutPolicy(token);
  let url = window.location.protocol + "//api.qiniu.com/v2/query?ak=" + putPolicy.ak + "&bucket=" + putPolicy.bucket;
  return request(url, { method: "GET" });
} catch (e) {
  return Promise.reject(e);
}

或者

return new Promise(resolve => {
  let putPolicy = getPutPolicy(token);
  let url = window.location.protocol + "//api.qiniu.com/v2/query?ak=" + putPolicy.ak + "&bucket=" + putPolicy.bucket;
  resolve(request(url, { method: "GET" }));
})

src/utils.js Outdated
let putPolicy = getPutPolicy(token);
if(putPolicy.bucket){
let url = window.location.protocol + "//api.qiniu.com/v2/query?ak=" + putPolicy.ak + "&bucket=" + putPolicy.bucket;
return request(url, { method: "GET" })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

分号,小本本 +1

src/utils.js Outdated

function getUpHosts(token) {
let putPolicy = getPutPolicy(token);
if(putPolicy.bucket){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if 空格,加个代码格式的 lint 吧,提 PR 前先 lint 通过

@nighca
Copy link
Contributor

nighca commented Mar 8, 2018

标题是“当region不设置时自动分析上传区域”,但实际上改动远不止这个,把所有改动在 PR 里列出来说清楚,参考 https://github.com/qbox/portal-base/pull/124

nighca
nighca previously approved these changes Mar 9, 2018
src/upload.js Outdated
if (err.isRequestError){
if (err.code === 599){
this.retryCount++;
this.retryCount > 3 ? "" : this.putFile();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要用 a ? b : c 的操作符替代 if,影响阅读体验:

if (err.code === 599 && this.retryCount < 3) {
  this.retryCount++;
  this.putFile();
}

另外按这边的逻辑,加上重试的话,最多总共会发 4 次,确认下是符合预期的?

src/upload.js Outdated
}
if (!this.config.disableStatisticsReport){
err.code === 0 ? this.sendLog("", -2) : this.sendLog(err.reqId, err.code);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上,a ? b : c 的操作符组合的结果是一个表达式(expression),不是一个语句(statement),一般代表一个值,而不是一个行为;这里为了达到简洁的目的用这个三元操作符有滥用的嫌疑

@nighca
Copy link
Contributor

nighca commented Mar 12, 2018

另外就是,这么多的改动主题,是应该拆成独立的 PR 来做的,而不是放到同一个分支同一个 PR 里,以后注意吧

src/upload.js Outdated
return;
}
// 重试过程中遇到 599 不需要 onError,当超过最大次数后再 onError
this.onError(err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这行跟下一行都不需要吧?从 if 出来自然就走到 98 行的 onError

src/upload.js Outdated
}
// 出现 599 重试,最多重试 6 次
if (err.code === 599){
if (this.retryCount < 6){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个行为(6)可配置?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已改为可配置

@nighca nighca merged commit b0a296b into qiniu:master Mar 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants