How do I get the correct URL.resourceValues for an iCloud file that isn't downloaded?

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

  1. 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 {}
}
  1. 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)
  1. 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

In your NSFileCoordinator test, where exactly does url point?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Here's an example path that the URL would point to

Thanks. Unfortunately that confirms that my only theory about this isn’t viable, and so I’m basically out of ideas )-: You should open a DTS tech support incident and talk to one of DTS’s specialists in this area.

ps DTS is shut down for the week of WWDC, so you won’t get a reply until after that. Oh, and if you’re eligible for WWDC labs, you could ask there.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I was able to get in contact with Developer Technical Support. There's no real answer to this problem unfortunately as there is no public API. Here is what DTS said ⤵︎

Thanks for your patience. I am writing to confirm that there is currently no public API to get the metadata for an un-downloaded >iCloud file. If later on you believe that it has a significant negative impact for your app, please feel free to file a feedback >report for the relevant engineering team to evaluate.


There is however a way to sort of get a working solution as suggested by DTS. You can automatically download the file from iCloud prior to reading its metadata but I wasn't keen on this solution. ⤵︎

var error: NSError?
let fileCoordinator = NSFileCoordinator()

// `theULR` should be the URL without the ‘.’ & ‘.icloud’ components.
fileCoordinator.coordinate(readingItemAt: theURL, options: .withoutChanges, error: &error) { newURL in
   print('\(newURL)') // Try to get the meta data here.
}

After fiddling with this I think the best solution, for now, is to not attempt to get any metadata that could be incorrect from a non-downloaded iCloud file.

How do I get the correct URL.resourceValues for an iCloud file that isn't downloaded?
 
 
Q