-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocumentPickerUtil
More file actions
42 lines (35 loc) · 1.56 KB
/
DocumentPickerUtil
File metadata and controls
42 lines (35 loc) · 1.56 KB
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
import Foundation
class DocumentPickerUtil {
static let shared = DocumentPickerUtil()
func isFileSizeExceeds(_ fileUrl: URL?, maxSizeMB: Float) -> Bool {
guard let fileUrl = fileUrl else { return true }
let fileSize = getFileSizeFromURL(fileUrl)
return allowToSendFile(fileSize, maxSizeMB)
}
private func getFileSizeFromURL(_ fileUrl: URL) -> String {
var fileSizeValue: UInt64 = 0
do {
let fileAttribute: [FileAttributeKey : Any] = try FileManager.default.attributesOfItem(atPath: fileUrl.path)
if let fileNumberSize: NSNumber = fileAttribute[FileAttributeKey.size] as? NSNumber {
fileSizeValue = UInt64(truncating: fileNumberSize)
let byteCountFormatter: ByteCountFormatter = ByteCountFormatter()
byteCountFormatter.countStyle = ByteCountFormatter.CountStyle.file
byteCountFormatter.allowedUnits = ByteCountFormatter.Units.useMB
let fileSize = byteCountFormatter.string(fromByteCount: Int64(fileSizeValue))
Logger.log(fileSize)
return fileSize
}
} catch {
print(error.localizedDescription)
}
return ""
}
private func allowToSendFile(_ fileSize: String, _ maxSize: Float) -> Bool {
let strSize = fileSize.replacingOccurrences(of: " MB", with: "")
if let intSize = Float(strSize), intSize > maxSize {
return false
} else {
return true
}
}
}