-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathFileParser.swift
64 lines (54 loc) · 1.81 KB
/
FileParser.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
//
// FileParser.swift
// FileBrowser
//
// Created by Roy Marmelstein on 13/02/2016.
// Copyright © 2016 Roy Marmelstein. All rights reserved.
//
import Foundation
class FileParser {
static let sharedInstance = FileParser()
var _excludesFileExtensions = [String]()
/// Mapped for case insensitivity
var excludesFileExtensions: [String]? {
get {
return _excludesFileExtensions.map({$0.lowercased()})
}
set {
if let newValue = newValue {
_excludesFileExtensions = newValue
}
}
}
var excludesFilepaths: [URL]?
let fileManager = FileManager.default
func documentsURL() -> URL {
return fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0] as URL
}
func filesForDirectory(_ directoryPath: URL) -> [FBFile] {
var files = [FBFile]()
var filePaths = [URL]()
// Get contents
do {
filePaths = try self.fileManager.contentsOfDirectory(at: directoryPath, includingPropertiesForKeys: [], options: [.skipsHiddenFiles])
} catch {
return files
}
// Parse
for filePath in filePaths {
let file = FBFile(filePath: filePath)
if let excludesFileExtensions = excludesFileExtensions, let fileExtensions = file.fileExtension , excludesFileExtensions.contains(fileExtensions) {
continue
}
if let excludesFilepaths = excludesFilepaths , excludesFilepaths.contains(file.filePath) {
continue
}
if file.displayName.isEmpty == false {
files.append(file)
}
}
// Sort
files = files.sorted(){$0.displayName < $1.displayName}
return files
}
}