Partial Download Support for macOS NSFileProvider Implementation

I've implemented a file provider for macos, and it works well. Now I'm trying to add partial download support in my implementation. however, it doesn't seem to work.

As per documentation, I've implemented

protocol NSFileProviderPartialContentFetching

and its method

func fetchPartialContents(for: NSFileProviderItemIdentifier, version: NSFileProviderItemVersion, request: NSFileProviderRequest, minimalRange: NSRange, aligningTo: Int, options: NSFileProviderFetchContentsOptions, completionHandler: (URL?, NSFileProviderItem?, NSRange, NSFileProviderMaterializationFlags, (any Error)?) -> Void) -> Progress

Now, I'm testing my implementation, and I open a movie from my File Provider using VLC. I see that fetchPartialContents get a hit. I download requested range, and return it through completionHandler. However VLC gives a read error.

here is my implementation of fetchPartialContents:

private func align(range: NSRange, to alignment: Int) -> NSRange {
           let start = range.location - (range.location % alignment)
            let end = range.location + range.length
            let alignedEnd = ((end + alignment - 1) / alignment) * alignment
            let alignedLength = alignedEnd - start
            return NSRange(location: start, length: alignedLength)
}

unc fetchPartialContents(for itemIdentifier: NSFileProviderItemIdentifier, version requestedVersion: NSFileProviderItemVersion, request: NSFileProviderRequest, minimalRange requestedRange: NSRange, aligningTo alignment: Int, options: NSFileProviderFetchContentsOptions = [], completionHandler: @escaping (URL?, NSFileProviderItem?, NSRange, NSFileProviderMaterializationFlags, (any Error)?) -> Void) -> Progress {

        let alignedRange = align(range: requestedRange, to: alignment)
        let progress = Progress(totalUnitCount: 100)
        let data = downloadData(for: alignedRange)               
        progress.completedUnitCount = 100;

                    tempFileHandle = try FileHandle(forWritingTo: tempFileURL)
                    tempFileHandle.seek(toFileOffset: UInt64(alignedRange.location))
                    tempFileHandle.write(data!)
                    tempFileHandle.closeFile()

       let item = FileProviderItem(identifier: itemIdentifier,)
       completionHandler(tempFileURL, item, alignedRange, [], nil)
       } catch {
               completionHandler(nil, nil, alignedRange, [], error)
      }
}

I beleive I'm doing everhting correctly & as per documentation, but certainly there is something is wrong. Can someone help me about it?

cheers,

Partial fetching is implemented in the following Apple sample:

Would you mind to give it try and see if you can reproduce the issue with it? And you can, please share the detailed steps you use to observe the issue. I'd take a look from there.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

This works however now VLC is not triggering fetchPatialContents anymore. Is there way we could enable streaming for our file provider so applications like VLC should always do the partial downloads?

Partial Download Support for macOS NSFileProvider Implementation
 
 
Q