forked from shadowfacts/Tusker
36 lines
1.1 KiB
Swift
36 lines
1.1 KiB
Swift
//
|
|
// FileManager+Size.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 11/28/22.
|
|
// Copyright © 2022 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
extension FileManager {
|
|
func recursiveSize(url: URL) -> Int64? {
|
|
if (try? url.resourceValues(forKeys: [.isRegularFileKey]).isRegularFile) == true {
|
|
return size(url: url)
|
|
} else {
|
|
guard let enumerator = enumerator(at: url, includingPropertiesForKeys: [.isRegularFileKey, .fileSizeKey, .totalFileAllocatedSizeKey]) else {
|
|
return nil
|
|
}
|
|
var total: Int64 = 0
|
|
for case let url as URL in enumerator {
|
|
total += size(url: url) ?? 0
|
|
}
|
|
return total
|
|
}
|
|
}
|
|
}
|
|
|
|
private func size(url: URL) -> Int64? {
|
|
guard let resourceValues = try? url.resourceValues(forKeys: [.isRegularFileKey, .fileSizeKey, .totalFileAllocatedSizeKey]),
|
|
resourceValues.isRegularFile ?? false,
|
|
let size = resourceValues.fileSize ?? resourceValues.totalFileAllocatedSize else {
|
|
return nil
|
|
}
|
|
return Int64(size)
|
|
}
|