Skip to content

Commit d2a1af7

Browse files
committed
feat: add ios implementation
1 parent 72b291b commit d2a1af7

File tree

12 files changed

+417
-36
lines changed

12 files changed

+417
-36
lines changed

packages/capacitor-plugin/.gitignore

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@ DerivedData/
1616
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
1717
.netrc
1818

19-
2019
# macOS files
2120
.DS_Store
2221

23-
24-
2522
# Based on Android gitignore template: https://github.com/github/gitignore/blob/HEAD/Android.gitignore
2623

2724
# Built application files
@@ -67,4 +64,4 @@ captures
6764
#*.jks
6865

6966
# External native build folder generated in Android Studio 2.2 and later
70-
.externalNativeBuild
67+
.externalNativeBuild

packages/capacitor-plugin/CapacitorFileTransfer.podspec

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ Pod::Spec.new do |s|
1212
s.source = { :git => package['repository']['url'], :tag => s.version.to_s }
1313
s.source_files = 'ios/Sources/FileTransferPlugin/*.{swift,h,m,c,cc,mm,cpp}'
1414
s.ios.deployment_target = '14.0'
15-
#s.dependency 'FileTransferLib', spec='~> 1.0'
16-
# temporary xcframeowrk dependency - TODO update to official pod (commented line above) once published
17-
s.vendored_frameworks = 'ios/Sources/*/IONFileTransferLib.xcframework'
1815
s.dependency 'Capacitor'
16+
s.dependency 'IONFileTransferLib', spec='~> 1.0'
1917
s.swift_version = '5.1'
2018
end

packages/capacitor-plugin/Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ let package = Package(
1010
targets: ["FileTransferPlugin"])
1111
],
1212
dependencies: [
13-
.package(url: "https://github.com/ionic-team/capacitor-swift-pm.git", from: "7.0.0")
13+
.package(url: "https://github.com/ionic-team/capacitor-swift-pm.git", branch: "main")
1414
],
1515
targets: [
1616
.binaryTarget(
1717
name: "IONFileTransferLib",
1818
// url: "https://github.com/ionic-team/ion-ios-filetransfer/releases/download/1.0.0/IONFileTransferLib.zip",
1919
// checksum: "<compute_checksum>" // sha-256
20-
path: "./ios/Sources/FileTransferPlugin/IONFileTransferLib.xcframework"
20+
path: "./ios/Sources/FileTransferPlugin/IONFileTransferLib.zip"
2121
),
2222
.target(
2323
name: "FileTransferPlugin",

packages/capacitor-plugin/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Perform an HTTP request to upload a file to a server
6565
### addListener('progress', ...)
6666

6767
```typescript
68-
addListener(eventName: 'progress', listenerFunc: (progress: ProgressStatus) => void) => Promise<PluginListenerHandle>
68+
addListener(eventName: "progress", listenerFunc: (progress: ProgressStatus) => void) => Promise<PluginListenerHandle>
6969
```
7070

7171
Add a listener to file transfer (download or upload) progress events.

packages/capacitor-plugin/ios/Sources/FileTransferPlugin/FileTransfer.swift

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import Foundation
2+
import IONFileTransferLib
3+
4+
/// Represents errors that can occur during file transfer operations.
5+
///
6+
/// The `FileTransferError` enum defines various error cases that may arise
7+
/// when performing file transfer operations, such as invalid parameters,
8+
/// connection issues, or permission denial.
9+
enum FileTransferError: Error {
10+
11+
/// Indicates that the input parameters provided to a method are invalid.
12+
case invalidParameters
13+
14+
/// Indicates that the provided server URL is invalid.
15+
///
16+
/// - Parameter url: The invalid URL string.
17+
case invalidServerUrl(url: String)
18+
19+
/// Indicates that the URL to connect to is empty.
20+
case urlEmpty
21+
22+
/// Indicates that the user denied the required permissions for the operation.
23+
case permissionDenied
24+
25+
/// Indicates that the file does not exist.
26+
///
27+
/// - Parameter cause: The underlying error, if available.
28+
case fileDoesNotExist(cause: Error?)
29+
30+
/// Indicates a connection error occurred.
31+
///
32+
/// - Parameter cause: The underlying error, if available.
33+
case connectionError(cause: Error?)
34+
35+
/// Indicates that the server responded with HTTP 304 – Not Modified.
36+
///
37+
/// - Parameters:
38+
/// - responseCode: The HTTP response code.
39+
/// - responseBody: The response body, if available.
40+
/// - headers: The response headers, if available.
41+
case notModified(responseCode: Int, responseBody: String?, headers: [String: String]?)
42+
43+
/// Indicates a generic error occurred.
44+
///
45+
/// - Parameter cause: The underlying error, if available.
46+
case genericError(cause: Error?)
47+
48+
/// A unique error code associated with the error.
49+
///
50+
/// The error code is formatted as `OS-PLUG-FLTR-XXXX`, where `XXXX` is a
51+
/// numeric identifier for the specific error case.
52+
var code: String {
53+
let code: Int
54+
switch self {
55+
case .invalidParameters: code = 5
56+
case .invalidServerUrl: code = 6
57+
case .urlEmpty: code = 6
58+
case .permissionDenied: code = 7
59+
case .fileDoesNotExist: code = 8
60+
case .connectionError: code = 9
61+
case .notModified: code = 10
62+
case .genericError: code = 11
63+
}
64+
return String(format: "OS-PLUG-FLTR-%04d", code)
65+
}
66+
67+
/// A human-readable description of the error.
68+
///
69+
/// This property provides a detailed explanation of the error, which can
70+
/// be used for logging or displaying error messages to the user.
71+
var description: String {
72+
switch self {
73+
case .invalidParameters: "The method's input parameters aren't valid."
74+
case .invalidServerUrl(url: let url): "Invalid server URL was provided - \(url)"
75+
case .urlEmpty: "URL to connect to is either null or empty."
76+
case .permissionDenied: "Unable to perform operation, user denied permission request."
77+
case .fileDoesNotExist: "Operation failed because file does not exist."
78+
case .connectionError: "Failed to connect to server."
79+
case .notModified: "The server responded with HTTP 304 – Not Modified. If you want to avoid this, check your headers related to HTTP caching."
80+
case .genericError: "The operation failed with an error."
81+
}
82+
}
83+
84+
/// The underlying cause of the error, if available.
85+
///
86+
/// This property provides additional context for the error, such as the
87+
/// original error that triggered the current error.
88+
var cause: Error? {
89+
switch self {
90+
case .invalidParameters: return nil
91+
case .invalidServerUrl: return nil
92+
case .urlEmpty: return nil
93+
case .permissionDenied: return nil
94+
case .fileDoesNotExist(cause: let cause): return cause
95+
case .connectionError(cause: let cause): return cause
96+
case .notModified: return nil
97+
case .genericError(cause: let cause): return cause
98+
}
99+
}
100+
}
101+
102+
/// Extension to map `IONFLTRException` to `FileTransferError`.
103+
extension IONFLTRException {
104+
105+
/// Converts an `IONFLTRException` to a corresponding `FileTransferError`.
106+
///
107+
/// This method maps specific cases of `IONFLTRException` to their
108+
/// equivalent `FileTransferError` cases, providing a unified error
109+
/// representation for file transfer operations.
110+
///
111+
/// - Returns: A `FileTransferError` instance representing the exception.
112+
func toFileTransferError() -> FileTransferError {
113+
switch self {
114+
case .invalidPath(_):
115+
return .invalidParameters
116+
case .emptyURL(_):
117+
return .urlEmpty
118+
case .invalidURL(let url):
119+
return .invalidServerUrl(url: url)
120+
case .fileDoesNotExist(let cause):
121+
return .fileDoesNotExist(cause: cause)
122+
case .cannotCreateDirectory(_, let cause):
123+
return .genericError(cause: cause)
124+
case .httpError(let responseCode, let responseBody, let headers):
125+
return responseCode == 304
126+
? .notModified(
127+
responseCode: responseCode,
128+
responseBody: responseBody,
129+
headers: headers
130+
) : .genericError(cause: nil)
131+
case .connectionError(let cause):
132+
return .connectionError(cause: cause)
133+
case .transferError(let cause):
134+
return .genericError(cause: cause)
135+
case .unknownError(let cause):
136+
return .genericError(cause: cause)
137+
}
138+
}
139+
}

0 commit comments

Comments
 (0)