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,