FileProviderExtension+NetworkingDelegate.swift 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. DispatchQueue.global(qos: .userInteractive).async {
  37. if error == .success, let ocId {
  38. /// SIGNAL
  39. fileProviderData.shared.signalEnumerator(ocId: metadata.ocIdTemp, type: .delete)
  40. metadata.fileName = fileName
  41. metadata.serverUrl = serverUrl
  42. metadata.uploadDate = (date as? NSDate) ?? NSDate()
  43. metadata.etag = etag ?? ""
  44. metadata.ocId = ocId
  45. metadata.size = size
  46. if let fileId = NCUtility().ocIdToFileId(ocId: ocId) {
  47. metadata.fileId = fileId
  48. }
  49. metadata.sceneIdentifier = nil
  50. metadata.session = ""
  51. metadata.sessionError = ""
  52. metadata.sessionSelector = ""
  53. metadata.sessionDate = nil
  54. metadata.sessionTaskIdentifier = 0
  55. metadata.status = NCGlobal.shared.metadataStatusNormal
  56. NCManageDatabase.shared.addMetadata(metadata)
  57. NCManageDatabase.shared.addLocalFile(metadata: metadata)
  58. /// NEW File
  59. if !metadata.ocIdTemp.isEmpty, ocId != metadata.ocIdTemp {
  60. let atPath = self.utilityFileSystem.getDirectoryProviderStorageOcId(metadata.ocIdTemp)
  61. let toPath = self.utilityFileSystem.getDirectoryProviderStorageOcId(ocId)
  62. self.utilityFileSystem.copyFile(atPath: atPath, toPath: toPath)
  63. NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocIdTemp))
  64. }
  65. /// SIGNAL
  66. fileProviderData.shared.signalEnumerator(ocId: metadata.ocId, type: .update)
  67. } else {
  68. NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocIdTemp))
  69. /// SIGNAL
  70. fileProviderData.shared.signalEnumerator(ocId: metadata.ocIdTemp, type: .delete)
  71. }
  72. }
  73. }
  74. }