NCManageDatabase+Share.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //
  2. // NCManageDatabase+Share.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 20/01/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. typealias tableShare = tableShareV2
  28. class tableShareV2: Object {
  29. @objc dynamic var account = ""
  30. @objc dynamic var canEdit: Bool = false
  31. @objc dynamic var canDelete: Bool = false
  32. @objc dynamic var date: NSDate?
  33. @objc dynamic var displaynameFileOwner = ""
  34. @objc dynamic var displaynameOwner = ""
  35. @objc dynamic var expirationDate: NSDate?
  36. @objc dynamic var fileName = ""
  37. @objc dynamic var fileParent: Int = 0
  38. @objc dynamic var fileSource: Int = 0
  39. @objc dynamic var fileTarget = ""
  40. @objc dynamic var hideDownload: Bool = false
  41. @objc dynamic var idShare: Int = 0
  42. @objc dynamic var itemSource: Int = 0
  43. @objc dynamic var itemType = ""
  44. @objc dynamic var label = ""
  45. @objc dynamic var mailSend: Bool = false
  46. @objc dynamic var mimeType = ""
  47. @objc dynamic var note = ""
  48. @objc dynamic var parent: String = ""
  49. @objc dynamic var password: String = ""
  50. @objc dynamic var path = ""
  51. @objc dynamic var permissions: Int = 0
  52. @objc dynamic var primaryKey = ""
  53. @objc dynamic var sendPasswordByTalk: Bool = false
  54. @objc dynamic var serverUrl = ""
  55. @objc dynamic var shareType: Int = 0
  56. @objc dynamic var shareWith = ""
  57. @objc dynamic var shareWithDisplayname = ""
  58. @objc dynamic var storage: Int = 0
  59. @objc dynamic var storageId = ""
  60. @objc dynamic var token = ""
  61. @objc dynamic var uidFileOwner = ""
  62. @objc dynamic var uidOwner = ""
  63. @objc dynamic var url = ""
  64. @objc dynamic var userClearAt: NSDate?
  65. @objc dynamic var userIcon = ""
  66. @objc dynamic var userMessage = ""
  67. @objc dynamic var userStatus = ""
  68. @objc dynamic var attributes: String?
  69. override static func primaryKey() -> String {
  70. return "primaryKey"
  71. }
  72. }
  73. extension NCManageDatabase {
  74. func addShare(account: String, home: String, shares: [NKShare]) {
  75. do {
  76. let realm = try Realm()
  77. try realm.write {
  78. for share in shares {
  79. let serverUrlPath = home + share.path
  80. guard let serverUrl = utilityFileSystem.deleteLastPath(serverUrlPath: serverUrlPath, home: home) else { continue }
  81. let object = tableShare()
  82. object.account = account
  83. if let fileName = share.path.components(separatedBy: "/").last {
  84. object.fileName = fileName
  85. }
  86. object.serverUrl = serverUrl
  87. object.canEdit = share.canEdit
  88. object.canDelete = share.canDelete
  89. object.date = share.date as? NSDate
  90. object.displaynameFileOwner = share.displaynameFileOwner
  91. object.displaynameOwner = share.displaynameOwner
  92. object.expirationDate = share.expirationDate
  93. object.fileParent = share.fileParent
  94. object.fileSource = share.fileSource
  95. object.fileTarget = share.fileTarget
  96. object.hideDownload = share.hideDownload
  97. object.idShare = share.idShare
  98. object.itemSource = share.itemSource
  99. object.itemType = share.itemType
  100. object.label = share.label
  101. object.mailSend = share.mailSend
  102. object.mimeType = share.mimeType
  103. object.note = share.note
  104. object.parent = share.parent
  105. object.password = share.password
  106. object.path = share.path
  107. object.permissions = share.permissions
  108. object.primaryKey = account + " " + String(share.idShare)
  109. object.sendPasswordByTalk = share.sendPasswordByTalk
  110. object.shareType = share.shareType
  111. object.shareWith = share.shareWith
  112. object.shareWithDisplayname = share.shareWithDisplayname
  113. object.storage = share.storage
  114. object.storageId = share.storageId
  115. object.token = share.token
  116. object.uidOwner = share.uidOwner
  117. object.uidFileOwner = share.uidFileOwner
  118. object.url = share.url
  119. object.userClearAt = share.userClearAt as? NSDate
  120. object.userIcon = share.userIcon
  121. object.userMessage = share.userMessage
  122. object.userStatus = share.userStatus
  123. object.attributes = share.attributes
  124. realm.add(object, update: .all)
  125. }
  126. }
  127. } catch let error {
  128. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  129. }
  130. }
  131. func getTableShares(account: String) -> [tableShare] {
  132. do {
  133. let realm = try Realm()
  134. realm.refresh()
  135. let sortProperties = [SortDescriptor(keyPath: "shareType", ascending: false), SortDescriptor(keyPath: "idShare", ascending: false)]
  136. let results = realm.objects(tableShare.self).filter("account == %@", account).sorted(by: sortProperties)
  137. return Array(results.map { tableShare.init(value: $0) })
  138. } catch let error as NSError {
  139. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  140. }
  141. return []
  142. }
  143. func getTableShares(metadata: tableMetadata) -> (firstShareLink: tableShare?, share: [tableShare]?) {
  144. do {
  145. let realm = try Realm()
  146. realm.refresh()
  147. let sortProperties = [SortDescriptor(keyPath: "shareType", ascending: false), SortDescriptor(keyPath: "idShare", ascending: false)]
  148. let firstShareLink = realm.objects(tableShare.self).filter("account == %@ AND serverUrl == %@ AND fileName == %@ AND shareType == 3", metadata.account, metadata.serverUrl, metadata.fileName).first
  149. if let firstShareLink = firstShareLink {
  150. let results = realm.objects(tableShare.self).filter("account == %@ AND serverUrl == %@ AND fileName == %@ AND idShare != %d", metadata.account, metadata.serverUrl, metadata.fileName, firstShareLink.idShare).sorted(by: sortProperties)
  151. return(firstShareLink: tableShare.init(value: firstShareLink), share: Array(results.map { tableShare.init(value: $0) }))
  152. } else {
  153. let results = realm.objects(tableShare.self).filter("account == %@ AND serverUrl == %@ AND fileName == %@", metadata.account, metadata.serverUrl, metadata.fileName).sorted(by: sortProperties)
  154. return(firstShareLink: firstShareLink, share: Array(results.map { tableShare.init(value: $0) }))
  155. }
  156. } catch let error as NSError {
  157. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  158. }
  159. return (nil, nil)
  160. }
  161. func getTableShare(account: String, idShare: Int) -> tableShare? {
  162. do {
  163. let realm = try Realm()
  164. realm.refresh()
  165. guard let result = realm.objects(tableShare.self).filter("account = %@ AND idShare = %d", account, idShare).first else { return nil }
  166. return tableShare.init(value: result)
  167. } catch let error as NSError {
  168. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  169. }
  170. return nil
  171. }
  172. func getTableShares(account: String, serverUrl: String) -> [tableShare] {
  173. do {
  174. let realm = try Realm()
  175. realm.refresh()
  176. let sortProperties = [SortDescriptor(keyPath: "shareType", ascending: false), SortDescriptor(keyPath: "idShare", ascending: false)]
  177. let results = realm.objects(tableShare.self).filter("account == %@ AND serverUrl == %@", account, serverUrl).sorted(by: sortProperties)
  178. return Array(results.map { tableShare.init(value: $0) })
  179. } catch let error as NSError {
  180. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  181. }
  182. return []
  183. }
  184. func getTableShares(account: String, serverUrl: String, fileName: String) -> [tableShare] {
  185. do {
  186. let realm = try Realm()
  187. realm.refresh()
  188. let sortProperties = [SortDescriptor(keyPath: "shareType", ascending: false), SortDescriptor(keyPath: "idShare", ascending: false)]
  189. let results = realm.objects(tableShare.self).filter("account == %@ AND serverUrl == %@ AND fileName == %@", account, serverUrl, fileName).sorted(by: sortProperties)
  190. return Array(results.map { tableShare.init(value: $0) })
  191. } catch let error as NSError {
  192. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  193. }
  194. return []
  195. }
  196. func deleteTableShare(account: String, idShare: Int) {
  197. do {
  198. let realm = try Realm()
  199. try realm.write {
  200. let result = realm.objects(tableShare.self).filter("account == %@ AND idShare == %d", account, idShare)
  201. realm.delete(result)
  202. }
  203. } catch let error as NSError {
  204. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  205. }
  206. }
  207. func deleteTableShare(account: String, path: String) {
  208. do {
  209. let realm = try Realm()
  210. try realm.write {
  211. let result = realm.objects(tableShare.self).filter("account == %@ AND path == %@", account, path)
  212. realm.delete(result)
  213. }
  214. } catch let error as NSError {
  215. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  216. }
  217. }
  218. func deleteTableShare(account: String) {
  219. do {
  220. let realm = try Realm()
  221. try realm.write {
  222. let result = realm.objects(tableShare.self).filter("account == %@", account)
  223. realm.delete(result)
  224. }
  225. } catch let error as NSError {
  226. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  227. }
  228. }
  229. // There is currently only one share attribute “download” from the scope “permissions”. This attribute is only valid for user and group shares, not for public link shares.
  230. func setAttibuteDownload(state: Bool) -> String? {
  231. if state {
  232. return nil
  233. } else {
  234. return "[{\"scope\":\"permissions\",\"key\":\"download\",\"enabled\":false}]"
  235. }
  236. }
  237. func isAttributeDownloadEnabled(attributes: String?) -> Bool {
  238. if let attributes = attributes, let data = attributes.data(using: .utf8) {
  239. do {
  240. if let json = try JSONSerialization.jsonObject(with: data) as? [Dictionary<String, Any>] {
  241. for sub in json {
  242. let key = sub["key"] as? String
  243. let enabled = sub["enabled"] as? Bool
  244. let scope = sub["scope"] as? String
  245. if key == "download", scope == "permissions", let enabled = enabled {
  246. return enabled
  247. }
  248. }
  249. }
  250. } catch let error as NSError { print(error) }
  251. }
  252. return true
  253. }
  254. }