Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
glsx
/
gl_tape
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
3
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit fa995534
authored
Jul 04, 2024
by
yixinjj
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
格式化
1 parent
a3f0b4cd
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
89 additions
and
102 deletions
hybrid/html/js/OSSUpLoader.js
mixin/uploader.js
hybrid/html/js/OSSUpLoader.js
View file @
fa99553
...
@@ -2,110 +2,96 @@ class OSSUpLoader {
...
@@ -2,110 +2,96 @@ class OSSUpLoader {
/**
/**
* yourRegion填写Bucket所在地域。以华东1(杭州)为例,yourRegion填写为oss-cn-hangzhou。
* yourRegion填写Bucket所在地域。以华东1(杭州)为例,yourRegion填写为oss-cn-hangzhou。
*/
*/
region
=
"oss-cn-shenzhen"
;
region
=
'oss-cn-shenzhen'
/**
/**
* 填写Bucket名称。
* 填写Bucket名称。
*/
*/
bucket
=
"glsk-oss"
;
bucket
=
'glsk-oss'
/**
/**
* 文件的 MIME 类型
* 文件的 MIME 类型
*/
*/
mime
=
"audio/mp3"
;
mime
=
'audio/mp3'
/**
/**
* 本地保存文件路径(待上传文件路径)
* 本地保存文件路径(待上传文件路径)
*/
*/
filePath
;
filePath
/**
/**
* 从STS服务获取的临时访问密钥(AccessKey ID和AccessKey Secret)。
* 从STS服务获取的临时访问密钥(AccessKey ID和AccessKey Secret)。
*/
*/
accessKeyId
;
accessKeyId
/**
/**
* 从STS服务获取的安全令牌(SecurityToken)。
* 从STS服务获取的安全令牌(SecurityToken)。
*/
*/
accessKeySecret
;
accessKeySecret
/**
/**
* 从STS服务获取的安Token。
* 从STS服务获取的安Token。
*/
*/
stsToken
;
stsToken
/**
/**
* OSS 文件上传实例
* OSS 文件上传实例
*/
*/
client
=
null
;
client
=
null
/**
/**
* 同时上传的最大任务数
* 同时上传的最大任务数
*/
*/
parallel
=
4
;
parallel
=
4
/**
/**
* 每个上传分片的大小(字节)。默认500kb。
* 每个上传分片的大小(字节)。默认500kb。
*/
*/
partSize
=
500
;
partSize
=
500
/**
/**
* 最小文件大小
* 最小文件大小
*/
*/
minPartSize
=
100
;
minPartSize
=
100
/**
/**
* 上传任务的超时时间(毫秒):默认60
* 上传任务的超时时间(毫秒):默认60
*/
*/
timeout
=
60
*
1000
;
timeout
=
60
*
1000
// headers,
// headers,
/**
/**
* 自定义元数据,通过HeadObject接口可以获取Object的元数据。
* 自定义元数据,通过HeadObject接口可以获取Object的元数据。
*/
*/
meta
=
{}
;
meta
=
{}
/**
/**
* 初始化重传次数
* 初始化重传次数
*/
*/
retryCount
=
0
;
retryCount
=
0
/**
/**
* 上传失败时的最大重试次数
* 上传失败时的最大重试次数
*/
*/
retryCountMax
=
3
;
retryCountMax
=
3
/**
/**
* 上传失败时的重试间隔时间(毫秒)
* 上传失败时的重试间隔时间(毫秒)
*/
*/
retryInterval
=
3
*
1000
;
retryInterval
=
3
*
1000
currentCheckpoint
;
currentCheckpoint
/**
/**
* OSS 存储桶中的目标文件夹路径
* OSS 存储桶中的目标文件夹路径
*/
*/
ossFolder
=
"quality/"
;
ossFolder
=
'quality/'
/**
/**
* 构造函数
* 构造函数
*/
*/
constructor
({
constructor
({
region
=
this
.
region
,
bucket
=
this
.
bucket
,
accessKeyId
,
accessKeySecret
,
stsToken
,
mime
=
this
.
mime
,
partSize
=
this
.
partSize
,
parallel
=
this
.
parallel
,
timeout
=
this
.
timeout
,
filePath
,
retryCountMax
=
this
.
retryCountMax
,
retryInterval
=
this
.
retryInterval
,
ossFolder
=
this
.
ossFolder
})
{
region
=
this
.
region
,
bucket
=
this
.
bucket
,
accessKeyId
,
accessKeySecret
,
stsToken
,
mime
=
this
.
mime
,
partSize
=
this
.
partSize
,
parallel
=
this
.
parallel
,
timeout
=
this
.
timeout
,
filePath
,
retryCountMax
=
this
.
retryCountMax
,
retryInterval
=
this
.
retryInterval
,
ossFolder
=
this
.
ossFolder
,
})
{
if
(
partSize
<
this
.
minPartSize
)
{
if
(
partSize
<
this
.
minPartSize
)
{
throw
new
Error
(
common
.
constants
.
CODE
.
FILE_PART_SIZE_TOO_SMALL
)
;
throw
new
Error
(
common
.
constants
.
CODE
.
FILE_PART_SIZE_TOO_SMALL
)
}
}
this
.
mime
=
mime
;
this
.
mime
=
mime
this
.
partSize
=
partSize
;
this
.
partSize
=
partSize
this
.
parallel
=
parallel
;
this
.
parallel
=
parallel
this
.
timeout
=
timeout
;
this
.
timeout
=
timeout
this
.
filePath
=
filePath
;
this
.
filePath
=
filePath
this
.
retryCountMax
=
retryCountMax
;
this
.
retryCountMax
=
retryCountMax
this
.
retryInterval
=
retryInterval
;
this
.
retryInterval
=
retryInterval
this
.
ossFolder
=
ossFolder
;
this
.
ossFolder
=
ossFolder
//实例化OSS
//实例化OSS
this
.
client
=
new
OSS
({
this
.
client
=
new
OSS
({
region
,
region
,
bucket
,
bucket
,
accessKeyId
,
accessKeyId
,
accessKeySecret
,
accessKeySecret
,
stsToken
,
stsToken
})
;
})
}
}
/**
/**
* 上传文件到指定的 OSS 存储空间
* 上传文件到指定的 OSS 存储空间
...
@@ -118,12 +104,12 @@ class OSSUpLoader {
...
@@ -118,12 +104,12 @@ class OSSUpLoader {
this
.
filePath
+
fileName
,
this
.
filePath
+
fileName
,
function
(
result
)
{
function
(
result
)
{
if
(
result
.
code
===
common
.
constants
.
CODE
.
FILE_PATH_TRANSFORM_SUCCESS
)
{
if
(
result
.
code
===
common
.
constants
.
CODE
.
FILE_PATH_TRANSFORM_SUCCESS
)
{
this
.
_upload
(
fileName
,
result
.
data
.
file
,
callback
)
;
this
.
_upload
(
fileName
,
result
.
data
.
file
,
callback
)
}
else
{
}
else
{
callback
(
result
)
;
callback
(
result
)
}
}
}.
bind
(
this
)
}.
bind
(
this
)
)
;
)
}
}
/**
/**
...
@@ -140,58 +126,58 @@ class OSSUpLoader {
...
@@ -140,58 +126,58 @@ class OSSUpLoader {
.
multipartUpload
(
this
.
ossFolder
+
fileName
,
file
,
{
.
multipartUpload
(
this
.
ossFolder
+
fileName
,
file
,
{
// 获取分片上传进度、断点和返回值。
// 获取分片上传进度、断点和返回值。
progress
:
(
p
,
checkpoint
,
res
)
=>
{
progress
:
(
p
,
checkpoint
,
res
)
=>
{
this
.
currentCheckpoint
=
checkpoint
;
this
.
currentCheckpoint
=
checkpoint
console
.
log
(
p
)
;
console
.
log
(
p
)
},
},
// 设置并发上传的分片数量。
// 设置并发上传的分片数量。
parallel
:
this
.
parallel
,
parallel
:
this
.
parallel
,
//分片大小(kb) 最小为100kb 默认500kb
//分片大小(kb) 最小为100kb 默认500kb
partSize
:
1024
*
this
.
partSize
,
partSize
:
1024
*
this
.
partSize
,
timeout
:
this
.
timeout
,
timeout
:
this
.
timeout
,
mime
:
this
.
mime
,
mime
:
this
.
mime
})
})
.
then
(
(
res
)
=>
{
.
then
(
res
=>
{
// console.log(JSON.stringify(res),res.status,res.status==200)
// console.log(JSON.stringify(res),res.status,res.status==200)
if
(
res
.
res
.
statusCode
==
200
)
{
if
(
res
.
res
.
statusCode
==
200
)
{
// console.log('upload success: %j', JSON.stringify(res));
// console.log('upload success: %j', JSON.stringify(res));
this
.
currentCheckpoint
=
null
;
this
.
currentCheckpoint
=
null
this
.
retryCount
=
0
;
this
.
retryCount
=
0
callback
(
callback
(
common
.
genCallBacks
(
"FILE_UPLOAD_OSS_SUCCESS"
,
{
common
.
genCallBacks
(
'FILE_UPLOAD_OSS_SUCCESS'
,
{
oss
:
res
,
oss
:
res
})
})
)
;
)
}
else
{
}
else
{
callback
(
callback
(
common
.
genCallBacks
(
"FILE_UPLOAD_OSS_FAIL"
,
{
common
.
genCallBacks
(
'FILE_UPLOAD_OSS_FAIL'
,
{
errMsg
:
`OSS返回错误 状态码:
${
res
.
status
}
`
,
errMsg
:
`OSS返回错误 状态码:
${
res
.
status
}
`
,
oss
:
res
,
oss
:
res
})
})
)
;
)
}
}
})
})
.
catch
(
(
err
)
=>
{
.
catch
(
err
=>
{
console
.
error
(
err
)
;
console
.
error
(
err
)
console
.
log
(
`err.name :
${
err
.
name
}
`
)
;
console
.
log
(
`err.name :
${
err
.
name
}
`
)
console
.
log
(
`err.message :
${
err
.
message
}
`
)
;
console
.
log
(
`err.message :
${
err
.
message
}
`
)
// if (err.name.toLowerCase().indexOf('connectiontimeout') !== -1) {
// if (err.name.toLowerCase().indexOf('connectiontimeout') !== -1) {
// timeout retry
// timeout retry
if
(
this
.
retryCount
<
this
.
retryCountMax
)
{
if
(
this
.
retryCount
<
this
.
retryCountMax
)
{
this
.
retryCount
++
;
this
.
retryCount
++
console
.
error
(
`retryCount :
${
this
.
retryCount
}
`
)
;
console
.
error
(
`retryCount :
${
this
.
retryCount
}
`
)
setTimeout
(()
=>
{
setTimeout
(()
=>
{
this
.
_upload
(
fileName
,
file
,
callback
)
;
this
.
_upload
(
fileName
,
file
,
callback
)
},
this
.
retryInterval
)
;
},
this
.
retryInterval
)
}
else
{
}
else
{
callback
(
callback
(
common
.
genCallBacks
(
"FILE_UPLOAD_OSS_TIMEOUT_OR_OTHER"
,
{
common
.
genCallBacks
(
'FILE_UPLOAD_OSS_TIMEOUT_OR_OTHER'
,
{
errMsg
:
err
.
message
,
errMsg
:
err
.
message
,
err
,
err
})
})
)
;
)
}
}
// }
// }
})
;
})
}
}
/**
/**
...
@@ -203,39 +189,39 @@ class OSSUpLoader {
...
@@ -203,39 +189,39 @@ class OSSUpLoader {
* @throws {Error} 如果请求过程中发生错误,将抛出错误
* @throws {Error} 如果请求过程中发生错误,将抛出错误
*/
*/
_pathToFile
(
filePath
,
callback
)
{
_pathToFile
(
filePath
,
callback
)
{
window
.
webkitURL
=
window
.
webkitURL
||
window
.
URL
;
window
.
webkitURL
=
window
.
webkitURL
||
window
.
URL
var
xhr
=
new
XMLHttpRequest
()
;
var
xhr
=
new
XMLHttpRequest
()
xhr
.
open
(
"GET"
,
filePath
,
true
);
xhr
.
open
(
'GET'
,
filePath
,
true
)
xhr
.
responseType
=
"blob"
;
xhr
.
responseType
=
'blob'
xhr
.
onload
=
function
()
{
xhr
.
onload
=
function
()
{
if
(
this
.
status
==
200
)
{
if
(
this
.
status
==
200
)
{
var
file
=
this
.
response
;
var
file
=
this
.
response
callback
(
callback
(
common
.
genCallBacks
(
"FILE_PATH_TRANSFORM_SUCCESS"
,
{
common
.
genCallBacks
(
'FILE_PATH_TRANSFORM_SUCCESS'
,
{
file
,
file
})
})
)
;
)
}
else
{
}
else
{
callback
(
common
.
genCallBacks
(
"FILE_PATH_TRANSFORM_FAIL"
));
callback
(
common
.
genCallBacks
(
'FILE_PATH_TRANSFORM_FAIL'
))
}
}
}
};
xhr
.
onerror
=
function
(
errType
,
errMsg
)
{
xhr
.
onerror
=
function
(
errType
,
errMsg
)
{
callback
(
callback
(
common
.
genCallBacks
(
"FILE_PATH_REQUEST_FAIL"
,
{
common
.
genCallBacks
(
'FILE_PATH_REQUEST_FAIL'
,
{
errType
,
errType
,
errMsg
,
errMsg
})
})
)
;
)
}
;
}
xhr
.
send
()
;
xhr
.
send
()
}
}
/**
/**
* 销毁当前实例对象,释放资源并重置状态
* 销毁当前实例对象,释放资源并重置状态
* 该方法将重置上传器的重试次数为 0,并将客户端实例置为 null
* 该方法将重置上传器的重试次数为 0,并将客户端实例置为 null
*/
*/
destroy
()
{
destroy
()
{
this
.
retryCount
=
0
;
this
.
retryCount
=
0
this
.
client
=
null
;
this
.
client
=
null
}
}
}
}
...
@@ -247,17 +233,17 @@ const common = {
...
@@ -247,17 +233,17 @@ const common = {
FILE_PATH_TRANSFORM_FAIL
:
2
,
FILE_PATH_TRANSFORM_FAIL
:
2
,
FILE_UPLOAD_OSS_SUCCESS
:
0
,
FILE_UPLOAD_OSS_SUCCESS
:
0
,
FILE_UPLOAD_OSS_FAIL
:
3
,
FILE_UPLOAD_OSS_FAIL
:
3
,
FILE_UPLOAD_OSS_TIMEOUT_OR_OTHER
:
4
,
FILE_UPLOAD_OSS_TIMEOUT_OR_OTHER
:
4
},
},
MESSAGE
:
{
MESSAGE
:
{
FILE_PATH_TRANSFORM_SUCCESS
:
"文件转换成功"
,
FILE_PATH_TRANSFORM_SUCCESS
:
'文件转换成功'
,
FILE_PATH_REQUEST_FAIL
:
"文件转换请求失败"
,
FILE_PATH_REQUEST_FAIL
:
'文件转换请求失败'
,
FILE_PATH_TRANSFORM_FAIL
:
"文件转换失败"
,
FILE_PATH_TRANSFORM_FAIL
:
'文件转换失败'
,
FILE_UPLOAD_OSS_SUCCESS
:
"文件上传至OSS成功"
,
FILE_UPLOAD_OSS_SUCCESS
:
'文件上传至OSS成功'
,
FILE_UPLOAD_OSS_FAIL
:
"文件上传至OSS失败"
,
FILE_UPLOAD_OSS_FAIL
:
'文件上传至OSS失败'
,
FILE_UPLOAD_OSS_TIMEOUT_OR_OTHER
:
"文件上传至OSS超时或其他错误"
,
FILE_UPLOAD_OSS_TIMEOUT_OR_OTHER
:
'文件上传至OSS超时或其他错误'
,
FILE_PART_SIZE_TOO_SMALL
:
"分片大小不能小于100kb"
,
FILE_PART_SIZE_TOO_SMALL
:
'分片大小不能小于100kb'
}
,
}
},
},
/**
/**
* 生成回调对象
* 生成回调对象
...
@@ -267,11 +253,11 @@ const common = {
...
@@ -267,11 +253,11 @@ const common = {
* @return {Object} 回调对象
* @return {Object} 回调对象
*/
*/
genCallBacks
(
key
,
data
=
{})
{
genCallBacks
(
key
,
data
=
{})
{
const
{
CODE
,
MESSAGE
}
=
common
.
constants
;
const
{
CODE
,
MESSAGE
}
=
common
.
constants
return
{
return
{
code
:
CODE
[
key
],
code
:
CODE
[
key
],
message
:
MESSAGE
[
key
],
message
:
MESSAGE
[
key
],
data
,
data
}
;
}
}
,
}
}
;
}
mixin/uploader.js
View file @
fa99553
...
@@ -42,6 +42,7 @@ export default {
...
@@ -42,6 +42,7 @@ export default {
this
.
uploadSuccess
(
recordId
,
filePathOnServer
,
filePathOnClient
,
fileName
)
this
.
uploadSuccess
(
recordId
,
filePathOnServer
,
filePathOnClient
,
fileName
)
}
else
{
}
else
{
this
.
polling
()
this
.
polling
()
this
.
$sendSentry
(
`上传失败:文件名-
${
fileName
}
错误码-
${
data
.
oss
.
res
.
status
}
,失败原因-
${
JSON
.
stringify
(
data
.
oss
.
res
)}
`
);
}
}
},
},
/**
/**
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment