fix: add file magic number verification to diagnose corruption

This commit is contained in:
yumoqing 2026-07-03 08:18:33 +08:00
parent beaeb3b4c6
commit 0e172e015e

View File

@ -271,7 +271,15 @@ final class PlayerBridge: ObservableObject {
let exists = FileManager.default.fileExists(atPath: path)
let readable = FileManager.default.isReadableFile(atPath: path)
let fileSize = (try? FileManager.default.attributesOfItem(atPath: path)[.size] as? Int) ?? 0
diagLog("Local file check: exists=\(exists) readable=\(readable) size=\(fileSize)")
// 4/
var magic: UInt32 = 0
if let fh = try? FileHandle(forReadingFrom: item.url) {
if let d = try? fh.read(upToCount: 4), d.count >= 4 {
magic = d.withUnsafeBytes { $0.load(as: UInt32.self) }
}
try? fh.close()
}
diagLog("Local file check: exists=\(exists) readable=\(readable) size=\(fileSize) magic=\(String(format:"%08X", magic))")
}
// KVO