FileProviderExtension+NetworkingDelegate.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //
  2. // FileProviderExtension+x.swift
  3. // File Provider Extension
  4. //
  5. // Created by Marino Faggiana on 11/07/24.
  6. // Copyright © 2024 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 Foundation
  24. import UIKit
  25. import UniformTypeIdentifiers
  26. import FileProvider
  27. import NextcloudKit
  28. import Alamofire
  29. extension FileProviderExtension: NCNetworkingDelegate {
  30. func downloadComplete(fileName: String, serverUrl: String, etag: String?, date: Date?, dateLastModified: Date?, length: Int64, task: URLSessionTask, error: NKError) { }
  31. func downloadProgress(_ progress: Float, totalBytes: Int64, totalBytesExpected: Int64, fileName: String, serverUrl: String, session: URLSession, task: URLSessionTask) { }
  32. func uploadProgress(_ progress: Float, totalBytes: Int64, totalBytesExpected: Int64, fileName: String, serverUrl: String, session: URLSession, task: URLSessionTask) { }
  33. func uploadComplete(fileName: String, serverUrl: String, ocId: String?, etag: String?, date: Date?, size: Int64, task: URLSessionTask, error: NKError) {
  34. guard let url = task.currentRequest?.url,
  35. let metadata = NCManageDatabase.shared.getMetadata(from: url, sessionTaskIdentifier: task.taskIdentifier) else { return }
  36. if error == .success, let ocId {
  37. /// SIGNAL
  38. fileProviderData.shared.signalEnumerator(ocId: metadata.ocIdTemp, type: .delete)
  39. metadata.fileName = fileName
  40. metadata.serverUrl = serverUrl
  41. metadata.uploadDate = (date as? NSDate) ?? NSDate()
  42. metadata.etag = etag ?? ""
  43. metadata.ocId = ocId
  44. metadata.size = size
  45. if let fileId = NCUtility().ocIdToFileId(ocId: ocId) {
  46. metadata.fileId = fileId
  47. }
  48. metadata.session = ""
  49. metadata.sessionError = ""
  50. metadata.status = NCGlobal.shared.metadataStatusNormal
  51. NCManageDatabase.shared.addMetadata(metadata)
  52. NCManageDatabase.shared.addLocalFile(metadata: metadata)
  53. /// NEW File
  54. if ocId != metadata.ocIdTemp {
  55. let atPath = utilityFileSystem.getDirectoryProviderStorageOcId(metadata.ocIdTemp)
  56. let toPath = utilityFileSystem.getDirectoryProviderStorageOcId(ocId)
  57. utilityFileSystem.copyFile(atPath: atPath, toPath: toPath)
  58. NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocIdTemp))
  59. }
  60. /// SIGNAL
  61. fileProviderData.shared.signalEnumerator(ocId: metadata.ocId, type: .update)
  62. } else {
  63. NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocIdTemp))
  64. /// SIGNAL
  65. fileProviderData.shared.signalEnumerator(ocId: metadata.ocIdTemp, type: .delete)
  66. }
  67. }
  68. }