34 lines
1.2 KiB
Swift
34 lines
1.2 KiB
Swift
//
|
|
// FileManager+Ext.swift
|
|
// gen_html
|
|
//
|
|
// Created by Isaac Paul on 4/30/25.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
extension FileManager {
|
|
//TODO: should also check if url is file
|
|
func listFiles(_ directory:URL, withLowercaseExtensions:[String] = [], recursive:Bool = false) throws -> [URL] {
|
|
let options:FileManager.DirectoryEnumerationOptions = recursive ? [] : [.skipsSubdirectoryDescendants]
|
|
let fileIterator = try self.contentsOfDirectory(at: directory, includingPropertiesForKeys: [.isDirectoryKey], options: options)
|
|
let filesOnly = fileIterator.filter { (url) -> Bool in
|
|
do {
|
|
let resourceValues = try url.resourceValues(forKeys: [.isDirectoryKey])
|
|
let isDirectory = resourceValues.isDirectory ?? true
|
|
if (isDirectory) {
|
|
return false
|
|
}
|
|
} catch { return false }
|
|
|
|
if withLowercaseExtensions.count > 0 {
|
|
let pathExt = url.pathExtension.lowercased()
|
|
let contains = withLowercaseExtensions.firstIndex(of: pathExt)
|
|
return contains != nil
|
|
}
|
|
return true
|
|
}
|
|
return filesOnly
|
|
}
|
|
}
|