NCManageDatabase+Chunk.swift 4.4 KB

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