FileProviderExtension+Thumbnail.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // FileProviderExtension+Thumbnail.swift
  3. // PickerFileProvider
  4. //
  5. // Created by Marino Faggiana on 28/05/18.
  6. // Copyright © 2018 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 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. guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata!.directoryID) else {
  38. continue
  39. }
  40. let width = NCUtility.sharedInstance.getScreenWidthForPreview()
  41. let height = NCUtility.sharedInstance.getScreenHeightForPreview()
  42. let ocNetworking = OCnetworking.init(delegate: nil, metadataNet: nil, withUser: providerData.accountUser, withUserID: providerData.accountUserID, withPassword: providerData.accountPassword, withUrl: providerData.accountUrl)
  43. ocNetworking?.downloadPreview(with: metadata!, serverUrl: serverUrl, withWidth: width, andHeight: height, completion: { (message, errorCode) in
  44. if errorCode == 0 {
  45. do {
  46. let url = URL.init(fileURLWithPath: CCUtility.getDirectoryProviderStorageIconFileID(metadata!.fileID, fileNameView: metadata!.fileNameView))
  47. let data = try Data.init(contentsOf: url)
  48. perThumbnailCompletionHandler(itemIdentifier, data, nil)
  49. } catch let error {
  50. print("error: \(error)")
  51. perThumbnailCompletionHandler(itemIdentifier, nil, NSFileProviderError(.noSuchItem))
  52. }
  53. } else {
  54. perThumbnailCompletionHandler(itemIdentifier, nil, NSFileProviderError(.serverUnreachable))
  55. }
  56. counterProgress += 1
  57. if (counterProgress == progress.totalUnitCount) {
  58. completionHandler(nil)
  59. }
  60. })
  61. } else {
  62. counterProgress += 1
  63. if (counterProgress == progress.totalUnitCount) {
  64. completionHandler(nil)
  65. }
  66. }
  67. } else {
  68. counterProgress += 1
  69. if (counterProgress == progress.totalUnitCount) {
  70. completionHandler(nil)
  71. }
  72. }
  73. }
  74. return progress
  75. }
  76. }