FileProviderExtension+NetworkingDelegate.swift 3.9 KB

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