Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
galaxy-iOS
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mobile-group
galaxy-iOS
Commits
dd27986c
Commit
dd27986c
authored
May 24, 2024
by
pete谢兆麟
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
oss优化
parent
a9d5a4ec
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
60 additions
and
54 deletions
+60
-54
YHOSSManager.swift
galaxy/galaxy/Classes/Tools/Upload/YHOSSManager.swift
+60
-54
No files found.
galaxy/galaxy/Classes/Tools/Upload/YHOSSManager.swift
View file @
dd27986c
...
...
@@ -71,38 +71,41 @@ class YHOSSManager: NSObject, URLSessionDelegate {
callBackBlock
(
nil
,
YHErrorModel
())
return
}
request
.
uploadingData
=
imageData
request
.
bucketName
=
self
.
model
.
bucket
let
timestamp
=
Date
()
.
timeIntervalSince1970
let
randomInt
=
Int
.
random
(
in
:
1
...
100
)
// 随机生成1到10之间的整数
let
name
=
"
\(
UInt64
(
timestamp
)
)
"
+
"
\(
randomInt
)
"
+
".jpg"
let
dateFormatter
=
DateFormatter
()
dateFormatter
.
dateFormat
=
"yyyyMMdd"
let
currentDateMMdd
=
dateFormatter
.
string
(
from
:
Date
())
self
.
fileName
.
append
(
name
)
request
.
objectKey
=
"4001001"
+
"/"
+
currentDateMMdd
+
"/"
+
"
\(
name
)
"
request
.
contentDisposition
=
"inline"
request
.
uploadProgress
=
{
(
bytesSent
:
Int64
,
totalBytesSent
:
Int64
,
totalBytesExpectedToSend
:
Int64
)
->
Void
in
printLog
(
"bytesSent:
\(
bytesSent
)
,totalBytesSent:
\(
totalBytesSent
)
,totalBytesExpectedToSend:
\(
totalBytesExpectedToSend
)
"
)
}
let
task
=
self
.
mClient
.
putObject
(
request
)
task
.
continue
({
(
t
)
->
Any
?
in
let
result
=
t
if
(
result
.
error
!=
nil
)
{
callBackBlock
(
nil
,
YHErrorModel
())
}
else
{
let
dateFormatter
=
DateFormatter
()
dateFormatter
.
dateFormat
=
"yyyyMMdd"
let
currentDateMMdd
=
dateFormatter
.
string
(
from
:
Date
())
let
string
=
"https://"
+
self
.
model
.
bucket
+
"."
+
"oss-cn-shenzhen.aliyuncs.com/"
+
"4001001"
+
"/"
+
currentDateMMdd
+
"/"
+
"
\(
self
.
fileName
.
first
??
""
)
"
self
.
fileName
.
removeFirst
()
print
(
"------------
\(
string
)
"
)
callBackBlock
(
string
,
YHErrorModel
())
DispatchQueue
.
global
()
.
async
{
request
.
uploadingData
=
imageData
request
.
bucketName
=
self
.
model
.
bucket
let
timestamp
=
Date
()
.
timeIntervalSince1970
let
randomInt
=
Int
.
random
(
in
:
1
...
100
)
// 随机生成1到10之间的整数
let
name
=
"
\(
UInt64
(
timestamp
)
)
"
+
"
\(
randomInt
)
"
+
".jpg"
let
dateFormatter
=
DateFormatter
()
dateFormatter
.
dateFormat
=
"yyyyMMdd"
let
currentDateMMdd
=
dateFormatter
.
string
(
from
:
Date
())
self
.
fileName
.
append
(
name
)
request
.
objectKey
=
"4001001"
+
"/"
+
currentDateMMdd
+
"/"
+
"
\(
name
)
"
request
.
contentDisposition
=
"inline"
request
.
uploadProgress
=
{
(
bytesSent
:
Int64
,
totalBytesSent
:
Int64
,
totalBytesExpectedToSend
:
Int64
)
->
Void
in
printLog
(
"bytesSent:
\(
bytesSent
)
,totalBytesSent:
\(
totalBytesSent
)
,totalBytesExpectedToSend:
\(
totalBytesExpectedToSend
)
"
)
}
return
})
.
waitUntilFinished
()
let
task
=
self
.
mClient
.
putObject
(
request
)
task
.
continue
({
(
t
)
->
Any
?
in
let
result
=
t
if
(
result
.
error
!=
nil
)
{
callBackBlock
(
nil
,
YHErrorModel
())
}
else
{
let
dateFormatter
=
DateFormatter
()
dateFormatter
.
dateFormat
=
"yyyyMMdd"
let
currentDateMMdd
=
dateFormatter
.
string
(
from
:
Date
())
let
string
=
"https://"
+
self
.
model
.
bucket
+
"."
+
"oss-cn-shenzhen.aliyuncs.com/"
+
"4001001"
+
"/"
+
currentDateMMdd
+
"/"
+
"
\(
self
.
fileName
.
first
??
""
)
"
self
.
fileName
.
removeFirst
()
print
(
"------------
\(
string
)
"
)
callBackBlock
(
string
,
YHErrorModel
())
}
return
})
.
waitUntilFinished
()
}
}
func
putFile
(
file
:
String
,
callBackBlock
:
@escaping
(
_
success
:
String
?,
_
error
:
YHErrorModel
?)
->
())
{
...
...
@@ -150,31 +153,34 @@ class YHOSSManager: NSObject, URLSessionDelegate {
}
func
getPublic
(
url
:
String
,
callBackBlock
:
@escaping
(
_
success
:
String
?,
_
error
:
YHErrorModel
?)
->
())
{
let
bucketName
=
self
.
getBucket
(
url
)
let
pathComponents
=
url
.
pathComponents
var
objectKey
=
""
for
i
in
0
..<
pathComponents
.
count
{
if
i
==
0
||
i
==
1
{
}
else
if
i
==
pathComponents
.
count
-
1
{
objectKey
=
objectKey
+
pathComponents
[
i
]
}
else
{
objectKey
=
objectKey
+
pathComponents
[
i
]
+
"/"
DispatchQueue
.
global
()
.
async
{
let
bucketName
=
self
.
getBucket
(
url
)
let
pathComponents
=
url
.
pathComponents
var
objectKey
=
""
for
i
in
0
..<
pathComponents
.
count
{
if
i
==
0
||
i
==
1
{
}
else
if
i
==
pathComponents
.
count
-
1
{
objectKey
=
objectKey
+
pathComponents
[
i
]
}
else
{
objectKey
=
objectKey
+
pathComponents
[
i
]
+
"/"
}
}
let
task
=
self
.
mClient
.
presignConstrainURL
(
withBucketName
:
bucketName
,
withObjectKey
:
objectKey
,
withExpirationInterval
:
60
*
5
)
task
.
continue
({
(
t
)
->
Any
?
in
let
result
=
t
if
(
result
.
error
!=
nil
)
{
let
error
:
NSError
=
(
task
.
error
)
!
as
NSError
_
=
error
.
description
callBackBlock
(
nil
,
YHErrorModel
())
}
else
{
let
string
=
result
.
result
as!
String
callBackBlock
(
string
,
YHErrorModel
())
}
return
})
.
waitUntilFinished
()
}
let
task
=
mClient
.
presignConstrainURL
(
withBucketName
:
bucketName
,
withObjectKey
:
objectKey
,
withExpirationInterval
:
60
*
5
)
task
.
continue
({
(
t
)
->
Any
?
in
let
result
=
t
if
(
result
.
error
!=
nil
)
{
let
error
:
NSError
=
(
task
.
error
)
!
as
NSError
_
=
error
.
description
callBackBlock
(
nil
,
YHErrorModel
())
}
else
{
let
string
=
result
.
result
as!
String
callBackBlock
(
string
,
YHErrorModel
())
}
return
})
.
waitUntilFinished
()
}
func
getBucket
(
_
url
:
String
)
->
String
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
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 comment