-
Notifications
You must be signed in to change notification settings - Fork 515
当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
当region不设置时自动分析上传区域 #346
Conversation
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"; |
There was a problem hiding this comment.
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]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里为什么不直接 `${protocol}//${hosts[0]}`
README.md
Outdated
|
||
* 通过源码编译 | ||
|
||
`·git clone git@github.com:qiniu/js-sdk.git`,进入项目根目录执行`npm install`,执行`npm run build`,即可在dist目录生成qiniu.min.js |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
行尾分号?
There was a problem hiding this comment.
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 => {}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
err => {}
是为什么
There was a problem hiding this comment.
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;
test/demo1/main.js
Outdated
@@ -4,7 +4,7 @@ | |||
var config = { | |||
useCdnDomain: true, | |||
disableStatisticsReport: false, | |||
region: qiniu.region.z2 | |||
region: qiniu.region.z2, |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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)}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里改动是为啥
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里是我发现在网络比较慢的时候进行直传,进度达到100,但是请求并未完成,那么这个时候不应该进度为100,所以在这里加了个1
There was a problem hiding this comment.
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 => {}); |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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) | ||
}; |
There was a problem hiding this comment.
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 => { |
There was a problem hiding this comment.
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 =>{ |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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" }) |
There was a problem hiding this comment.
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){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if 空格,加个代码格式的 lint 吧,提 PR 前先 lint 通过
标题是“当region不设置时自动分析上传区域”,但实际上改动远不止这个,把所有改动在 PR 里列出来说清楚,参考 https://github.com/qbox/portal-base/pull/124 |
src/upload.js
Outdated
if (err.isRequestError){ | ||
if (err.code === 599){ | ||
this.retryCount++; | ||
this.retryCount > 3 ? "" : this.putFile(); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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),一般代表一个值,而不是一个行为;这里为了达到简洁的目的用这个三元操作符有滥用的嫌疑
另外就是,这么多的改动主题,是应该拆成独立的 PR 来做的,而不是放到同一个分支同一个 PR 里,以后注意吧 |
src/upload.js
Outdated
return; | ||
} | ||
// 重试过程中遇到 599 不需要 onError,当超过最大次数后再 onError | ||
this.onError(err) |
There was a problem hiding this comment.
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){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个行为(6
)可配置?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已改为可配置
Uh oh!
There was an error while loading. Please reload this page.