FileProvider.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // FileProvider.swift
  3. // PickerFileProvider
  4. //
  5. // Created by Marino Faggiana on 27/12/16.
  6. // Copyright © 2016 TWS. All rights reserved.
  7. //
  8. import UIKit
  9. class FileProvider: NSFileProviderExtension {
  10. var fileCoordinator: NSFileCoordinator {
  11. let fileCoordinator = NSFileCoordinator()
  12. fileCoordinator.purposeIdentifier = self.providerIdentifier
  13. return fileCoordinator
  14. }
  15. override init() {
  16. super.init()
  17. self.fileCoordinator.coordinate(writingItemAt: self.documentStorageURL, options: [], error: nil, byAccessor: { newURL in
  18. // ensure the documentStorageURL actually exists
  19. do {
  20. try FileManager.default.createDirectory(at: newURL, withIntermediateDirectories: true, attributes: nil)
  21. } catch {
  22. // Handle error
  23. }
  24. })
  25. }
  26. override func providePlaceholder(at url: URL, completionHandler: ((_ error: Error?) -> Void)?) {
  27. // Should call writePlaceholderAtURL(_:withMetadata:error:) with the placeholder URL, then call the completion handler with the error if applicable.
  28. let fileName = url.lastPathComponent
  29. let placeholderURL = NSFileProviderExtension.placeholderURL(for: self.documentStorageURL.appendingPathComponent(fileName))
  30. // TODO: get file size for file at <url> from model
  31. let fileSize = 0
  32. let metadata = [AnyHashable(URLResourceKey.fileSizeKey): fileSize]
  33. do {
  34. try NSFileProviderExtension.writePlaceholder(at: placeholderURL, withMetadata: metadata)
  35. } catch {
  36. // Handle error
  37. }
  38. completionHandler?(nil)
  39. }
  40. override func startProvidingItem(at url: URL, completionHandler: ((_ error: Error?) -> Void)?) {
  41. // Should ensure that the actual file is in the position returned by URLForItemWithIdentifier, then call the completion handler
  42. // TODO: get the contents of file at <url> from model
  43. let fileData = NSData()
  44. do {
  45. _ = try fileData.write(to: url, options: [])
  46. } catch {
  47. // Handle error
  48. }
  49. completionHandler?(nil);
  50. }
  51. override func itemChanged(at url: URL) {
  52. // Called at some point after the file has changed; the provider may then trigger an upload
  53. // TODO: mark file at <url> as needing an update in the model; kick off update process
  54. NSLog("Item changed at URL %@", url as NSURL)
  55. }
  56. override func stopProvidingItem(at url: URL) {
  57. // Called after the last claim to the file has been released. At this point, it is safe for the file provider to remove the content file.
  58. // Care should be taken that the corresponding placeholder file stays behind after the content file has been deleted.
  59. do {
  60. _ = try FileManager.default.removeItem(at: url)
  61. } catch {
  62. // Handle error
  63. }
  64. self.providePlaceholder(at: url, completionHandler: { error in
  65. // TODO: handle any error, do any necessary cleanup
  66. })
  67. }
  68. }