NCManageDatabase+Chunk.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // NCManageDatabase+Chunk.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 05/08/23.
  6. // Copyright © 2023 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 RealmSwift
  26. import NextcloudKit
  27. class tableChunk: Object {
  28. @Persisted var account = ""
  29. @Persisted var chunkFolder = ""
  30. @Persisted(primaryKey: true) var index = ""
  31. @Persisted var fileName: Int = 0
  32. @Persisted var ocId = ""
  33. @Persisted var size: Int64 = 0
  34. }
  35. extension NCManageDatabase {
  36. func getChunkFolder(account: String, ocId: String) -> String {
  37. do {
  38. let realm = try Realm()
  39. realm.refresh()
  40. guard let result = realm.objects(tableChunk.self).filter("account == %@ AND ocId == %@", account, ocId).first else { return NSUUID().uuidString }
  41. return result.chunkFolder
  42. } catch let error as NSError {
  43. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access to database: \(error)")
  44. }
  45. return NSUUID().uuidString
  46. }
  47. func getChunks(account: String, ocId: String) -> [(fileName: String, size: Int64)] {
  48. var filesChunk: [(fileName: String, size: Int64)] = []
  49. do {
  50. let realm = try Realm()
  51. realm.refresh()
  52. let results = realm.objects(tableChunk.self).filter("account == %@ AND ocId == %@", account, ocId).sorted(byKeyPath: "fileName", ascending: true)
  53. for result in results {
  54. filesChunk.append((fileName: "\(result.fileName)", size: result.size))
  55. }
  56. } catch let error as NSError {
  57. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access to database: \(error)")
  58. }
  59. return filesChunk
  60. }
  61. func addChunks(account: String, ocId: String, chunkFolder: String, filesChunk: [(fileName: String, size: Int64)]) {
  62. do {
  63. let realm = try Realm()
  64. try realm.write {
  65. let results = realm.objects(tableChunk.self).filter(NSPredicate(format: "account == %@ AND ocId == %@", account, ocId))
  66. realm.delete(results)
  67. for fileChunk in filesChunk {
  68. let object = tableChunk()
  69. object.account = account
  70. object.chunkFolder = chunkFolder
  71. object.fileName = Int(fileChunk.fileName) ?? 0
  72. object.index = ocId + fileChunk.fileName
  73. object.ocId = ocId
  74. object.size = fileChunk.size
  75. realm.add(object, update: .all)
  76. }
  77. }
  78. } catch let error {
  79. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  80. }
  81. }
  82. func deleteChunk(account: String, ocId: String, fileChunk: (fileName: String, size: Int64), directory: String) {
  83. do {
  84. let realm = try Realm()
  85. try realm.write {
  86. let result = realm.objects(tableChunk.self).filter(NSPredicate(format: "account == %@ AND ocId == %@ AND fileName == %d", account, ocId, Int(fileChunk.fileName) ?? 0))
  87. realm.delete(result)
  88. let filePath = directory + "/\(fileChunk.fileName)"
  89. utilityFileSystem.removeFile(atPath: filePath)
  90. }
  91. } catch let error {
  92. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  93. }
  94. }
  95. func deleteChunks(account: String, ocId: String, directory: String) {
  96. do {
  97. let realm = try Realm()
  98. try realm.write {
  99. let results = realm.objects(tableChunk.self).filter(NSPredicate(format: "account == %@ AND ocId == %@", account, ocId))
  100. for result in results {
  101. let filePath = directory + "/\(result.fileName)"
  102. utilityFileSystem.removeFile(atPath: filePath)
  103. }
  104. realm.delete(results)
  105. }
  106. } catch let error {
  107. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  108. }
  109. }
  110. }