Skip to content

Commit

Permalink
feat: support on UploadFile (#46)
Browse files Browse the repository at this point in the history
* feat: support on UploadFile
  • Loading branch information
wzxha authored Apr 1, 2024
1 parent c5bc9d4 commit 797443d
Show file tree
Hide file tree
Showing 17 changed files with 521 additions and 92 deletions.
22 changes: 21 additions & 1 deletion AliyunpanSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
F447857D2BBAB9AE00FE8247 /* TestFile1.txt in Resources */ = {isa = PBXBuildFile; fileRef = F447857C2BBAB9AE00FE8247 /* TestFile1.txt */; };
F47FADDE2B14767D00EC0D8D /* AliyunpanSDK.swift in Sources */ = {isa = PBXBuildFile; fileRef = F47FADB72B14767D00EC0D8D /* AliyunpanSDK.swift */; };
F47FADDF2B14767D00EC0D8D /* AliyunpanPKCECredentials.swift in Sources */ = {isa = PBXBuildFile; fileRef = F47FADB92B14767D00EC0D8D /* AliyunpanPKCECredentials.swift */; };
F47FADE12B14767D00EC0D8D /* AliyunpanAppJumper.swift in Sources */ = {isa = PBXBuildFile; fileRef = F47FADBB2B14767D00EC0D8D /* AliyunpanAppJumper.swift */; };
Expand Down Expand Up @@ -60,6 +61,7 @@
F498215C2B188A6D006559CC /* GetVipFeatureList.swift in Sources */ = {isa = PBXBuildFile; fileRef = F498213D2B188A6D006559CC /* GetVipFeatureList.swift */; };
F498215E2B188A6D006559CC /* GetVipFeatureTrial.swift in Sources */ = {isa = PBXBuildFile; fileRef = F498213F2B188A6D006559CC /* GetVipFeatureTrial.swift */; };
F49821602B188C4C006559CC /* AliyunpanSpaceInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = F498215F2B188C4C006559CC /* AliyunpanSpaceInfo.swift */; };
F4A0FF742BA2F95800E83A46 /* AliyunpanLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4A0FF732BA2F95800E83A46 /* AliyunpanLogger.swift */; };
F4A37F1C2B562081009AC6AA /* AliyunpanTokenCredentials.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4A37F1B2B562081009AC6AA /* AliyunpanTokenCredentials.swift */; };
F4B3F7332B29859100D9E122 /* CredentialTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4B3F7322B29859100D9E122 /* CredentialTests.swift */; };
F4BD1B642B29A11B002BEA2A /* Platform.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4BD1B632B29A11B002BEA2A /* Platform.swift */; };
Expand All @@ -79,6 +81,7 @@
F4BD1C622B34366E002BEA2A /* DownloaderOperationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4BD1C602B343423002BEA2A /* DownloaderOperationTests.swift */; };
F4BD1C652B34403F002BEA2A /* DownloaderTaskTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4BD1C632B343FD6002BEA2A /* DownloaderTaskTests.swift */; };
F4C6F2312B060C4B003A06B3 /* AliyunpanSDK.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F4C6F2262B060C4B003A06B3 /* AliyunpanSDK.framework */; };
F4DDB1CE2BB17E4C0005DA0C /* AliyunpanUploader.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4DDB1CD2BB17E4C0005DA0C /* AliyunpanUploader.swift */; };
F4E9F9692B148ABA00DC8DBF /* Crypto.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4E9F9672B148ABA00DC8DBF /* Crypto.swift */; };
F4E9F96A2B148ABA00DC8DBF /* URL+AliyunpanSDK.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4E9F9682B148ABA00DC8DBF /* URL+AliyunpanSDK.swift */; };
F4E9F96C2B148AEE00DC8DBF /* AliyunpanMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4E9F96B2B148AEE00DC8DBF /* AliyunpanMessage.swift */; };
Expand All @@ -97,6 +100,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
F447857C2BBAB9AE00FE8247 /* TestFile1.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = TestFile1.txt; path = Tests/TestFile1.txt; sourceTree = SOURCE_ROOT; };
F47FADB72B14767D00EC0D8D /* AliyunpanSDK.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AliyunpanSDK.swift; sourceTree = "<group>"; };
F47FADB92B14767D00EC0D8D /* AliyunpanPKCECredentials.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AliyunpanPKCECredentials.swift; sourceTree = "<group>"; };
F47FADBB2B14767D00EC0D8D /* AliyunpanAppJumper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AliyunpanAppJumper.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -152,6 +156,7 @@
F498213D2B188A6D006559CC /* GetVipFeatureList.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GetVipFeatureList.swift; sourceTree = "<group>"; };
F498213F2B188A6D006559CC /* GetVipFeatureTrial.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GetVipFeatureTrial.swift; sourceTree = "<group>"; };
F498215F2B188C4C006559CC /* AliyunpanSpaceInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AliyunpanSpaceInfo.swift; sourceTree = "<group>"; };
F4A0FF732BA2F95800E83A46 /* AliyunpanLogger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AliyunpanLogger.swift; sourceTree = "<group>"; };
F4A37F1B2B562081009AC6AA /* AliyunpanTokenCredentials.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AliyunpanTokenCredentials.swift; sourceTree = "<group>"; };
F4B3F7142B202CB500D9E122 /* DownloaderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DownloaderTests.swift; sourceTree = "<group>"; };
F4B3F7322B29859100D9E122 /* CredentialTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CredentialTests.swift; sourceTree = "<group>"; };
Expand All @@ -172,6 +177,7 @@
F4BD1C632B343FD6002BEA2A /* DownloaderTaskTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DownloaderTaskTests.swift; sourceTree = "<group>"; };
F4C6F2262B060C4B003A06B3 /* AliyunpanSDK.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = AliyunpanSDK.framework; sourceTree = BUILT_PRODUCTS_DIR; };
F4C6F2302B060C4B003A06B3 /* AliyunpanSDKTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = AliyunpanSDKTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
F4DDB1CD2BB17E4C0005DA0C /* AliyunpanUploader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AliyunpanUploader.swift; sourceTree = "<group>"; };
F4E9F9672B148ABA00DC8DBF /* Crypto.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Crypto.swift; sourceTree = "<group>"; };
F4E9F9682B148ABA00DC8DBF /* URL+AliyunpanSDK.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "URL+AliyunpanSDK.swift"; sourceTree = "<group>"; };
F4E9F96B2B148AEE00DC8DBF /* AliyunpanMessage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AliyunpanMessage.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -217,8 +223,9 @@
F47FADC62B14767D00EC0D8D /* AliyunpanError.swift */,
F47FADD62B14767D00EC0D8D /* AliyunpanClient.swift */,
F47FADB72B14767D00EC0D8D /* AliyunpanSDK.swift */,
F47FADBE2B14767D00EC0D8D /* AliyunpanSDK.h */,
F4A0FF732BA2F95800E83A46 /* AliyunpanLogger.swift */,
F4BD1B632B29A11B002BEA2A /* Platform.swift */,
F47FADBE2B14767D00EC0D8D /* AliyunpanSDK.h */,
F47FADD72B14767D00EC0D8D /* Info.plist */,
);
path = AliyunpanSDK;
Expand All @@ -241,6 +248,7 @@
F47FADC02B14767D00EC0D8D /* HTTPRequest */ = {
isa = PBXGroup;
children = (
F4A0FF722BA2F85700E83A46 /* Upload */,
F4BD1C422B302EF0002BEA2A /* Download */,
F49821192B1742B6006559CC /* DebugDescription.swift */,
F47FADC12B14767D00EC0D8D /* URLConvertible.swift */,
Expand Down Expand Up @@ -288,6 +296,7 @@
F47FADDA2B14767D00EC0D8D /* AliyunpanSDKTests */ = {
isa = PBXGroup;
children = (
F447857C2BBAB9AE00FE8247 /* TestFile1.txt */,
F47FADDC2B14767D00EC0D8D /* AliyunpanSDKTests.swift */,
F49745FE2B1F100C0043A4D7 /* AliyunpanClientTests.swift */,
F4B3F7322B29859100D9E122 /* CredentialTests.swift */,
Expand Down Expand Up @@ -370,6 +379,14 @@
path = Vip;
sourceTree = "<group>";
};
F4A0FF722BA2F85700E83A46 /* Upload */ = {
isa = PBXGroup;
children = (
F4DDB1CD2BB17E4C0005DA0C /* AliyunpanUploader.swift */,
);
path = Upload;
sourceTree = "<group>";
};
F4BD1C422B302EF0002BEA2A /* Download */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -512,6 +529,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
F447857D2BBAB9AE00FE8247 /* TestFile1.txt in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -583,6 +601,7 @@
F47FADE72B14767D00EC0D8D /* HTTPRequest.swift in Sources */,
F498214E2B188A6D006559CC /* TrashFileToRecyclebin.swift in Sources */,
F47FADEB2B14767D00EC0D8D /* AliyunpanError.swift in Sources */,
F4A0FF742BA2F95800E83A46 /* AliyunpanLogger.swift in Sources */,
F47FADE82B14767D00EC0D8D /* HTTPMethod.swift in Sources */,
F498214B2B188A6D006559CC /* GetFileByPath.swift in Sources */,
F498215A2B188A6D006559CC /* GetAsyncTask.swift in Sources */,
Expand All @@ -598,6 +617,7 @@
F4BD1C362B2AD8E1002BEA2A /* Task+AliyunpanSDK.swift in Sources */,
F4BD1B642B29A11B002BEA2A /* Platform.swift in Sources */,
F49821432B188A6D006559CC /* GetVideoRecentList.swift in Sources */,
F4DDB1CE2BB17E4C0005DA0C /* AliyunpanUploader.swift in Sources */,
F498214C2B188A6D006559CC /* MoveFile.swift in Sources */,
F47FADE62B14767D00EC0D8D /* URLConvertible.swift in Sources */,
F4E9F96A2B148ABA00DC8DBF /* URL+AliyunpanSDK.swift in Sources */,
Expand Down
39 changes: 8 additions & 31 deletions Demo/Demo/Demo-iOS/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ class ViewController: UIViewController {
}

private func uploadFile(withURL url: URL) {
/// 单片,小于 5G
/// 大于 5G 请分片上传
/// https://www.yuque.com/aliyundrive/zpfszx/ezlzok#C8JdZ
Task {
do {
let driveInfo = try await client
Expand All @@ -75,35 +72,15 @@ class ViewController: UIViewController {

let driveId = driveInfo.default_drive_id

let response = try await client
.authorize()
.send(
AliyunpanScope.File.CreateFile(
.init(
drive_id: driveId,
parent_file_id: "root",
name: url.lastPathComponent,
check_name_mode: .auto_rename)))
let file = try await client.uploader
.upload(
fileURL: url,
fileName: "test_\(Date().timeIntervalSince1970).pdf",
driveId: driveId,
folderId: "root",
useProof: true)

if let uploadURL = response.part_info_list?.first?.upload_url {
var urlRequest = URLRequest(url: uploadURL)
urlRequest.httpMethod = "put"
urlRequest.allHTTPHeaderFields = [
"Content-Type": "" // 不能传 Cotent-Type,否则会失败
]
_ = try await URLSession.shared.upload(for: urlRequest, fromFile: url)

let file = try await client
.authorize()
.send(
AliyunpanScope.File.CompleteUpload(
.init(
drive_id: driveId,
file_id: response.file_id,
upload_id: response.upload_id ?? "")))

showAlert(message: file.description)
}
showAlert(message: file.description)
} catch {
print(error)
}
Expand Down
6 changes: 3 additions & 3 deletions Demo/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- AliyunpanSDK (0.1.18)
- AliyunpanSDK (0.2.0)

DEPENDENCIES:
- AliyunpanSDK (from `../`)
Expand All @@ -9,8 +9,8 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
AliyunpanSDK: 59f71bc271322edb3eb39c6260a7a4ef616410c7
AliyunpanSDK: 9677ed52f2519f512e91bf3097438c35772535e3

PODFILE CHECKSUM: 40334940ba0e4f081833162ea871b81051e79c17

COCOAPODS: 1.14.3
COCOAPODS: 1.15.2
44 changes: 29 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,41 @@ client.authorize(credentials: credentials)

```swift
// Concurrency
try await client
.authorize() // 默认 pkce
.send(AliyunpanScope.User.GetUsersInfo()) // -> GetUsersInfo.Response

try await client
.authorize()
.send(
AliyunpanScope.File.GetFileList(
.init(drive_id: driveId, parent_file_id: "root")))) // -> GetFileList.Response
try await client.send(
AliyunpanScope.User.GetUsersInfo()) // -> GetUsersInfo.Response

try await client.send(
AliyunpanScope.File.GetFileList(
.init(drive_id: driveId, parent_file_id: "root")))) // -> GetFileList.Response

// Closure
client
.authorize()
.send(
AliyunpanScope.User.GetUsersInfo()) { result in
/// do something
}
client.send(
AliyunpanScope.User.GetUsersInfo()) { result in
/// do something
}
```

## 高级功能

### 上传
```swift
let uploader = client.uploader

// 上传
let task = Task {
let file = try? await uploader.upload(
fileURL: url,
fileName: fileName,
driveId: driveId,
folderId: folderId,
useProof: true // 是否开启快传
)
}

// 取消
task.cancel()
```

### 下载
```swift
let downloader = client.downloader
Expand Down
41 changes: 41 additions & 0 deletions Sources/AliyunpanSDK/AliyunpanClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ public class AliyunpanClient {
return downloader
}()

/// 上传器
public lazy var uploader: AliyunpanUploader = {
let uploader = AliyunpanUploader()
uploader.client = self
return uploader
}()

public init(_ config: AliyunpanClientConfig) {
self.config = config

Expand Down Expand Up @@ -91,6 +98,40 @@ public class AliyunpanClient {
}
return token
}

/// 发送请求
///
/// - throws:
/// `DecodingError`: JSON 解析错误
/// `AliyunpanAuthorizeError`: 授权错误
/// `AliyunpanServerError`: 服务端错误
/// `AliyunpanNetworkSystemError`: 网络系统错误
public func send<T: AliyunpanCommand>(_ command: T) async throws -> T.Response where T.Response: Decodable {
guard let token = await token else {
throw AliyunpanError.AuthorizeError.accessTokenInvalid
}
return try await token.send(command)
}

/// 发送请求
///
/// - throws:
/// `DecodingError`: JSON 解析错误
/// `AliyunpanAuthorizeError`: 授权错误
/// `AliyunpanServerError`: 服务端错误
/// `AliyunpanNetworkSystemError`: 网络系统错误
public func send<T: AliyunpanCommand>(
_ command: T,
completionHandle: @escaping (Result<T.Response, Error>) -> Void) where T.Response: Decodable {
Task {
do {
let response = try await send(command)
completionHandle(.success(response))
} catch {
completionHandle(.failure(error))
}
}
}
}

extension AliyunpanToken {
Expand Down
14 changes: 13 additions & 1 deletion Sources/AliyunpanSDK/AliyunpanError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public struct AliyunpanError {
case authorizeFailed(error: String?, errorMsg: String?)
/// 验证码授权超时
case qrCodeAuthorizeTimeout
/// 未授权或授权已过期
case accessTokenInvalid
}

/// 网络层错误
Expand Down Expand Up @@ -59,9 +61,11 @@ public struct AliyunpanError {
case forbiddenDriveLocked = "ForbiddenDriveLocked"
/// 非法访问drive
case forbiddenDriveNotValid = "ForbiddenDriveNotValid"
/// 快传预检匹配成功
case preHashMatched = "PreHashMatched"
}

public let code: Code?
public let code: Code
public let message: String?
public let requestId: String?

Expand All @@ -81,6 +85,14 @@ public struct AliyunpanError {
/// 缺少 client
case invalidClient
}

/// 上传错误
public enum UploadError: Error {
/// 缺少 client
case invalidClient
/// 快传预检查失败
case preHashNotMatched
}

/// 系统级网络层错误
public enum NetworkSystemError: Error {
Expand Down
37 changes: 37 additions & 0 deletions Sources/AliyunpanSDK/AliyunpanLogger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// AliyunpanLogger.swift
// AliyunpanSDK
//
// Created by zhaixian on 2024/3/14.
//

import Foundation

public enum AliyunpanLogLevel: Int {
case debug
case info
case warn
case error

var msg: String {
switch self {
case .debug:
return "DEBUG"
case .info:
return "INFO"
case .warn:
return "⚠️"
case .error:
return ""
}
}
}

class Logger {
static func log(_ level: AliyunpanLogLevel, msg: String) {
guard level.rawValue >= Aliyunpan.logLevel.rawValue else {
return
}
print("[AliyunpanSDK][\(level.msg)]\(msg)")
}
}
Loading

0 comments on commit 797443d

Please sign in to comment.