FileProviderExtension+Thumbnail.swift 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 NextcloudKit
  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, userId: metadata.userId, account: metadata.account)!
  38. let fileNameIconLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)!
  39. if let urlBase = metadata.urlBase.urlEncoded,
  40. let fileNamePath = fileNamePath.urlEncoded,
  41. let url = URL(string: "\(urlBase)/index.php/core/preview.png?file=\(fileNamePath)&x=\(size.width)&y=\(size.height)&a=1&mode=cover") {
  42. NextcloudKit.shared.getPreview(url: url) { _, data, error in
  43. if error == .success && data != nil {
  44. do {
  45. try data!.write(to: URL(fileURLWithPath: fileNameIconLocalPath), options: .atomic)
  46. } catch { }
  47. perThumbnailCompletionHandler(itemIdentifier, data, nil)
  48. } else {
  49. perThumbnailCompletionHandler(itemIdentifier, nil, NSFileProviderError(.serverUnreachable))
  50. }
  51. counterProgress += 1
  52. if counterProgress == progress.totalUnitCount {
  53. completionHandler(nil)
  54. }
  55. }
  56. }
  57. } else {
  58. counterProgress += 1
  59. if counterProgress == progress.totalUnitCount {
  60. completionHandler(nil)
  61. }
  62. }
  63. }
  64. return progress
  65. }
  66. }