FBFile.swift
3.58 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// FBFile.swift
// FileBrowser
//
// Created by Roy Marmelstein on 14/02/2016.
// Copyright © 2016 Roy Marmelstein. All rights reserved.
//
import Foundation
import UIKit
/// FBFile is a class representing a file in FileBrowser
@objc open class FBFile: NSObject {
/// Display name. String.
@objc public let displayName: String
// is Directory. Bool.
public let isDirectory: Bool
/// File extension.
public let fileExtension: String?
/// File attributes (including size, creation date etc).
public let fileAttributes: NSDictionary?
/// NSURL file path.
public let filePath: URL
// FBFileType
public let type: FBFileType
open func delete()
{
do
{
try FileManager.default.removeItem(at: self.filePath)
}
catch
{
print("An error occured when trying to delete file:\(self.filePath) Error:\(error)")
}
}
/**
Initialize an FBFile object with a filePath
- parameter filePath: NSURL filePath
- returns: FBFile object.
*/
init(filePath: URL) {
self.filePath = filePath
let isDirectory = checkDirectory(filePath)
self.isDirectory = isDirectory
if self.isDirectory {
self.fileAttributes = nil
self.fileExtension = nil
self.type = .Directory
}
else {
self.fileAttributes = getFileAttributes(self.filePath)
self.fileExtension = filePath.pathExtension
if let fileExtension = fileExtension {
self.type = FBFileType(rawValue: fileExtension) ?? .Default
}
else {
self.type = .Default
}
}
self.displayName = filePath.lastPathComponent
}
}
/**
FBFile type
*/
public enum FBFileType: String {
/// Directory
case Directory = "directory"
/// GIF file
case GIF = "gif"
/// JPG file
case JPG = "jpg"
/// PLIST file
case JSON = "json"
/// PDF file
case PDF = "pdf"
/// PLIST file
case PLIST = "plist"
/// PNG file
case PNG = "png"
/// ZIP file
case ZIP = "zip"
/// Any file
case Default = "file"
/**
Get representative image for file type
- returns: UIImage for file type
*/
public func image() -> UIImage? {
let bundle = Bundle(for: FileParser.self)
var fileName = String()
switch self {
case .Directory: fileName = "folder@2x.png"
case .JPG, .PNG, .GIF: fileName = "image@2x.png"
case .PDF: fileName = "pdf@2x.png"
case .ZIP: fileName = "zip@2x.png"
default: fileName = "file@2x.png"
}
let file = UIImage(named: fileName, in: bundle, compatibleWith: nil)
return file
}
}
/**
Check if file path NSURL is directory or file.
- parameter filePath: NSURL file path.
- returns: isDirectory Bool.
*/
func checkDirectory(_ filePath: URL) -> Bool {
var isDirectory = false
do {
var resourceValue: AnyObject?
try (filePath as NSURL).getResourceValue(&resourceValue, forKey: URLResourceKey.isDirectoryKey)
if let number = resourceValue as? NSNumber , number == true {
isDirectory = true
}
}
catch { }
return isDirectory
}
func getFileAttributes(_ filePath: URL) -> NSDictionary? {
let path = filePath.path
let fileManager = FileParser.sharedInstance.fileManager
do {
let attributes = try fileManager.attributesOfItem(atPath: path) as NSDictionary
return attributes
} catch {}
return nil
}