-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathSwiftFlutterDocumentPickerDelegate.swift
92 lines (73 loc) · 3.2 KB
/
SwiftFlutterDocumentPickerDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// SwiftFlutterDocumentPickerDelegate.swift
// flutter_document_picker
//
// Created by ts on 01/09/2018.
//
import Foundation
import UIKit
public class SwiftFlutterDocumentPickerDelegate: NSObject {
fileprivate var flutterResult: FlutterResult?
fileprivate var params: FlutterDocumentPickerParams?
func pickDocument(_ params: FlutterDocumentPickerParams?, result: @escaping FlutterResult) {
flutterResult = result
self.params = params
guard let viewController = UIApplication.shared.keyWindow?.rootViewController else {
result(FlutterError.init(code: "error",
message: "Unable to get view controller!",
details: nil)
)
return
}
var documentTypes = ["public.data"]
if let allowedUtiTypes = params?.allowedUtiTypes {
if !allowedUtiTypes.isEmpty {
documentTypes = allowedUtiTypes
}
}
let documentPickerViewController = UIDocumentPickerViewController(documentTypes: documentTypes, in: .import)
documentPickerViewController.delegate = self
viewController.present(documentPickerViewController, animated: true, completion: nil)
}
}
extension SwiftFlutterDocumentPickerDelegate: UIDocumentPickerDelegate {
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
let fileExtension = url.pathExtension
if let allowedFileExtensions = params?.allowedFileExtensions {
if !allowedFileExtensions.contains(where: { $0 == fileExtension }) {
flutterResult?(FlutterError.init(code: "extension_mismatch",
message: "Picked file extension mismatch!",
details: fileExtension))
}
}
let fileName = sanitizeFileName(url.lastPathComponent)
// Create file URL to temporary folder
var tempUrl = URL(fileURLWithPath: NSTemporaryDirectory())
// Apend filename (name+extension) to URL
tempUrl.appendPathComponent(fileName)
do {
// If file with same name exists remove it (replace file with new one)
if FileManager.default.fileExists(atPath: tempUrl.path) {
try FileManager.default.removeItem(atPath: tempUrl.path)
}
// Move file from app_id-Inbox to tmp/filename
try FileManager.default.moveItem(atPath: url.path, toPath: tempUrl.path)
flutterResult?(tempUrl.path)
} catch {
print(error.localizedDescription)
flutterResult?(nil)
}
}
public func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
flutterResult?(nil)
}
private func sanitizeFileName(_ fileName: String) -> String {
var sanitizedFileName = fileName
if let invalidSymbols = params?.invalidFileNameSymbols {
invalidSymbols.forEach{ symbol in
sanitizedFileName = sanitizedFileName.replacingOccurrences(of: symbol, with: "_")
}
}
return sanitizedFileName
}
}