FileProviderExtension+Thumbnail.swift 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // FileProviderExtension+Thumbnail.swift
  3. // PickerFileProvider
  4. //
  5. // Created by Marino Faggiana on 28/05/18.
  6. // Copyright © 2018 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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 FileProvider
  24. extension FileProviderExtension {
  25. override func fetchThumbnails(for itemIdentifiers: [NSFileProviderItemIdentifier], requestedSize size: CGSize, perThumbnailCompletionHandler: @escaping (NSFileProviderItemIdentifier, Data?, Error?) -> Void, completionHandler: @escaping (Error?) -> Void) -> Progress {
  26. let progress = Progress(totalUnitCount: Int64(itemIdentifiers.count))
  27. var counterProgress: Int64 = 0
  28. // Check account
  29. if providerData.setupActiveAccount() == false {
  30. completionHandler(NSFileProviderError(.notAuthenticated))
  31. return Progress(totalUnitCount:0)
  32. }
  33. for itemIdentifier in itemIdentifiers {
  34. guard let metadata = providerData.getTableMetadataFromItemIdentifier(itemIdentifier) else {
  35. counterProgress += 1
  36. if (counterProgress == progress.totalUnitCount) {
  37. completionHandler(nil)
  38. }
  39. continue
  40. }
  41. if (metadata.hasPreview == 1) {
  42. let width = NCUtility.sharedInstance.getScreenWidthForPreview()
  43. let height = NCUtility.sharedInstance.getScreenHeightForPreview()
  44. OCNetworking.sharedManager().downloadPreview(withAccount: providerData.account, metadata: metadata, withWidth: width, andHeight: height, completion: { (account, preview, message, errorCode) in
  45. if errorCode == 0 && account == self.providerData.account {
  46. do {
  47. let url = URL.init(fileURLWithPath: CCUtility.getDirectoryProviderStorageIconFileID(metadata.fileID, fileNameView: metadata.fileNameView))
  48. let data = try Data.init(contentsOf: url)
  49. perThumbnailCompletionHandler(itemIdentifier, data, nil)
  50. } catch let error {
  51. print("error: \(error)")
  52. perThumbnailCompletionHandler(itemIdentifier, nil, NSFileProviderError(.noSuchItem))
  53. }
  54. } else {
  55. perThumbnailCompletionHandler(itemIdentifier, nil, NSFileProviderError(.serverUnreachable))
  56. }
  57. counterProgress += 1
  58. if (counterProgress == progress.totalUnitCount) {
  59. completionHandler(nil)
  60. }
  61. })
  62. } else {
  63. counterProgress += 1
  64. if (counterProgress == progress.totalUnitCount) {
  65. completionHandler(nil)
  66. }
  67. }
  68. }
  69. return progress
  70. }
  71. }