catalyst-core 0.0.3-canary.9 → 0.1.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -5
- package/bin/catalyst.js +28 -15
- package/changelog.md +139 -30
- package/dist/native/androidProject/app/build.gradle.kts +125 -15
- package/dist/native/androidProject/app/proguard-rules.pro +85 -14
- package/dist/native/androidProject/app/src/debug/res/xml/network_security_config.xml +8 -0
- package/dist/native/androidProject/app/src/main/AndroidManifest.xml +10 -3
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/BridgeMessageValidator.kt +552 -0
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/CatalystConstants.kt +109 -0
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/CustomWebview.kt +605 -124
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/MainActivity.kt +405 -47
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/MetricsMonitor.kt +341 -0
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/NativeBridge.kt +954 -71
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/SplashActivity.kt +111 -0
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/URLWhitelistManager.kt +231 -0
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/Utils.kt +49 -55
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/WebCacheManager.kt +81 -8
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/design/DesignTokens.kt +307 -0
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/security/EmulatorDetector.kt +203 -0
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/security/FridaDetector.kt +187 -0
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/security/SecurityAlertHandler.kt +170 -0
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/security/SecurityAlertUI.kt +366 -0
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/security/SecurityBottomSheet.kt +138 -0
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/security/SecurityCheckManager.kt +511 -0
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/security/SecurityCheckScheduler.kt +68 -0
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/utils/BridgeUtils.kt +47 -11
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/utils/DeviceInfoUtils.kt +38 -13
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/utils/DownloadUtils.kt +36 -14
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/utils/FileUtils.kt +3 -2
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/utils/FrameworkServerUtils.kt +57 -8
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/utils/IntentUtils.kt +31 -6
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/utils/KeyboardUtil.kt +82 -0
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/utils/NetworkUtils.kt +105 -0
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/utils/NotificationConstants.kt +28 -0
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/utils/NotificationUtils.kt +639 -0
- package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/utils/SafeAreaUtils.kt +62 -0
- package/dist/native/androidProject/app/src/main/res/drawable/splashscreen_fallback.png +0 -0
- package/dist/native/androidProject/app/src/main/res/layout/activity_main.xml +1 -0
- package/dist/native/androidProject/app/src/main/res/layout/activity_splash.xml +20 -0
- package/dist/native/androidProject/app/src/main/res/values/themes.xml +23 -0
- package/dist/native/androidProject/app/src/main/res/values-night/themes.xml +11 -0
- package/dist/native/androidProject/app/src/main/res/xml/network_security_config.xml +12 -0
- package/dist/native/androidProject/app/src/noFcm/java/io/yourname/androidproject/utils/NotificationManager.kt +166 -0
- package/dist/native/androidProject/app/src/noFcm/java/io/yourname/androidproject/utils/PushNotificationUtils.kt +94 -0
- package/dist/native/androidProject/app/src/test/java/io/yourname/androidproject/BridgeMessageValidatorTest.kt +267 -0
- package/dist/native/androidProject/app/src/test/java/io/yourname/androidproject/BridgeUtilsTest.kt +286 -0
- package/dist/native/androidProject/app/src/test/java/io/yourname/androidproject/CameraUtilsTest.kt +296 -0
- package/dist/native/androidProject/app/src/test/java/io/yourname/androidproject/ConfigMappingTest.kt +594 -0
- package/dist/native/androidProject/app/src/test/java/io/yourname/androidproject/CustomWebviewTest.kt +466 -0
- package/dist/native/androidProject/app/src/test/java/io/yourname/androidproject/DeviceInfoUtilsTest.kt +187 -0
- package/dist/native/androidProject/app/src/test/java/io/yourname/androidproject/FileUtilsTest.kt +304 -0
- package/dist/native/androidProject/app/src/test/java/io/yourname/androidproject/NativeBridgeTest.kt +424 -0
- package/dist/native/androidProject/app/src/test/java/io/yourname/androidproject/NotificationUtilsTest.kt +469 -0
- package/dist/native/androidProject/app/src/test/java/io/yourname/androidproject/SecurityBridgeTest.kt +199 -0
- package/dist/native/androidProject/app/src/test/java/io/yourname/androidproject/URLWhitelistManagerTest.kt +462 -0
- package/dist/native/androidProject/app/src/test/java/io/yourname/androidproject/WebCacheManagerTest.kt +391 -0
- package/dist/native/androidProject/app/src/withFcm/java/io/yourname/androidproject/utils/NotificationManager.kt +224 -0
- package/dist/native/androidProject/app/src/withFcm/java/io/yourname/androidproject/utils/PushNotificationUtils.kt +399 -0
- package/dist/native/androidProject/build.gradle.kts +6 -0
- package/dist/native/androidProject/gradle/libs.versions.toml +8 -2
- package/dist/native/androidSetup.js +4 -3
- package/dist/native/assets/catalyst.png +0 -0
- package/dist/native/bridge/WebBridge.js +16 -3
- package/dist/native/bridge/constants/NativeInterfaces.js +28 -10
- package/dist/native/bridge/errors.js +1 -1
- package/dist/native/bridge/hooks.js +76 -16
- package/dist/native/bridge/safeArea.js +5 -0
- package/dist/native/bridge/standardizedHooks.js +23 -5
- package/dist/native/bridge/useBaseHook.js +7 -9
- package/dist/native/bridge/utils/CameraUtils.js +58 -0
- package/dist/native/bridge/utils/FileObjectConverter.js +40 -0
- package/dist/native/bridge/utils/NativeBridge.js +57 -8
- package/dist/native/buildAppAndroid.js +115 -43
- package/dist/native/buildAppIos.js +291 -26
- package/dist/native/iosnativeWebView/Configurations/Debug.xcconfig +21 -0
- package/dist/native/iosnativeWebView/Configurations/Release.xcconfig +24 -0
- package/dist/native/iosnativeWebView/Configurations/Shared.xcconfig +50 -0
- package/dist/native/iosnativeWebView/Package.resolved +32 -0
- package/dist/native/iosnativeWebView/Package.swift +37 -0
- package/dist/native/iosnativeWebView/Sources/CatalystNotifications/LocalNotificationHandler.swift +293 -0
- package/dist/native/iosnativeWebView/Sources/CatalystNotifications/NotificationManager.swift +299 -0
- package/dist/native/iosnativeWebView/Sources/CatalystNotifications/PushNotificationHandler.swift +199 -0
- package/dist/native/iosnativeWebView/Sources/Core/Constants/CatalystConstants.swift +125 -0
- package/dist/native/iosnativeWebView/Sources/Core/Constants/ConfigConstants.swift +70 -0
- package/dist/native/iosnativeWebView/Sources/Core/Notifications/NotificationModels.swift +156 -0
- package/dist/native/iosnativeWebView/Sources/Core/Protocols/NotificationHandlerProtocol.swift +35 -0
- package/dist/native/iosnativeWebView/Sources/Core/Utils/BootTimingUtility.swift +26 -0
- package/dist/native/iosnativeWebView/{iosnativeWebView → Sources/Core/Utils}/CacheManager.swift +69 -36
- package/dist/native/iosnativeWebView/Sources/Core/Utils/CameraTransportUtils.swift +363 -0
- package/dist/native/iosnativeWebView/Sources/Core/Utils/DeviceInfoUtils.swift +67 -0
- package/dist/native/iosnativeWebView/Sources/Core/Utils/FilePickerHandler.swift +1053 -0
- package/dist/native/iosnativeWebView/Sources/Core/Utils/FrameworkServerUtils.swift +901 -0
- package/dist/native/iosnativeWebView/{iosnativeWebView → Sources/Core/Utils}/ImageHandler.swift +206 -20
- package/dist/native/iosnativeWebView/Sources/Core/Utils/NetworkMonitor.swift +112 -0
- package/dist/native/iosnativeWebView/Sources/Core/Utils/NotificationHandlerProvider.swift +20 -0
- package/dist/native/iosnativeWebView/Sources/Core/Utils/NullNotificationHandler.swift +85 -0
- package/dist/native/iosnativeWebView/{iosnativeWebView → Sources/Core/Utils}/ResourceURLProtocol.swift +60 -25
- package/dist/native/iosnativeWebView/Sources/Core/Utils/SafeAreaUtils.swift +132 -0
- package/dist/native/iosnativeWebView/Sources/Core/Utils/ScreenSecureManager.swift +131 -0
- package/dist/native/iosnativeWebView/Sources/Core/Utils/URLWhitelistManager.swift +224 -0
- package/dist/native/iosnativeWebView/Sources/Core/Utils/WebKitConfig.swift +43 -0
- package/dist/native/iosnativeWebView/Sources/Core/WebView/BridgeCommandHandler.swift +991 -0
- package/dist/native/iosnativeWebView/Sources/Core/WebView/BridgeDelegateHandler.swift +166 -0
- package/dist/native/iosnativeWebView/Sources/Core/WebView/BridgeFileHandler.swift +340 -0
- package/dist/native/iosnativeWebView/Sources/Core/WebView/BridgeJavaScriptInterface.swift +149 -0
- package/dist/native/iosnativeWebView/Sources/Core/WebView/BridgeMessageValidator.swift +611 -0
- package/dist/native/iosnativeWebView/Sources/Core/WebView/NativeBridge.swift +334 -0
- package/dist/native/iosnativeWebView/Sources/Core/WebView/SplashView.swift +95 -0
- package/dist/native/iosnativeWebView/Sources/Core/WebView/SplashViewModel.swift +86 -0
- package/dist/native/iosnativeWebView/Sources/Core/WebView/WebView.swift +181 -0
- package/dist/native/iosnativeWebView/Sources/Core/WebView/WebViewModel.swift +182 -0
- package/dist/native/iosnativeWebView/Sources/Core/WebView/WebViewNavigationDelegate.swift +395 -0
- package/dist/native/iosnativeWebView/iosnativeWebView/AppDelegate.swift +84 -0
- package/dist/native/iosnativeWebView/iosnativeWebView/Assets.xcassets/AccentColor.colorset/Contents.json +8 -8
- package/dist/native/iosnativeWebView/iosnativeWebView/Assets.xcassets/AppIcon.appiconset/Contents.json +10 -10
- package/dist/native/iosnativeWebView/iosnativeWebView/Assets.xcassets/Contents.json +4 -4
- package/dist/native/iosnativeWebView/iosnativeWebView/ContentView.swift +65 -10
- package/dist/native/iosnativeWebView/iosnativeWebView/Info-Release.plist +21 -0
- package/dist/native/iosnativeWebView/iosnativeWebView/Info.plist +13 -0
- package/dist/native/iosnativeWebView/iosnativeWebView/Preview Content/Preview Assets.xcassets/Contents.json +4 -4
- package/dist/native/iosnativeWebView/iosnativeWebView/iosnativeWebView.entitlements +12 -0
- package/dist/native/iosnativeWebView/iosnativeWebView/iosnativeWebViewApp.swift +80 -0
- package/dist/native/iosnativeWebView/iosnativeWebView/localhost.p12 +0 -0
- package/dist/native/iosnativeWebView/iosnativeWebView.xcodeproj/project.pbxproj +118 -100
- package/dist/native/iosnativeWebView/iosnativeWebView.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +69 -0
- package/dist/native/iosnativeWebView/iosnativeWebView.xcodeproj/xcshareddata/xcschemes/iosnativeWebView.xcscheme +9 -4
- package/dist/native/iosnativeWebView/iosnativeWebView.xctestplan +47 -0
- package/dist/native/iosnativeWebView/iosnativeWebViewTests/BootTimingUtilityTests.swift +236 -0
- package/dist/native/iosnativeWebView/iosnativeWebViewTests/BridgeCommandHandlerSecurityTests.swift +212 -0
- package/dist/native/iosnativeWebView/iosnativeWebViewTests/BridgeMessageValidatorTests.swift +418 -0
- package/dist/native/iosnativeWebView/iosnativeWebViewTests/CacheManagerTests.swift +390 -0
- package/dist/native/iosnativeWebView/iosnativeWebViewTests/ConfigMappingTests.swift +299 -0
- package/dist/native/iosnativeWebView/iosnativeWebViewTests/FilePickerHandlerTests.swift +383 -0
- package/dist/native/iosnativeWebView/iosnativeWebViewTests/FrameworkServerUtilsTests.swift +391 -0
- package/dist/native/iosnativeWebView/iosnativeWebViewTests/NativeBridgeTests.swift +581 -0
- package/dist/native/iosnativeWebView/iosnativeWebViewTests/NotificationHandlerTests.swift +430 -0
- package/dist/native/iosnativeWebView/iosnativeWebViewTests/ScreenSecureManagerTests.swift +121 -0
- package/dist/native/iosnativeWebView/iosnativeWebViewTests/URLWhitelistManagerTests.swift +367 -0
- package/dist/native/iosnativeWebView/iosnativeWebViewTests/WebViewTests.swift +440 -0
- package/dist/native/renameAndroidProject.js +80 -70
- package/dist/native/setupEmulatorIos.js +22 -5
- package/dist/native/setupServer.js +11 -10
- package/dist/native/terminalProgress.js +10 -10
- package/dist/native/utils.js +4 -5
- package/dist/otel.js +4 -0
- package/dist/scripts/build.js +2 -2
- package/dist/scripts/devBuild.js +2 -2
- package/dist/scripts/loadEnvironmentVariables.js +3 -1
- package/dist/scripts/scriptUtils.js +2 -2
- package/dist/sentry.js +4 -1
- package/dist/server/renderer/document/Body.js +4 -4
- package/dist/server/renderer/handler.js +7 -5
- package/dist/webpack/development.client.babel.js +1 -1
- package/mcp_v2/conversion-tasks.json +326 -0
- package/mcp_v2/knowledge-base.json +1068 -0
- package/mcp_v2/lib/helpers.js +165 -0
- package/mcp_v2/mcp.js +563 -0
- package/mcp_v2/package.json +13 -0
- package/mcp_v2/schema.sql +88 -0
- package/mcp_v2/setup.js +278 -0
- package/mcp_v2/tools/build.js +686 -0
- package/mcp_v2/tools/config.js +453 -0
- package/mcp_v2/tools/conversion.js +799 -0
- package/mcp_v2/tools/debug.js +113 -0
- package/mcp_v2/tools/knowledge.js +219 -0
- package/mcp_v2/tools/sync.js +23 -0
- package/mcp_v2/tools/tasks.js +945 -0
- package/package.json +11 -15
- package/dist/native/androidProject/.idea/.name +0 -1
- package/dist/native/androidProject/.idea/AndroidProjectSystem.xml +0 -6
- package/dist/native/androidProject/.idea/compiler.xml +0 -6
- package/dist/native/androidProject/.idea/deploymentTargetSelector.xml +0 -10
- package/dist/native/androidProject/.idea/gradle.xml +0 -18
- package/dist/native/androidProject/.idea/migrations.xml +0 -10
- package/dist/native/androidProject/.idea/misc.xml +0 -10
- package/dist/native/androidProject/.idea/runConfigurations.xml +0 -17
- package/dist/native/androidProject/.idea/vcs.xml +0 -6
- package/dist/native/iosnativeWebView/iosnativeWebView/NativeBridge.swift +0 -217
- package/dist/native/iosnativeWebView/iosnativeWebView/WebView.swift +0 -96
- package/dist/native/iosnativeWebView/iosnativeWebView/WebViewModel.swift +0 -47
- package/dist/native/iosnativeWebView/iosnativeWebView/WebViewNavigationDelegate.swift +0 -140
- package/license +0 -10
|
@@ -0,0 +1,1053 @@
|
|
|
1
|
+
//
|
|
2
|
+
// FilePickerHandler.swift
|
|
3
|
+
// iosnativeWebView
|
|
4
|
+
//
|
|
5
|
+
// Created for file picker functionality
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
import Foundation
|
|
9
|
+
import UIKit
|
|
10
|
+
import UniformTypeIdentifiers
|
|
11
|
+
import PhotosUI
|
|
12
|
+
import os
|
|
13
|
+
|
|
14
|
+
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier ?? "com.app", category: "FilePickerHandler")
|
|
15
|
+
|
|
16
|
+
struct FilePickerOptions: CustomStringConvertible {
|
|
17
|
+
let mimeType: String
|
|
18
|
+
let multiple: Bool
|
|
19
|
+
let minFiles: Int?
|
|
20
|
+
let maxFiles: Int?
|
|
21
|
+
let minFileSize: Int64?
|
|
22
|
+
let maxFileSize: Int64?
|
|
23
|
+
|
|
24
|
+
static let `default` = FilePickerOptions(
|
|
25
|
+
mimeType: "*/*",
|
|
26
|
+
multiple: false,
|
|
27
|
+
minFiles: nil,
|
|
28
|
+
maxFiles: nil,
|
|
29
|
+
minFileSize: nil,
|
|
30
|
+
maxFileSize: nil
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
static func from(raw: String?) -> FilePickerOptions {
|
|
34
|
+
guard let raw = raw?.trimmingCharacters(in: .whitespacesAndNewlines), !raw.isEmpty else {
|
|
35
|
+
return .default
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
guard raw.hasPrefix("{"), raw.hasSuffix("}"),
|
|
39
|
+
let data = raw.data(using: .utf8),
|
|
40
|
+
let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else {
|
|
41
|
+
return FilePickerOptions(
|
|
42
|
+
mimeType: raw,
|
|
43
|
+
multiple: false,
|
|
44
|
+
minFiles: nil,
|
|
45
|
+
maxFiles: nil,
|
|
46
|
+
minFileSize: nil,
|
|
47
|
+
maxFileSize: nil
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
let mimeType = (json["mimeType"] as? String)?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "*/*"
|
|
52
|
+
let explicitMultiple = json["multiple"] as? Bool ?? false
|
|
53
|
+
let minFiles = parseInt(json["minFiles"])
|
|
54
|
+
let maxFiles = parseInt(json["maxFiles"])
|
|
55
|
+
let minFileSize = parseInt64(json["minFileSize"])
|
|
56
|
+
let maxFileSize = parseInt64(json["maxFileSize"])
|
|
57
|
+
|
|
58
|
+
let inferredMultiple = explicitMultiple || (minFiles ?? 0) > 1 || (maxFiles ?? 0) > 1
|
|
59
|
+
|
|
60
|
+
return FilePickerOptions(
|
|
61
|
+
mimeType: mimeType.isEmpty ? "*/*" : mimeType,
|
|
62
|
+
multiple: inferredMultiple,
|
|
63
|
+
minFiles: minFiles,
|
|
64
|
+
maxFiles: maxFiles,
|
|
65
|
+
minFileSize: minFileSize,
|
|
66
|
+
maxFileSize: maxFileSize
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
var selectionLimit: Int {
|
|
71
|
+
guard multiple else { return 1 }
|
|
72
|
+
if let maxFiles, maxFiles > 0 { return maxFiles }
|
|
73
|
+
return 0 // Unlimited
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
func toDictionary() -> [String: Any] {
|
|
77
|
+
var dict: [String: Any] = [
|
|
78
|
+
"mimeType": mimeType,
|
|
79
|
+
"multiple": multiple
|
|
80
|
+
]
|
|
81
|
+
if let minFiles { dict["minFiles"] = minFiles }
|
|
82
|
+
if let maxFiles { dict["maxFiles"] = maxFiles }
|
|
83
|
+
if let minFileSize { dict["minFileSize"] = minFileSize }
|
|
84
|
+
if let maxFileSize { dict["maxFileSize"] = maxFileSize }
|
|
85
|
+
return dict
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
var description: String {
|
|
89
|
+
return toDictionary().map { "\($0.key)=\($0.value)" }.sorted().joined(separator: ", ")
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private static func parseInt(_ value: Any?) -> Int? {
|
|
93
|
+
switch value {
|
|
94
|
+
case let intValue as Int: return intValue
|
|
95
|
+
case let number as NSNumber: return number.intValue
|
|
96
|
+
case let doubleValue as Double: return Int(doubleValue)
|
|
97
|
+
case let stringValue as String: return Int(stringValue)
|
|
98
|
+
default: return nil
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
private static func parseInt64(_ value: Any?) -> Int64? {
|
|
103
|
+
switch value {
|
|
104
|
+
case let intValue as Int: return Int64(intValue)
|
|
105
|
+
case let int64Value as Int64: return int64Value
|
|
106
|
+
case let number as NSNumber: return number.int64Value
|
|
107
|
+
case let doubleValue as Double: return Int64(doubleValue)
|
|
108
|
+
case let stringValue as String: return Int64(stringValue)
|
|
109
|
+
default: return nil
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
private struct PickedFile {
|
|
115
|
+
let index: Int
|
|
116
|
+
let url: URL
|
|
117
|
+
let metadata: FileMetadata
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
protocol FilePickerHandlerDelegate: AnyObject {
|
|
121
|
+
func filePickerHandler(_ handler: FilePickerHandler, didFinishWith payload: [String: Any])
|
|
122
|
+
func filePickerHandlerDidCancel(_ handler: FilePickerHandler)
|
|
123
|
+
func filePickerHandler(_ handler: FilePickerHandler, didFailWithError error: Error)
|
|
124
|
+
func filePickerHandler(_ handler: FilePickerHandler, stateDidChange state: String)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
struct FileMetadata {
|
|
128
|
+
let fileName: String
|
|
129
|
+
let fileSize: Int64
|
|
130
|
+
let mimeType: String
|
|
131
|
+
let fileExtension: String
|
|
132
|
+
let lastModified: Date?
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
enum FileTransportMethod {
|
|
136
|
+
case base64
|
|
137
|
+
case frameworkServer
|
|
138
|
+
|
|
139
|
+
var name: String {
|
|
140
|
+
switch self {
|
|
141
|
+
case .base64: return "BRIDGE_BASE64"
|
|
142
|
+
case .frameworkServer: return "FRAMEWORK_SERVER"
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
struct FileProcessingResult {
|
|
148
|
+
let success: Bool
|
|
149
|
+
let fileSrc: String?
|
|
150
|
+
let fileName: String
|
|
151
|
+
let fileSize: Int64
|
|
152
|
+
let mimeType: String
|
|
153
|
+
let transport: FileTransportMethod
|
|
154
|
+
let error: String?
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
class FilePickerHandler: NSObject {
|
|
158
|
+
weak var delegate: FilePickerHandlerDelegate?
|
|
159
|
+
private var documentPicker: UIDocumentPickerViewController?
|
|
160
|
+
private var photoPicker: PHPickerViewController?
|
|
161
|
+
private var currentOptions: FilePickerOptions = .default
|
|
162
|
+
private var temporaryFiles: [URL] = []
|
|
163
|
+
|
|
164
|
+
// File size constants from unified configuration
|
|
165
|
+
private let BASE64_SIZE_LIMIT: Int64 = CatalystConstants.FileTransport.base64SizeLimit
|
|
166
|
+
private let FRAMEWORK_SERVER_SIZE_LIMIT: Int64 = CatalystConstants.FileTransport.frameworkServerSizeLimit
|
|
167
|
+
|
|
168
|
+
override init() {
|
|
169
|
+
super.init()
|
|
170
|
+
logger.debug("FilePickerHandler initialized")
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// MARK: - Public Methods
|
|
174
|
+
|
|
175
|
+
func presentFilePicker(from viewController: UIViewController, options: FilePickerOptions = .default) {
|
|
176
|
+
currentOptions = options
|
|
177
|
+
cleanupTemporaryFiles()
|
|
178
|
+
logger.debug("Presenting file picker with options: \(options)")
|
|
179
|
+
|
|
180
|
+
delegate?.filePickerHandler(self, stateDidChange: "opening")
|
|
181
|
+
|
|
182
|
+
// Use photo picker if all MIME types are images/videos, else use document picker
|
|
183
|
+
let mimeTypes = options.mimeType.lowercased().split(separator: ",").map { $0.trimmingCharacters(in: .whitespaces) }
|
|
184
|
+
let allMedia = mimeTypes.allSatisfy { $0.hasPrefix("image/") || $0.hasPrefix("video/") }
|
|
185
|
+
|
|
186
|
+
if allMedia, #available(iOS 14.0, *) {
|
|
187
|
+
presentPhotoPicker(from: viewController, options: options)
|
|
188
|
+
} else {
|
|
189
|
+
presentDocumentPicker(from: viewController, options: options)
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
@available(iOS 14.0, *)
|
|
194
|
+
private func presentPhotoPicker(from viewController: UIViewController, options: FilePickerOptions) {
|
|
195
|
+
logger.debug("Using PHPickerViewController for image/video selection")
|
|
196
|
+
|
|
197
|
+
let mimeTypes = options.mimeType.lowercased().split(separator: ",").map { $0.trimmingCharacters(in: .whitespaces) }
|
|
198
|
+
let hasImages = mimeTypes.contains { $0.hasPrefix("image/") }
|
|
199
|
+
let hasVideos = mimeTypes.contains { $0.hasPrefix("video/") }
|
|
200
|
+
|
|
201
|
+
var config = PHPickerConfiguration()
|
|
202
|
+
config.selectionLimit = options.selectionLimit
|
|
203
|
+
config.filter = (hasImages && hasVideos) ? .any(of: [.images, .videos]) : hasVideos ? .videos : .images
|
|
204
|
+
|
|
205
|
+
photoPicker = PHPickerViewController(configuration: config)
|
|
206
|
+
photoPicker?.delegate = self
|
|
207
|
+
|
|
208
|
+
viewController.present(photoPicker!, animated: true) { [weak self] in
|
|
209
|
+
guard let strongSelf = self else {
|
|
210
|
+
logger.error("FilePickerHandler deallocated during photo picker presentation")
|
|
211
|
+
return
|
|
212
|
+
}
|
|
213
|
+
strongSelf.delegate?.filePickerHandler(strongSelf, stateDidChange: "opened")
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
private func presentDocumentPicker(from viewController: UIViewController, options: FilePickerOptions) {
|
|
218
|
+
logger.debug("Using UIDocumentPickerViewController for file selection")
|
|
219
|
+
|
|
220
|
+
// Convert MIME type to UTType
|
|
221
|
+
let allowedTypes = convertMimeTypeToUTTypes(options.mimeType)
|
|
222
|
+
logger.debug("Converted to UTTypes: \(allowedTypes.map { $0.identifier })")
|
|
223
|
+
|
|
224
|
+
// Check if we have valid UTTypes
|
|
225
|
+
if allowedTypes.isEmpty {
|
|
226
|
+
let error = NSError(domain: "FilePickerError", code: -5, userInfo: [NSLocalizedDescriptionKey: "Unsupported MIME type: \(options.mimeType)"])
|
|
227
|
+
delegate?.filePickerHandler(self, didFailWithError: error)
|
|
228
|
+
return
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: allowedTypes)
|
|
232
|
+
documentPicker?.delegate = self
|
|
233
|
+
documentPicker?.allowsMultipleSelection = options.multiple
|
|
234
|
+
documentPicker?.modalPresentationStyle = .formSheet
|
|
235
|
+
|
|
236
|
+
// Enable access to all locations including Photos
|
|
237
|
+
if #available(iOS 14.0, *) {
|
|
238
|
+
documentPicker?.shouldShowFileExtensions = true
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Log the actual UTTypes being used for debugging
|
|
242
|
+
logger.debug("Document picker created with \(allowedTypes.count) UTTypes for MIME: \(options.mimeType)")
|
|
243
|
+
for (index, utType) in allowedTypes.enumerated() {
|
|
244
|
+
logger.debug("UTType \(index): \(utType.identifier) - \(utType.description)")
|
|
245
|
+
if let preferredMimeType = utType.preferredMIMEType {
|
|
246
|
+
logger.debug(" Preferred MIME: \(preferredMimeType)")
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if let picker = documentPicker {
|
|
251
|
+
viewController.present(picker, animated: true) { [weak self] in
|
|
252
|
+
guard let strongSelf = self else {
|
|
253
|
+
logger.error("FilePickerHandler deallocated during document picker presentation")
|
|
254
|
+
return
|
|
255
|
+
}
|
|
256
|
+
strongSelf.delegate?.filePickerHandler(strongSelf, stateDidChange: "opened")
|
|
257
|
+
}
|
|
258
|
+
} else {
|
|
259
|
+
let error = NSError(domain: "FilePickerError", code: -1, userInfo: [NSLocalizedDescriptionKey: "Failed to create document picker"])
|
|
260
|
+
delegate?.filePickerHandler(self, didFailWithError: error)
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// MARK: - Private Methods
|
|
265
|
+
|
|
266
|
+
private func convertMimeTypeToUTTypes(_ mimeType: String) -> [UTType] {
|
|
267
|
+
logger.debug("Converting MIME type: \(mimeType)")
|
|
268
|
+
|
|
269
|
+
// Handle comma-separated MIME types (e.g., "application/pdf,image/*")
|
|
270
|
+
if mimeType.contains(",") {
|
|
271
|
+
logger.debug("Processing comma-separated MIME types: \(mimeType)")
|
|
272
|
+
let mimeTypes = mimeType.split(separator: ",").map { $0.trimmingCharacters(in: .whitespaces) }
|
|
273
|
+
var allUTTypes: [UTType] = []
|
|
274
|
+
|
|
275
|
+
for individualMimeType in mimeTypes {
|
|
276
|
+
let utTypes = convertMimeTypeToUTTypes(individualMimeType)
|
|
277
|
+
allUTTypes.append(contentsOf: utTypes)
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Remove duplicates using Set and return
|
|
281
|
+
let uniqueUTTypes = Array(Set(allUTTypes.map { $0.identifier })).compactMap { UTType($0) }
|
|
282
|
+
logger.debug("Combined UTTypes from multiple MIME types: \(uniqueUTTypes.map { $0.identifier })")
|
|
283
|
+
return uniqueUTTypes
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// Handle wildcards FIRST (before iOS native handling to ensure proper filtering)
|
|
287
|
+
if mimeType.contains("*") {
|
|
288
|
+
logger.debug("Processing wildcard MIME type: \(mimeType)")
|
|
289
|
+
return handleWildcardMimeType(mimeType)
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Use iOS native MIME type handling only for specific (non-wildcard) MIME types
|
|
293
|
+
if let nativeUTType = UTType(mimeType: mimeType) {
|
|
294
|
+
logger.debug("iOS natively handled specific MIME type: \(mimeType) -> \(nativeUTType.identifier)")
|
|
295
|
+
return [nativeUTType]
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// Fallback for unknown specific MIME types
|
|
299
|
+
logger.warning("Unknown specific MIME type: \(mimeType)")
|
|
300
|
+
return []
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
private func handleWildcardMimeType(_ mimeType: String) -> [UTType] {
|
|
304
|
+
switch mimeType.lowercased() {
|
|
305
|
+
case "*/*":
|
|
306
|
+
logger.debug("Allowing all file types including photos")
|
|
307
|
+
// Use UTType.item which includes all content types, including photos
|
|
308
|
+
// Also explicitly add image types to ensure photo library access
|
|
309
|
+
return [UTType.item, UTType.image, UTType.jpeg, UTType.png, UTType.heic]
|
|
310
|
+
|
|
311
|
+
case "image/*":
|
|
312
|
+
logger.debug("Restricting to common image formats only")
|
|
313
|
+
// Common image formats with broader compatibility
|
|
314
|
+
var imageTypes: [UTType] = [UTType.jpeg, UTType.png, UTType.gif, UTType.heic, UTType.image]
|
|
315
|
+
|
|
316
|
+
// Add webP if available (iOS 14+)
|
|
317
|
+
if #available(iOS 14.0, *), let webPType = UTType("public.webp") {
|
|
318
|
+
imageTypes.append(webPType)
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
return imageTypes
|
|
322
|
+
|
|
323
|
+
case "video/*":
|
|
324
|
+
logger.debug("Restricting to common video formats")
|
|
325
|
+
// Common video formats
|
|
326
|
+
return [UTType.mpeg4Movie, UTType.quickTimeMovie, UTType.avi, UTType.movie]
|
|
327
|
+
|
|
328
|
+
case "audio/*":
|
|
329
|
+
logger.debug("Restricting to common audio formats")
|
|
330
|
+
// Common audio formats
|
|
331
|
+
return [UTType.mp3, UTType.wav, UTType.aiff, UTType.audio]
|
|
332
|
+
|
|
333
|
+
case "text/*":
|
|
334
|
+
logger.debug("Restricting to common text formats")
|
|
335
|
+
// Common text formats
|
|
336
|
+
return [UTType.plainText, UTType.utf8PlainText, UTType.html]
|
|
337
|
+
|
|
338
|
+
case "application/*":
|
|
339
|
+
logger.debug("Restricting to common application formats")
|
|
340
|
+
// Common application formats
|
|
341
|
+
return [UTType.pdf, UTType.json, UTType.zip, UTType.executable]
|
|
342
|
+
|
|
343
|
+
default:
|
|
344
|
+
// Unknown wildcard pattern - be restrictive
|
|
345
|
+
logger.warning("Unknown wildcard MIME type pattern: \(mimeType)")
|
|
346
|
+
return []
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
private func extractFileMetadata(from url: URL) -> FileMetadata {
|
|
351
|
+
let fileName = url.lastPathComponent
|
|
352
|
+
let fileExtension = url.pathExtension
|
|
353
|
+
|
|
354
|
+
// Get file size
|
|
355
|
+
var fileSize: Int64 = 0
|
|
356
|
+
if let attributes = try? FileManager.default.attributesOfItem(atPath: url.path),
|
|
357
|
+
let size = attributes[.size] as? Int64 {
|
|
358
|
+
fileSize = size
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// Get MIME type
|
|
362
|
+
let mimeType = getMimeType(for: url)
|
|
363
|
+
|
|
364
|
+
// Get last modified date
|
|
365
|
+
var lastModified: Date?
|
|
366
|
+
if let attributes = try? FileManager.default.attributesOfItem(atPath: url.path),
|
|
367
|
+
let date = attributes[.modificationDate] as? Date {
|
|
368
|
+
lastModified = date
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
return FileMetadata(
|
|
372
|
+
fileName: fileName,
|
|
373
|
+
fileSize: fileSize,
|
|
374
|
+
mimeType: mimeType,
|
|
375
|
+
fileExtension: fileExtension,
|
|
376
|
+
lastModified: lastModified
|
|
377
|
+
)
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
private func getMimeType(for url: URL) -> String {
|
|
381
|
+
// First try UTType - only return if it has a preferredMIMEType
|
|
382
|
+
if let utType = UTType(filenameExtension: url.pathExtension),
|
|
383
|
+
let mimeType = utType.preferredMIMEType {
|
|
384
|
+
return mimeType
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// Fallback based on file extension with comprehensive mapping
|
|
388
|
+
let ext = url.pathExtension.lowercased()
|
|
389
|
+
|
|
390
|
+
switch ext {
|
|
391
|
+
// Images
|
|
392
|
+
case "jpg", "jpeg": return "image/jpeg"
|
|
393
|
+
case "png": return "image/png"
|
|
394
|
+
case "gif": return "image/gif"
|
|
395
|
+
case "webp": return "image/webp"
|
|
396
|
+
case "svg": return "image/svg+xml"
|
|
397
|
+
case "bmp": return "image/bmp"
|
|
398
|
+
case "heic": return "image/heic"
|
|
399
|
+
case "heif": return "image/heif"
|
|
400
|
+
case "ico": return "image/x-icon"
|
|
401
|
+
case "tiff", "tif": return "image/tiff"
|
|
402
|
+
|
|
403
|
+
// Documents
|
|
404
|
+
case "pdf": return "application/pdf"
|
|
405
|
+
case "doc": return "application/msword"
|
|
406
|
+
case "docx": return "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
|
407
|
+
case "xls": return "application/vnd.ms-excel"
|
|
408
|
+
case "xlsx": return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
|
409
|
+
case "ppt": return "application/vnd.ms-powerpoint"
|
|
410
|
+
case "pptx": return "application/vnd.openxmlformats-officedocument.presentationml.presentation"
|
|
411
|
+
|
|
412
|
+
// Text
|
|
413
|
+
case "txt": return "text/plain"
|
|
414
|
+
case "html", "htm": return "text/html"
|
|
415
|
+
case "css": return "text/css"
|
|
416
|
+
case "csv": return "text/csv"
|
|
417
|
+
|
|
418
|
+
// Data
|
|
419
|
+
case "json": return "application/json"
|
|
420
|
+
case "xml": return "application/xml"
|
|
421
|
+
case "js": return "application/javascript"
|
|
422
|
+
|
|
423
|
+
// Archives
|
|
424
|
+
case "zip": return "application/zip"
|
|
425
|
+
case "rar": return "application/x-rar-compressed"
|
|
426
|
+
case "7z": return "application/x-7z-compressed"
|
|
427
|
+
case "tar": return "application/x-tar"
|
|
428
|
+
case "gz": return "application/gzip"
|
|
429
|
+
|
|
430
|
+
// Video
|
|
431
|
+
case "mp4": return "video/mp4"
|
|
432
|
+
case "mov": return "video/quicktime"
|
|
433
|
+
case "avi": return "video/x-msvideo"
|
|
434
|
+
case "webm": return "video/webm"
|
|
435
|
+
case "mkv": return "video/x-matroska"
|
|
436
|
+
case "m4v": return "video/x-m4v"
|
|
437
|
+
case "flv": return "video/x-flv"
|
|
438
|
+
|
|
439
|
+
// Audio
|
|
440
|
+
case "mp3": return "audio/mpeg"
|
|
441
|
+
case "wav": return "audio/wav"
|
|
442
|
+
case "ogg": return "audio/ogg"
|
|
443
|
+
case "m4a": return "audio/mp4"
|
|
444
|
+
case "aac": return "audio/aac"
|
|
445
|
+
case "flac": return "audio/flac"
|
|
446
|
+
case "wma": return "audio/x-ms-wma"
|
|
447
|
+
|
|
448
|
+
default:
|
|
449
|
+
logger.warning("Unknown file extension '\(ext)', using application/octet-stream")
|
|
450
|
+
return "application/octet-stream"
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
private func determineTransportMethod(fileSize: Int64) -> FileTransportMethod {
|
|
455
|
+
if fileSize <= BASE64_SIZE_LIMIT {
|
|
456
|
+
return .base64
|
|
457
|
+
} else {
|
|
458
|
+
return .frameworkServer
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
func processFile(at url: URL, metadata: FileMetadata) -> FileProcessingResult {
|
|
463
|
+
logger.debug("Processing file: \(metadata.fileName) (\(self.formatFileSize(metadata.fileSize)))")
|
|
464
|
+
|
|
465
|
+
// Check file size limits
|
|
466
|
+
if metadata.fileSize > FRAMEWORK_SERVER_SIZE_LIMIT {
|
|
467
|
+
return FileProcessingResult(
|
|
468
|
+
success: false,
|
|
469
|
+
fileSrc: nil,
|
|
470
|
+
fileName: metadata.fileName,
|
|
471
|
+
fileSize: metadata.fileSize,
|
|
472
|
+
mimeType: metadata.mimeType,
|
|
473
|
+
transport: .base64,
|
|
474
|
+
error: "File too large. Maximum size: \(self.formatFileSize(FRAMEWORK_SERVER_SIZE_LIMIT))"
|
|
475
|
+
)
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
let transport = determineTransportMethod(fileSize: metadata.fileSize)
|
|
479
|
+
logger.debug("Using transport method: \(transport.name)")
|
|
480
|
+
|
|
481
|
+
switch transport {
|
|
482
|
+
case .base64:
|
|
483
|
+
return processFileAsBase64(url: url, metadata: metadata)
|
|
484
|
+
case .frameworkServer:
|
|
485
|
+
return processFileWithFrameworkServer(url: url, metadata: metadata)
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
private func processFileAsBase64(url: URL, metadata: FileMetadata) -> FileProcessingResult {
|
|
490
|
+
do {
|
|
491
|
+
let data = try Data(contentsOf: url)
|
|
492
|
+
let base64String = data.base64EncodedString()
|
|
493
|
+
let dataURL = "data:\(metadata.mimeType);base64,\(base64String)"
|
|
494
|
+
|
|
495
|
+
logger.debug("File converted to base64 successfully")
|
|
496
|
+
|
|
497
|
+
return FileProcessingResult(
|
|
498
|
+
success: true,
|
|
499
|
+
fileSrc: dataURL,
|
|
500
|
+
fileName: metadata.fileName,
|
|
501
|
+
fileSize: metadata.fileSize,
|
|
502
|
+
mimeType: metadata.mimeType,
|
|
503
|
+
transport: .base64,
|
|
504
|
+
error: nil
|
|
505
|
+
)
|
|
506
|
+
} catch {
|
|
507
|
+
logger.error("Failed to convert file to base64: \(error.localizedDescription)")
|
|
508
|
+
return FileProcessingResult(
|
|
509
|
+
success: false,
|
|
510
|
+
fileSrc: nil,
|
|
511
|
+
fileName: metadata.fileName,
|
|
512
|
+
fileSize: metadata.fileSize,
|
|
513
|
+
mimeType: metadata.mimeType,
|
|
514
|
+
transport: .base64,
|
|
515
|
+
error: "Failed to read file: \(error.localizedDescription)"
|
|
516
|
+
)
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
private func processFileWithFrameworkServer(url: URL, metadata: FileMetadata) -> FileProcessingResult {
|
|
522
|
+
logger.info("Processing file with framework server transport")
|
|
523
|
+
|
|
524
|
+
let frameworkServer = FrameworkServerUtils.shared
|
|
525
|
+
|
|
526
|
+
// Ensure framework server is running
|
|
527
|
+
if !frameworkServer.isRunning() {
|
|
528
|
+
logger.info("Framework server not running, starting server...")
|
|
529
|
+
if !frameworkServer.startServer() {
|
|
530
|
+
logger.error("Failed to start framework server, falling back to local file URL")
|
|
531
|
+
return fallbackToFileUrlProcessing(url: url, metadata: metadata)
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
// Copy file to server cache and get serving URL
|
|
536
|
+
guard let serverUrl = frameworkServer.copyAndServeFile(
|
|
537
|
+
originalFile: url,
|
|
538
|
+
fileName: metadata.fileName,
|
|
539
|
+
mimeType: metadata.mimeType
|
|
540
|
+
) else {
|
|
541
|
+
logger.error("Failed to add file to framework server, falling back to local file URL")
|
|
542
|
+
return fallbackToFileUrlProcessing(url: url, metadata: metadata)
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
logger.debug("Successfully processed file with framework server: \(serverUrl)")
|
|
546
|
+
|
|
547
|
+
return FileProcessingResult(
|
|
548
|
+
success: true,
|
|
549
|
+
fileSrc: serverUrl,
|
|
550
|
+
fileName: metadata.fileName,
|
|
551
|
+
fileSize: metadata.fileSize,
|
|
552
|
+
mimeType: metadata.mimeType,
|
|
553
|
+
transport: .frameworkServer,
|
|
554
|
+
error: nil
|
|
555
|
+
)
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
private func fallbackToFileUrlProcessing(url: URL, metadata: FileMetadata) -> FileProcessingResult {
|
|
559
|
+
do {
|
|
560
|
+
// Copy file to app's documents directory to ensure access (iOS equivalent of content provider)
|
|
561
|
+
let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
|
|
562
|
+
let fileName = "\(UUID().uuidString)_\(metadata.fileName)"
|
|
563
|
+
let destinationURL = documentsPath.appendingPathComponent(fileName)
|
|
564
|
+
|
|
565
|
+
// Remove existing file if it exists
|
|
566
|
+
if FileManager.default.fileExists(atPath: destinationURL.path) {
|
|
567
|
+
try FileManager.default.removeItem(at: destinationURL)
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
try FileManager.default.copyItem(at: url, to: destinationURL)
|
|
571
|
+
|
|
572
|
+
logger.debug("File copied to accessible location for fallback: \(destinationURL.path)")
|
|
573
|
+
|
|
574
|
+
// Use secure relative path instead of absolute file:// URL
|
|
575
|
+
let secureFilePath = "catalyst-files/\(fileName)"
|
|
576
|
+
|
|
577
|
+
return FileProcessingResult(
|
|
578
|
+
success: true,
|
|
579
|
+
fileSrc: secureFilePath,
|
|
580
|
+
fileName: metadata.fileName,
|
|
581
|
+
fileSize: metadata.fileSize,
|
|
582
|
+
mimeType: metadata.mimeType,
|
|
583
|
+
transport: .frameworkServer, // Still report as framework server since it's the intended transport
|
|
584
|
+
error: nil
|
|
585
|
+
)
|
|
586
|
+
} catch {
|
|
587
|
+
logger.error("Fallback file URL processing failed: \(error.localizedDescription)")
|
|
588
|
+
return FileProcessingResult(
|
|
589
|
+
success: false,
|
|
590
|
+
fileSrc: nil,
|
|
591
|
+
fileName: metadata.fileName,
|
|
592
|
+
fileSize: metadata.fileSize,
|
|
593
|
+
mimeType: metadata.mimeType,
|
|
594
|
+
transport: .frameworkServer,
|
|
595
|
+
error: "Framework server and fallback both failed: \(error.localizedDescription)"
|
|
596
|
+
)
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
private func formatFileSize(_ bytes: Int64) -> String {
|
|
601
|
+
let formatter = ByteCountFormatter()
|
|
602
|
+
formatter.countStyle = .file
|
|
603
|
+
return formatter.string(fromByteCount: bytes)
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
private func cleanupTemporaryFiles() {
|
|
607
|
+
guard !temporaryFiles.isEmpty else { return }
|
|
608
|
+
let fileManager = FileManager.default
|
|
609
|
+
for url in temporaryFiles {
|
|
610
|
+
do {
|
|
611
|
+
try fileManager.removeItem(at: url)
|
|
612
|
+
logger.debug("Removed temporary file: \(url.lastPathComponent)")
|
|
613
|
+
} catch {
|
|
614
|
+
logger.warning("Failed to remove temporary file \(url.lastPathComponent): \(error.localizedDescription)")
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
temporaryFiles.removeAll()
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
private func sanitizeSelectionForSingleSelect<T>(_ items: [T]) -> [T] {
|
|
621
|
+
guard !currentOptions.multiple, items.count > 1 else {
|
|
622
|
+
return items
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
logger.warning("Multiple selections received but picker configured for single file. Processing only the first item.")
|
|
626
|
+
return Array(items.prefix(1))
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
private func selectionCountValidationMessage(_ count: Int) -> String? {
|
|
630
|
+
if let minFiles = currentOptions.minFiles, count < minFiles {
|
|
631
|
+
return "Select at least \(minFiles) file(s). You selected \(count)."
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
if let maxFiles = currentOptions.maxFiles, maxFiles > 0, count > maxFiles {
|
|
635
|
+
return "You can select up to \(maxFiles) file(s). You selected \(count)."
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
return nil
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
private func sizeValidationMessage(for index: Int, size: Int64) -> String? {
|
|
642
|
+
if let minSize = currentOptions.minFileSize, size < minSize {
|
|
643
|
+
let actual = formatFileSize(size)
|
|
644
|
+
let minimum = formatFileSize(minSize)
|
|
645
|
+
return "File \(index + 1) is too small (\(actual)). Minimum size is \(minimum)."
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
if let maxSize = currentOptions.maxFileSize, size > maxSize {
|
|
649
|
+
let actual = formatFileSize(size)
|
|
650
|
+
let maximum = formatFileSize(maxSize)
|
|
651
|
+
return "File \(index + 1) exceeds maximum size (\(actual) > \(maximum))."
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
return nil
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
private func makeError(code: Int, message: String) -> NSError {
|
|
658
|
+
return NSError(domain: "FilePickerError", code: code, userInfo: [NSLocalizedDescriptionKey: message])
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
private func finalizeSelection(_ files: [PickedFile]) {
|
|
662
|
+
defer { cleanupTemporaryFiles() }
|
|
663
|
+
|
|
664
|
+
guard !files.isEmpty else {
|
|
665
|
+
delegate?.filePickerHandler(self, didFailWithError: makeError(code: -2, message: "No file selected"))
|
|
666
|
+
return
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
let orderedFiles = files.sorted { $0.index < $1.index }
|
|
670
|
+
|
|
671
|
+
if let message = selectionCountValidationMessage(orderedFiles.count) {
|
|
672
|
+
delegate?.filePickerHandler(self, didFailWithError: makeError(code: -10, message: message))
|
|
673
|
+
return
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
delegate?.filePickerHandler(self, stateDidChange: "processing")
|
|
677
|
+
|
|
678
|
+
for file in orderedFiles {
|
|
679
|
+
if let message = sizeValidationMessage(for: file.index, size: file.metadata.fileSize) {
|
|
680
|
+
delegate?.filePickerHandler(self, didFailWithError: makeError(code: -11, message: message))
|
|
681
|
+
return
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
delegate?.filePickerHandler(self, stateDidChange: "routing")
|
|
686
|
+
|
|
687
|
+
var payloadFiles: [[String: Any]] = []
|
|
688
|
+
var totalSize: Int64 = 0
|
|
689
|
+
|
|
690
|
+
for file in orderedFiles {
|
|
691
|
+
let result = processFile(at: file.url, metadata: file.metadata)
|
|
692
|
+
guard result.success, let fileSrc = result.fileSrc else {
|
|
693
|
+
let message = result.error ?? "Failed to process selected file."
|
|
694
|
+
delegate?.filePickerHandler(self, didFailWithError: makeError(code: -12, message: message))
|
|
695
|
+
return
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
let payload: [String: Any] = [
|
|
699
|
+
"index": file.index,
|
|
700
|
+
"fileName": result.fileName,
|
|
701
|
+
"fileSrc": fileSrc,
|
|
702
|
+
"size": result.fileSize,
|
|
703
|
+
"mimeType": result.mimeType,
|
|
704
|
+
"transport": result.transport.name,
|
|
705
|
+
"source": "file_picker",
|
|
706
|
+
"platform": "ios"
|
|
707
|
+
]
|
|
708
|
+
|
|
709
|
+
payloadFiles.append(payload)
|
|
710
|
+
totalSize += result.fileSize
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
var response: [String: Any] = [
|
|
714
|
+
"multiple": payloadFiles.count > 1,
|
|
715
|
+
"count": payloadFiles.count,
|
|
716
|
+
"totalSize": totalSize,
|
|
717
|
+
"files": payloadFiles,
|
|
718
|
+
"options": currentOptions.toDictionary(),
|
|
719
|
+
"source": "file_picker",
|
|
720
|
+
"platform": "ios"
|
|
721
|
+
]
|
|
722
|
+
|
|
723
|
+
if let first = payloadFiles.first {
|
|
724
|
+
response["fileName"] = first["fileName"]
|
|
725
|
+
response["fileSrc"] = first["fileSrc"]
|
|
726
|
+
response["size"] = first["size"]
|
|
727
|
+
response["mimeType"] = first["mimeType"]
|
|
728
|
+
response["transport"] = first["transport"]
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
delegate?.filePickerHandler(self, stateDidChange: "complete")
|
|
732
|
+
delegate?.filePickerHandler(self, didFinishWith: response)
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
// MARK: - PHPickerViewControllerDelegate
|
|
737
|
+
|
|
738
|
+
@available(iOS 14.0, *)
|
|
739
|
+
extension FilePickerHandler: PHPickerViewControllerDelegate {
|
|
740
|
+
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
|
|
741
|
+
picker.dismiss(animated: true)
|
|
742
|
+
|
|
743
|
+
guard !results.isEmpty else {
|
|
744
|
+
cleanupTemporaryFiles()
|
|
745
|
+
logger.debug("Photo picker was cancelled or no selection made")
|
|
746
|
+
delegate?.filePickerHandlerDidCancel(self)
|
|
747
|
+
return
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
let sanitizedResults = sanitizeSelectionForSingleSelect(results)
|
|
751
|
+
|
|
752
|
+
if let message = selectionCountValidationMessage(sanitizedResults.count) {
|
|
753
|
+
cleanupTemporaryFiles()
|
|
754
|
+
delegate?.filePickerHandler(self, didFailWithError: makeError(code: -10, message: message))
|
|
755
|
+
return
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
processPhotoResults(sanitizedResults)
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
private func processPhotoResults(_ results: [PHPickerResult]) {
|
|
762
|
+
var collected: [PickedFile] = []
|
|
763
|
+
|
|
764
|
+
func handleResult(at index: Int) {
|
|
765
|
+
if index >= results.count {
|
|
766
|
+
finalizeSelection(collected)
|
|
767
|
+
return
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
processPhotoResult(result: results[index], index: index) { outcome in
|
|
771
|
+
switch outcome {
|
|
772
|
+
case .success(let file):
|
|
773
|
+
collected.append(file)
|
|
774
|
+
handleResult(at: index + 1)
|
|
775
|
+
case .failure(let error):
|
|
776
|
+
self.cleanupTemporaryFiles()
|
|
777
|
+
self.delegate?.filePickerHandler(self, didFailWithError: error)
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
handleResult(at: 0)
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
private func processPhotoResult(
|
|
786
|
+
result: PHPickerResult,
|
|
787
|
+
index: Int,
|
|
788
|
+
completion: @escaping (Result<PickedFile, NSError>) -> Void
|
|
789
|
+
) {
|
|
790
|
+
let itemProvider = result.itemProvider
|
|
791
|
+
|
|
792
|
+
guard let typeIdentifier = preferredTypeIdentifier(for: itemProvider) else {
|
|
793
|
+
logger.error("Selected item has no supported type identifiers")
|
|
794
|
+
completion(.failure(makeError(code: -8, message: "Selected item type not supported")))
|
|
795
|
+
return
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
logger.debug("Processing selected file with type: \(typeIdentifier)")
|
|
799
|
+
|
|
800
|
+
loadItemData(
|
|
801
|
+
itemProvider: itemProvider,
|
|
802
|
+
typeIdentifier: typeIdentifier,
|
|
803
|
+
result: result,
|
|
804
|
+
index: index,
|
|
805
|
+
completion: completion
|
|
806
|
+
)
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
private func preferredTypeIdentifier(for itemProvider: NSItemProvider) -> String? {
|
|
810
|
+
// Try to get the most specific type identifier first
|
|
811
|
+
// Prefer specific types over generic ones
|
|
812
|
+
let specificImageTypes = [
|
|
813
|
+
UTType.heic.identifier,
|
|
814
|
+
UTType.heif.identifier,
|
|
815
|
+
UTType.jpeg.identifier,
|
|
816
|
+
UTType.png.identifier,
|
|
817
|
+
UTType.gif.identifier,
|
|
818
|
+
"public.heic",
|
|
819
|
+
"public.heif",
|
|
820
|
+
"public.jpeg",
|
|
821
|
+
"public.png",
|
|
822
|
+
"public.gif"
|
|
823
|
+
]
|
|
824
|
+
|
|
825
|
+
// Check if any specific image type is available
|
|
826
|
+
for specificType in specificImageTypes {
|
|
827
|
+
if itemProvider.hasItemConformingToTypeIdentifier(specificType) {
|
|
828
|
+
return specificType
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
// Check for specific video types
|
|
833
|
+
let specificVideoTypes = [
|
|
834
|
+
UTType.mpeg4Movie.identifier,
|
|
835
|
+
UTType.quickTimeMovie.identifier,
|
|
836
|
+
"public.mpeg-4",
|
|
837
|
+
"com.apple.quicktime-movie"
|
|
838
|
+
]
|
|
839
|
+
|
|
840
|
+
for specificType in specificVideoTypes {
|
|
841
|
+
if itemProvider.hasItemConformingToTypeIdentifier(specificType) {
|
|
842
|
+
return specificType
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
// Fall back to generic types
|
|
847
|
+
if itemProvider.hasItemConformingToTypeIdentifier(UTType.image.identifier) {
|
|
848
|
+
return UTType.image.identifier
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
if itemProvider.hasItemConformingToTypeIdentifier(UTType.movie.identifier) {
|
|
852
|
+
return UTType.movie.identifier
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
if itemProvider.hasItemConformingToTypeIdentifier(UTType.data.identifier) {
|
|
856
|
+
return UTType.data.identifier
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
return itemProvider.registeredTypeIdentifiers.first
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
private func loadItemData(
|
|
863
|
+
itemProvider: NSItemProvider,
|
|
864
|
+
typeIdentifier: String,
|
|
865
|
+
result: PHPickerResult,
|
|
866
|
+
index: Int,
|
|
867
|
+
completion: @escaping (Result<PickedFile, NSError>) -> Void
|
|
868
|
+
) {
|
|
869
|
+
itemProvider.loadDataRepresentation(forTypeIdentifier: typeIdentifier) { [weak self] data, error in
|
|
870
|
+
DispatchQueue.main.async {
|
|
871
|
+
guard let self = self else { return }
|
|
872
|
+
|
|
873
|
+
if let error = error {
|
|
874
|
+
logger.error("Failed to load item data: \(error.localizedDescription)")
|
|
875
|
+
completion(.failure(self.makeError(code: -6, message: "Failed to load file: \(error.localizedDescription)")))
|
|
876
|
+
return
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
guard let data = data else {
|
|
880
|
+
completion(.failure(self.makeError(code: -7, message: "No file data received")))
|
|
881
|
+
return
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
do {
|
|
885
|
+
let pickedFile = try self.savePhotoPickerResult(
|
|
886
|
+
data: data,
|
|
887
|
+
result: result,
|
|
888
|
+
typeIdentifier: typeIdentifier,
|
|
889
|
+
index: index
|
|
890
|
+
)
|
|
891
|
+
completion(.success(pickedFile))
|
|
892
|
+
} catch let nsError as NSError {
|
|
893
|
+
completion(.failure(nsError))
|
|
894
|
+
} catch {
|
|
895
|
+
completion(.failure(self.makeError(code: -9, message: "Failed to save file: \(error.localizedDescription)")))
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
private func savePhotoPickerResult(
|
|
902
|
+
data: Data,
|
|
903
|
+
result: PHPickerResult,
|
|
904
|
+
typeIdentifier: String,
|
|
905
|
+
index: Int
|
|
906
|
+
) throws -> PickedFile {
|
|
907
|
+
let fileExtension = getFileExtension(from: result, typeIdentifier: typeIdentifier)
|
|
908
|
+
let fileName = "\(UUID().uuidString).\(fileExtension)"
|
|
909
|
+
let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
|
|
910
|
+
let fileURL = documentsPath.appendingPathComponent(fileName)
|
|
911
|
+
|
|
912
|
+
do {
|
|
913
|
+
try data.write(to: fileURL)
|
|
914
|
+
} catch {
|
|
915
|
+
logger.error("Failed to save photo picker result: \(error.localizedDescription)")
|
|
916
|
+
throw makeError(code: -9, message: "Failed to save file: \(error.localizedDescription)")
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
temporaryFiles.append(fileURL)
|
|
920
|
+
|
|
921
|
+
// Get MIME type from typeIdentifier first, as file extension might be generic (.dat)
|
|
922
|
+
let mimeTypeFromIdentifier = getMimeTypeFromTypeIdentifier(typeIdentifier)
|
|
923
|
+
let finalMimeType = mimeTypeFromIdentifier ?? getMimeType(for: fileURL)
|
|
924
|
+
|
|
925
|
+
let metadata = FileMetadata(
|
|
926
|
+
fileName: fileName,
|
|
927
|
+
fileSize: Int64(data.count),
|
|
928
|
+
mimeType: finalMimeType,
|
|
929
|
+
fileExtension: fileExtension,
|
|
930
|
+
lastModified: Date()
|
|
931
|
+
)
|
|
932
|
+
|
|
933
|
+
logger.debug("Photo picker result saved: \(fileName), type: \(typeIdentifier), size: \(self.formatFileSize(metadata.fileSize))")
|
|
934
|
+
|
|
935
|
+
return PickedFile(index: index, url: fileURL, metadata: metadata)
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
private func getFileExtension(from result: PHPickerResult, typeIdentifier: String) -> String {
|
|
939
|
+
// Try to get the file extension from the suggested name first
|
|
940
|
+
if let suggestedName = result.itemProvider.suggestedName,
|
|
941
|
+
!URL(fileURLWithPath: suggestedName).pathExtension.isEmpty {
|
|
942
|
+
let ext = URL(fileURLWithPath: suggestedName).pathExtension
|
|
943
|
+
logger.debug("Using file extension '\(ext)' from suggested name: \(suggestedName)")
|
|
944
|
+
return ext
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
// Fallback based on type identifier
|
|
948
|
+
if let utType = UTType(typeIdentifier) {
|
|
949
|
+
if let preferredExtension = utType.preferredFilenameExtension {
|
|
950
|
+
logger.debug("Using file extension '\(preferredExtension)' from UTType for identifier: \(typeIdentifier)")
|
|
951
|
+
return preferredExtension
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
// Final fallback based on common type identifiers
|
|
956
|
+
let fileExtension: String
|
|
957
|
+
switch typeIdentifier {
|
|
958
|
+
case UTType.jpeg.identifier, "public.jpeg":
|
|
959
|
+
fileExtension = "jpg"
|
|
960
|
+
case UTType.png.identifier, "public.png":
|
|
961
|
+
fileExtension = "png"
|
|
962
|
+
case UTType.heic.identifier, "public.heic":
|
|
963
|
+
fileExtension = "heic"
|
|
964
|
+
case UTType.gif.identifier, "public.gif":
|
|
965
|
+
fileExtension = "gif"
|
|
966
|
+
case UTType.mpeg4Movie.identifier, "public.mpeg-4":
|
|
967
|
+
fileExtension = "mp4"
|
|
968
|
+
case UTType.quickTimeMovie.identifier, "com.apple.quicktime-movie":
|
|
969
|
+
fileExtension = "mov"
|
|
970
|
+
default:
|
|
971
|
+
fileExtension = "dat"
|
|
972
|
+
logger.warning("Unknown type identifier '\(typeIdentifier)', defaulting to 'dat' extension")
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
logger.debug("Using file extension '\(fileExtension)' from type identifier fallback")
|
|
976
|
+
return fileExtension
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
private func getMimeTypeFromTypeIdentifier(_ typeIdentifier: String) -> String? {
|
|
980
|
+
guard let utType = UTType(typeIdentifier) else {
|
|
981
|
+
return nil
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
// If UTType has a preferred MIME type, return it
|
|
985
|
+
if let mimeType = utType.preferredMIMEType {
|
|
986
|
+
return mimeType
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
// Handle generic types by checking if they conform to specific types
|
|
990
|
+
if utType.conforms(to: .jpeg) {
|
|
991
|
+
return "image/jpeg"
|
|
992
|
+
} else if utType.conforms(to: .png) {
|
|
993
|
+
return "image/png"
|
|
994
|
+
} else if utType.conforms(to: .heic) {
|
|
995
|
+
return "image/heic"
|
|
996
|
+
} else if utType.conforms(to: .gif) {
|
|
997
|
+
return "image/gif"
|
|
998
|
+
} else if utType.conforms(to: .image) {
|
|
999
|
+
return "image/jpeg" // Default for generic images
|
|
1000
|
+
} else if utType.conforms(to: .movie) {
|
|
1001
|
+
return "video/mp4" // Default for generic videos
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
return nil
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
// MARK: - UIDocumentPickerDelegate
|
|
1009
|
+
|
|
1010
|
+
extension FilePickerHandler: UIDocumentPickerDelegate {
|
|
1011
|
+
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
|
|
1012
|
+
logger.debug("Document picker did pick documents: \(urls.count)")
|
|
1013
|
+
|
|
1014
|
+
if let message = selectionCountValidationMessage(urls.count) {
|
|
1015
|
+
delegate?.filePickerHandler(self, didFailWithError: makeError(code: -10, message: message))
|
|
1016
|
+
return
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
let sanitizedURLs = sanitizeSelectionForSingleSelect(urls)
|
|
1020
|
+
|
|
1021
|
+
guard !sanitizedURLs.isEmpty else {
|
|
1022
|
+
delegate?.filePickerHandler(self, didFailWithError: makeError(code: -2, message: "No file selected"))
|
|
1023
|
+
return
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
var scopedURLs: [URL] = []
|
|
1027
|
+
var pickedFiles: [PickedFile] = []
|
|
1028
|
+
|
|
1029
|
+
for (index, url) in sanitizedURLs.enumerated() {
|
|
1030
|
+
if url.startAccessingSecurityScopedResource() {
|
|
1031
|
+
scopedURLs.append(url)
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
let metadata = extractFileMetadata(from: url)
|
|
1035
|
+
logger.debug("File metadata: \(metadata.fileName), \(self.formatFileSize(metadata.fileSize)), \(metadata.mimeType)")
|
|
1036
|
+
|
|
1037
|
+
pickedFiles.append(PickedFile(index: index, url: url, metadata: metadata))
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
defer {
|
|
1041
|
+
for url in scopedURLs {
|
|
1042
|
+
url.stopAccessingSecurityScopedResource()
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
finalizeSelection(pickedFiles)
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
|
|
1050
|
+
logger.debug("Document picker was cancelled")
|
|
1051
|
+
delegate?.filePickerHandlerDidCancel(self)
|
|
1052
|
+
}
|
|
1053
|
+
}
|