From 3243318e17378bf8b990a4484a874a66dd6b695a Mon Sep 17 00:00:00 2001 From: winddies <517817136@qq.com> Date: Wed, 23 Oct 2019 15:30:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=88=9B=E5=BB=BA=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=B8=8A=E4=BC=A0=E5=9C=B0=E5=9D=80=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- package.json | 2 +- src/upload.js | 18 +++++++----------- src/utils.js | 16 +++++----------- 4 files changed, 15 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 6471d185..94a6404a 100644 --- a/README.md +++ b/README.md @@ -185,7 +185,7 @@ qiniu.compressImage(file, options).then(data => { * config.disableStatisticsReport: 是否禁用日志报告,为布尔值,默认为 `false`。 * config.uphost: 上传 `host`,类型为 `string`, 如果设定该参数则优先使用该参数作为上传地址,默认为 `null`。 * config.region: 选择上传域名区域;当为 `null` 或 `undefined` 时,自动分析上传域名区域。 - * config.retryCount: 上传自动重试次数(整体重试次数,而不是某个分片的重试次数);默认 3 次(即上传失败后最多重试两次);**目前仅在上传过程中产生 `599` 内部错误时生效**,**但是未来很可能会扩展为支持更多的情况**。 + * config.retryCount: 上传自动重试次数(整体重试次数,而不是某个分片的重试次数);默认 3 次(即上传失败后最多重试两次)。 * config.concurrentRequestLimit: 分片上传的并发请求量,`number`,默认为3;因为浏览器本身也会限制最大并发量,所以最大并发量与浏览器有关。 * config.checkByMD5: 是否开启 MD5 校验,为布尔值;在断点续传时,开启 MD5 校验会将已上传的分片与当前分片进行 MD5 值比对,若不一致,则重传该分片,避免使用错误的分片。读取分片内容并计算 MD5 需要花费一定的时间,因此会稍微增加断点续传时的耗时,默认为 false,不开启。 * config.forceDirect: 是否上传全部采用直传方式,为布尔值;为 `true` 时则上传方式全部为直传 form 方式,禁用断点续传,默认 `false`。 @@ -201,7 +201,7 @@ qiniu.compressImage(file, options).then(data => { ``` * fname: `string`,文件原文件名 - * params: `object`,用来放置自定义变量 + * params: `object`,用来放置自定义变量,自定义变量格式请参考[文档](https://developer.qiniu.com/kodo/manual/1235/vars) * mimeType: `null || array`,用来限制上传文件类型,为 `null` 时表示不对文件类型限制;限制类型放到数组里: `["image/png", "image/jpeg", "image/gif"]` diff --git a/package.json b/package.json index 4823b138..76512eaa 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "qiniu-js", "jsName": "qiniu", - "version": "2.5.4", + "version": "2.5.5", "private": false, "description": "Javascript SDK for Qiniu Resource (Cloud) Storage AP", "main": "dist/qiniu.min.js", diff --git a/src/upload.js b/src/upload.js index fc35d545..74e40c8d 100644 --- a/src/upload.js +++ b/src/upload.js @@ -5,7 +5,7 @@ import { setLocalFileInfo, removeLocalFileInfo, getLocalFileInfo, - findMimeType, + isContainFileMimeType, sum, getDomainFromUrl, getPortFromUrl, @@ -64,15 +64,11 @@ export class UploadManager { if (!this.putExtra.fname) { this.putExtra.fname = this.file.name; } - if (this.putExtra.mimeType && this.putExtra.mimeType.length) { - var compareMimeType = findMimeType(this.file.type, this.putExtra.mimeType); - if (compareMimeType == null || compareMimeType == undefined) { - let err = new Error("file type doesn't match with what you specify"); - this.onError(err); - return; - } else { - this.putExtra.mimeType = [compareMimeType]; - } + if (this.putExtra.mimeType && this.putExtra.mimeType.length + && !isContainFileMimeType(this.file.type, this.putExtra.mimeType)) { + let err = new Error("file type doesn't match with what you specify"); + this.onError(err); + return; } let upload = getUploadUrl(this.config, this.token).then(res => { this.uploadUrl = res; @@ -255,7 +251,7 @@ export class UploadManager { let requestUrL = createMkFileUrl( this.uploadUrl, - this.file.size, + this.file, this.key, putExtra ); diff --git a/src/utils.js b/src/utils.js index c917bc2b..e7e84c47 100644 --- a/src/utils.js +++ b/src/utils.js @@ -80,13 +80,13 @@ export function getResumeUploadedSize(file) { } // 构造file上传url -export function createMkFileUrl(url, size, key, putExtra) { - let requestUrl = url + "/mkfile/" + size; +export function createMkFileUrl(url, file, key, putExtra) { + let requestUrl = url + "/mkfile/" + file.size; if (key != null) { requestUrl += "/key/" + urlSafeBase64Encode(key); } if (putExtra.mimeType) { - requestUrl += "/mimeType/" + urlSafeBase64Encode(putExtra.mimeType); + requestUrl += "/mimeType/" + urlSafeBase64Encode(file.type); } let fname = putExtra.fname; if (fname) { @@ -264,14 +264,8 @@ function getUpHosts(token) { } } -export function findMimeType(fileType, mimeType) { - var rtType = null; - mimeType.forEach(elem => { - if(fileType == elem){ - rtType = fileType; - } - }); - return rtType; +export function isContainFileMimeType(fileType, mimeType) { + return mimeType.indexOf(fileType) > -1; } export function createObjectURL(file) {