FileProviderExtension+Thumbnail.swift 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata!.directoryID)
  38. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata!.fileName, serverUrl: serverUrl, activeUrl: providerData.accountUrl)
  39. let ocNetworking = OCnetworking.init(delegate: nil, metadataNet: nil, withUser: providerData.accountUser, withUserID: providerData.accountUserID, withPassword: providerData.accountPassword, withUrl: providerData.accountUrl)
  40. ocNetworking?.downloadThumbnail(withDimOfThumbnail: "m", fileID: metadata!.fileID, fileNamePath: fileNamePath, fileNameView: metadata!.fileNameView, success: {
  41. do {
  42. let url = URL.init(fileURLWithPath: CCUtility.getDirectoryProviderStorageIconFileID(metadata!.fileID, fileNameView: metadata!.fileNameView))
  43. let data = try Data.init(contentsOf: url)
  44. perThumbnailCompletionHandler(itemIdentifier, data, nil)
  45. } catch let error {
  46. print("error: \(error)")
  47. perThumbnailCompletionHandler(itemIdentifier, nil, NSFileProviderError(.noSuchItem))
  48. }
  49. counterProgress += 1
  50. if (counterProgress == progress.totalUnitCount) {
  51. completionHandler(nil)
  52. }
  53. }, failure: { (errorMessage, errorCode) in
  54. perThumbnailCompletionHandler(itemIdentifier, nil, NSFileProviderError(.serverUnreachable))
  55. counterProgress += 1
  56. if (counterProgress == progress.totalUnitCount) {
  57. completionHandler(nil)
  58. }
  59. })
  60. } else {
  61. counterProgress += 1
  62. if (counterProgress == progress.totalUnitCount) {
  63. completionHandler(nil)
  64. }
  65. }
  66. } else {
  67. counterProgress += 1
  68. if (counterProgress == progress.totalUnitCount) {
  69. completionHandler(nil)
  70. }
  71. }
  72. }
  73. return progress
  74. }
  75. }