FileProviderExtension+Thumbnail.swift 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. let metadata = providerData.getTableMetadataFromItemIdentifier(itemIdentifier)
  35. if metadata != nil {
  36. if (metadata!.typeFile == k_metadataTypeFile_image || metadata!.typeFile == k_metadataTypeFile_video) {
  37. let width = NCUtility.sharedInstance.getScreenWidthForPreview()
  38. let height = NCUtility.sharedInstance.getScreenHeightForPreview()
  39. let ocNetworking = OCnetworking.init(delegate: nil, metadataNet: nil, withUser: providerData.accountUser, withUserID: providerData.accountUserID, withPassword: providerData.accountPassword, withUrl: providerData.accountUrl)
  40. ocNetworking?.downloadPreview(with: metadata!, withWidth: width, andHeight: height, completion: { (message, errorCode) in
  41. if errorCode == 0 {
  42. do {
  43. let url = URL.init(fileURLWithPath: CCUtility.getDirectoryProviderStorageIconFileID(metadata!.fileID, fileNameView: metadata!.fileNameView))
  44. let data = try Data.init(contentsOf: url)
  45. perThumbnailCompletionHandler(itemIdentifier, data, nil)
  46. } catch let error {
  47. print("error: \(error)")
  48. perThumbnailCompletionHandler(itemIdentifier, nil, NSFileProviderError(.noSuchItem))
  49. }
  50. } else {
  51. perThumbnailCompletionHandler(itemIdentifier, nil, NSFileProviderError(.serverUnreachable))
  52. }
  53. counterProgress += 1
  54. if (counterProgress == progress.totalUnitCount) {
  55. completionHandler(nil)
  56. }
  57. })
  58. } else {
  59. counterProgress += 1
  60. if (counterProgress == progress.totalUnitCount) {
  61. completionHandler(nil)
  62. }
  63. }
  64. } else {
  65. counterProgress += 1
  66. if (counterProgress == progress.totalUnitCount) {
  67. completionHandler(nil)
  68. }
  69. }
  70. }
  71. return progress
  72. }
  73. }