FileProviderExtension+Thumbnail.swift 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 UIKit
  24. import FileProvider
  25. import NCCommunication
  26. extension FileProviderExtension {
  27. override func fetchThumbnails(for itemIdentifiers: [NSFileProviderItemIdentifier], requestedSize size: CGSize, perThumbnailCompletionHandler: @escaping (NSFileProviderItemIdentifier, Data?, Error?) -> Void, completionHandler: @escaping (Error?) -> Void) -> Progress {
  28. let progress = Progress(totalUnitCount: Int64(itemIdentifiers.count))
  29. var counterProgress: Int64 = 0
  30. for itemIdentifier in itemIdentifiers {
  31. guard let metadata = fileProviderUtility.shared.getTableMetadataFromItemIdentifier(itemIdentifier) else {
  32. counterProgress += 1
  33. if (counterProgress == progress.totalUnitCount) { completionHandler(nil) }
  34. continue
  35. }
  36. if (metadata.hasPreview) {
  37. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, account: metadata.account)!
  38. let fileNameIconLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)!
  39. NCCommunication.shared.getPreview(fileNamePath: fileNamePath, widthPreview: NCGlobal.shared.sizeIcon, heightPreview: NCGlobal.shared.sizeIcon) { (account, data, errorCode, errorDescription) in
  40. if errorCode == 0 && data != nil {
  41. do {
  42. try data!.write(to: URL.init(fileURLWithPath: fileNameIconLocalPath), options: .atomic)
  43. } catch { }
  44. perThumbnailCompletionHandler(itemIdentifier, data, nil)
  45. } else {
  46. perThumbnailCompletionHandler(itemIdentifier, nil, NSFileProviderError(.serverUnreachable))
  47. }
  48. counterProgress += 1
  49. if (counterProgress == progress.totalUnitCount) {
  50. completionHandler(nil)
  51. }
  52. }
  53. } else {
  54. counterProgress += 1
  55. if (counterProgress == progress.totalUnitCount) {
  56. completionHandler(nil)
  57. }
  58. }
  59. }
  60. return progress
  61. }
  62. }