NCManageDatabase+PhotoLibrary.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // NCManageDatabase+PhotoLibrary.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 13/11/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 tablePhotoLibrary: Object {
  27. @objc dynamic var account = ""
  28. @objc dynamic var assetLocalIdentifier = ""
  29. @objc dynamic var creationDate: NSDate?
  30. @objc dynamic var idAsset = ""
  31. @objc dynamic var modificationDate: NSDate?
  32. @objc dynamic var mediaType: Int = 0
  33. override static func primaryKey() -> String {
  34. return "idAsset"
  35. }
  36. }
  37. extension NCManageDatabase {
  38. @discardableResult
  39. func addPhotoLibrary(_ assets: [PHAsset], account: String) -> Bool {
  40. do {
  41. let realm = try Realm()
  42. try realm.write {
  43. for asset in assets {
  44. var creationDateString = ""
  45. let addObject = tablePhotoLibrary()
  46. addObject.account = account
  47. addObject.assetLocalIdentifier = asset.localIdentifier
  48. addObject.mediaType = asset.mediaType.rawValue
  49. if let creationDate = asset.creationDate {
  50. addObject.creationDate = creationDate as NSDate
  51. creationDateString = String(describing: creationDate)
  52. }
  53. if let modificationDate = asset.modificationDate {
  54. addObject.modificationDate = modificationDate as NSDate
  55. }
  56. addObject.idAsset = account + asset.localIdentifier + creationDateString
  57. realm.add(addObject, update: .all)
  58. }
  59. }
  60. } catch let error {
  61. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  62. return false
  63. }
  64. return true
  65. }
  66. func getPhotoLibraryIdAsset(image: Bool, video: Bool, account: String) -> [String]? {
  67. var predicate = NSPredicate()
  68. if image && video {
  69. predicate = NSPredicate(format: "account == %@ AND (mediaType == %d OR mediaType == %d)", account, PHAssetMediaType.image.rawValue, PHAssetMediaType.video.rawValue)
  70. } else if image {
  71. predicate = NSPredicate(format: "account == %@ AND mediaType == %d", account, PHAssetMediaType.image.rawValue)
  72. } else if video {
  73. predicate = NSPredicate(format: "account == %@ AND mediaType == %d", account, PHAssetMediaType.video.rawValue)
  74. }
  75. do {
  76. let realm = try Realm()
  77. realm.refresh()
  78. let results = realm.objects(tablePhotoLibrary.self).filter(predicate)
  79. let idsAsset = results.map { $0.idAsset }
  80. return Array(idsAsset)
  81. } catch let error as NSError {
  82. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access to database: \(error)")
  83. }
  84. return nil
  85. }
  86. }