FileProvider.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // FileProvider.swift
  3. // PickerFileProvider
  4. //
  5. // Created by Marino Faggiana on 27/12/16.
  6. // Copyright © 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. class FileProvider: NSFileProviderExtension {
  25. var fileCoordinator: NSFileCoordinator {
  26. let fileCoordinator = NSFileCoordinator()
  27. fileCoordinator.purposeIdentifier = self.providerIdentifier
  28. return fileCoordinator
  29. }
  30. override init() {
  31. super.init()
  32. self.fileCoordinator.coordinate(writingItemAt: self.documentStorageURL, options: [], error: nil, byAccessor: { newURL in
  33. // ensure the documentStorageURL actually exists
  34. do {
  35. try FileManager.default.createDirectory(at: newURL, withIntermediateDirectories: true, attributes: nil)
  36. } catch {
  37. // Handle error
  38. }
  39. })
  40. }
  41. override func providePlaceholder(at url: URL, completionHandler: ((_ error: Error?) -> Void)?) {
  42. // Should call writePlaceholderAtURL(_:withMetadata:error:) with the placeholder URL, then call the completion handler with the error if applicable.
  43. let fileName = url.lastPathComponent
  44. let placeholderURL = NSFileProviderExtension.placeholderURL(for: self.documentStorageURL.appendingPathComponent(fileName))
  45. // TODO: get file size for file at <url> from model
  46. let fileSize = 0
  47. let metadata = [AnyHashable(URLResourceKey.fileSizeKey): fileSize]
  48. do {
  49. try NSFileProviderExtension.writePlaceholder(at: placeholderURL, withMetadata: metadata)
  50. } catch {
  51. // Handle error
  52. }
  53. completionHandler?(nil)
  54. }
  55. override func startProvidingItem(at url: URL, completionHandler: ((_ error: Error?) -> Void)?) {
  56. guard let fileData = try? Data(contentsOf: url) else {
  57. // NOTE: you would generate an NSError to supply to the completionHandler
  58. // here however that is outside of the scope for this tutorial
  59. completionHandler?(nil)
  60. return
  61. }
  62. do {
  63. _ = try fileData.write(to: url, options: NSData.WritingOptions())
  64. completionHandler?(nil)
  65. } catch let error as NSError {
  66. print("error writing file to URL")
  67. completionHandler?(error)
  68. }
  69. }
  70. override func itemChanged(at url: URL) {
  71. // Called at some point after the file has changed; the provider may then trigger an upload
  72. // TODO: mark file at <url> as needing an update in the model; kick off update process
  73. NSLog("Item changed at URL %@", url as NSURL)
  74. }
  75. override func stopProvidingItem(at url: URL) {
  76. // 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.
  77. // Care should be taken that the corresponding placeholder file stays behind after the content file has been deleted.
  78. do {
  79. _ = try FileManager.default.removeItem(at: url)
  80. } catch {
  81. // Handle error
  82. }
  83. self.providePlaceholder(at: url, completionHandler: { error in
  84. // TODO: handle any error, do any necessary cleanup
  85. })
  86. }
  87. }