Problem
When grabbing the .fileSizeKey
and .localizedTypeDescriptionKey
on a URL.resourceValues
's for an non-downloaded iCloud file with the .icloud
extension, I get an incorrect file size and the description is listed as Alias.
var url = URL(fileURLWithPath: "/.image.png.icloud")
var fileResources: URLResourceValues?
let keys: Set<URLResourceKey> = [.localizedTypeDescriptionKey, .fileSizeKey]
do {
guard let resources = try url?.resourceValues(forKeys: keys) else { return }
fileResources = resources
} catch {}
print(fileResources?.localizedTypeDescription) // Alias
print(fileResources?.fileSize) // 192 bytes (when it should be 4MB)
.
What I've Tried
- I've tried setting up a
NSFileCoordinator()
to request the metadata for the file but that didn't work.
var error: NSError?
let coordinator = NSFileCoordinator(filePresenter: nil)
coordinator.coordinate(readingItemAt: url!, options: .immediatelyAvailableMetadataOnly, error: &error) { URL in
do {
let resources = try URL.promisedItemResourceValues(forKeys: [.fileSizeKey])
print(resources.fileSize) // 192 bytes (file type reads as Alias too)
} catch {}
}
- I've also tried MDItem and it does work, but it's unreliable because sometimes there's no metadata (on mail files for example) and I had to turn off sandboxing to get it to work.
let md = MDItemCreateWithURL(kCFAllocatorDefault, url! as CFURL) // nil if sandbox is enabled
let meta = MDItemCopyAttribute(md, kMDItemFSSize)
print(meta) // 4MB (correct size)
- I've also tried taking the
.image.png.icloud
url and tried to find the alias original url, but it doesn't seem possible as the file isn't actually an alias.
.
What I'm Considering
I imagine this is possible somehow as Finder's Get Info Pane has no problem getting the info. I've wondered if maybe it's possible to change the file and remove the '.'
and the '.icloud'
components of the url temporarily but I'm not sure how to go about this because I believe it involves duplication or copying of the file.
Thank you in advance! I posted this on Stack Overflow as well