FileProviderExtension+Thumbnail.swift 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. import FileProvider
  9. extension FileProviderExtension {
  10. override func fetchThumbnails(for itemIdentifiers: [NSFileProviderItemIdentifier], requestedSize size: CGSize, perThumbnailCompletionHandler: @escaping (NSFileProviderItemIdentifier, Data?, Error?) -> Void, completionHandler: @escaping (Error?) -> Void) -> Progress {
  11. /* ONLY iOS 11*/
  12. guard #available(iOS 11, *) else { return Progress(totalUnitCount:0) }
  13. let progress = Progress(totalUnitCount: Int64(itemIdentifiers.count))
  14. var counterProgress: Int64 = 0
  15. // Check account
  16. if providerData.setupActiveAccount() == false {
  17. completionHandler(NSFileProviderError(.notAuthenticated))
  18. return Progress(totalUnitCount:0)
  19. }
  20. for itemIdentifier in itemIdentifiers {
  21. let metadata = providerData.getTableMetadataFromItemIdentifier(itemIdentifier)
  22. if metadata != nil {
  23. if (metadata!.typeFile == k_metadataTypeFile_image || metadata!.typeFile == k_metadataTypeFile_video) {
  24. let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata!.directoryID)
  25. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata!.fileName, serverUrl: serverUrl, activeUrl: providerData.accountUrl)
  26. let ocNetworking = OCnetworking.init(delegate: nil, metadataNet: nil, withUser: providerData.accountUser, withUserID: providerData.accountUserID, withPassword: providerData.accountPassword, withUrl: providerData.accountUrl)
  27. ocNetworking?.downloadThumbnail(withDimOfThumbnail: "m", fileID: metadata!.fileID, fileNamePath: fileNamePath, fileNameView: metadata!.fileNameView, success: {
  28. do {
  29. let url = URL.init(fileURLWithPath: CCUtility.getDirectoryProviderStorageIconFileID(metadata!.fileID, fileNameView: metadata?.fileNameView))
  30. let data = try Data.init(contentsOf: url)
  31. perThumbnailCompletionHandler(itemIdentifier, data, nil)
  32. } catch let error {
  33. print("error: \(error)")
  34. perThumbnailCompletionHandler(itemIdentifier, nil, NSFileProviderError(.noSuchItem))
  35. }
  36. counterProgress += 1
  37. if (counterProgress == progress.totalUnitCount) {
  38. completionHandler(nil)
  39. }
  40. }, failure: { (errorMessage, errorCode) in
  41. perThumbnailCompletionHandler(itemIdentifier, nil, NSFileProviderError(.serverUnreachable))
  42. counterProgress += 1
  43. if (counterProgress == progress.totalUnitCount) {
  44. completionHandler(nil)
  45. }
  46. })
  47. } else {
  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. }