mirror of
https://github.com/ninxsoft/Mist.git
synced 2025-05-21 18:55:24 -04:00
59 lines
1.5 KiB
Swift
59 lines
1.5 KiB
Swift
//
|
|
// URL+Extension.swift
|
|
// Mist
|
|
//
|
|
// Created by Nindi Gill on 17/6/2022.
|
|
//
|
|
|
|
import CryptoKit
|
|
import Foundation
|
|
|
|
extension URL {
|
|
func fileSize() throws -> UInt64 {
|
|
let urlResourceKeys: Set<URLResourceKey> = [
|
|
.isRegularFileKey,
|
|
.fileAllocatedSizeKey,
|
|
.totalFileAllocatedSizeKey
|
|
]
|
|
|
|
let resourceValues: URLResourceValues = try self.resourceValues(forKeys: urlResourceKeys)
|
|
|
|
guard let isRegularFile: Bool = resourceValues.isRegularFile,
|
|
isRegularFile else {
|
|
return 0
|
|
}
|
|
|
|
return UInt64(resourceValues.totalFileAllocatedSize ?? resourceValues.fileAllocatedSize ?? 0)
|
|
}
|
|
|
|
func shasum() -> String? {
|
|
let length: Int = 1_024 * 1_024 * 50 // 50 MB
|
|
|
|
do {
|
|
let fileHandle: FileHandle = try FileHandle(forReadingFrom: self)
|
|
|
|
defer {
|
|
fileHandle.closeFile()
|
|
}
|
|
|
|
var shasum: Insecure.SHA1 = .init()
|
|
|
|
while try autoreleasepool(invoking: {
|
|
try Task.checkCancellation()
|
|
let data: Data = fileHandle.readData(ofLength: length)
|
|
|
|
if !data.isEmpty {
|
|
shasum.update(data: data)
|
|
}
|
|
|
|
return !data.isEmpty
|
|
}) { }
|
|
|
|
let data: Data = .init(shasum.finalize())
|
|
return data.map { String(format: "%02hhx", $0) }.joined()
|
|
} catch {
|
|
print(error.localizedDescription)
|
|
return nil
|
|
}
|
|
}
|
|
}
|