NCManageDatabase.swift 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. //
  2. // NCManageDatabase.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 06/05/17.
  6. // Copyright © 2017 TWS. All rights reserved.
  7. //
  8. import RealmSwift
  9. class NCManageDatabase: NSObject {
  10. static let sharedInstance: NCManageDatabase = {
  11. let instance = NCManageDatabase()
  12. return instance
  13. }()
  14. override init() {
  15. let dirGroup = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: NCBrandOptions.sharedInstance.capabilitiesGroups)
  16. var config = Realm.Configuration()
  17. config.fileURL = dirGroup?.appendingPathComponent("\(appDatabaseNextcloud)/\(k_databaseDefault)")
  18. Realm.Configuration.defaultConfiguration = config
  19. }
  20. //MARK: -
  21. //MARK: Utility Database
  22. func clearTable(_ table : Object.Type, account: String?) {
  23. let results : Results<Object>
  24. let realm = try! Realm()
  25. if (account != nil) {
  26. results = realm.objects(table).filter("account = %@", account!)
  27. } else {
  28. results = realm.objects(table)
  29. }
  30. try! realm.write {
  31. realm.delete(results)
  32. }
  33. }
  34. func removeDB() {
  35. let realmURL = Realm.Configuration.defaultConfiguration.fileURL!
  36. let realmURLs = [
  37. realmURL,
  38. realmURL.appendingPathExtension("lock"),
  39. realmURL.appendingPathExtension("note"),
  40. realmURL.appendingPathExtension("management")
  41. ]
  42. for URL in realmURLs {
  43. do {
  44. try FileManager.default.removeItem(at: URL)
  45. } catch {
  46. // handle error
  47. }
  48. }
  49. }
  50. //MARK: -
  51. //MARK: Table Account
  52. func addAccount(_ account: String, url: String, user: String, password: String) {
  53. let realm = try! Realm()
  54. try! realm.write {
  55. let addAccount = tableAccount()
  56. addAccount.account = account
  57. // Brand
  58. if NCBrandOptions.sharedInstance.use_default_automatic_upload {
  59. addAccount.cameraUpload = true
  60. addAccount.cameraUploadPhoto = true
  61. addAccount.cameraUploadVideo = true
  62. addAccount.cameraUploadWWAnVideo = true
  63. }
  64. addAccount.password = password
  65. addAccount.url = url
  66. addAccount.user = user
  67. }
  68. }
  69. func updateAccount(_ account: String, password: String) {
  70. let realm = try! Realm()
  71. let results = realm.objects(tableAccount.self).filter("account = %@", account)
  72. if (results.count > 0) {
  73. try! realm.write {
  74. results[0].password = password
  75. }
  76. }
  77. }
  78. func deleteAccount(_ account: String) {
  79. let realm = try! Realm()
  80. let results = realm.objects(tableAccount.self).filter("account = %@", account)
  81. if (results.count > 0) {
  82. try! realm.write {
  83. realm.delete(results)
  84. }
  85. }
  86. }
  87. func setAccountActive(_ account: String) -> tableAccount {
  88. let realm = try! Realm()
  89. var activeAccount = tableAccount()
  90. let results = realm.objects(tableAccount.self)
  91. try! realm.write {
  92. for result in results {
  93. if result.account == account {
  94. result.active = true
  95. activeAccount = result
  96. } else {
  97. result.active = false
  98. }
  99. }
  100. }
  101. return activeAccount
  102. }
  103. func getAccountActive() -> tableAccount? {
  104. let realm = try! Realm()
  105. let results = realm.objects(tableAccount.self).filter("active = true")
  106. if (results.count > 0) {
  107. return results[0]
  108. } else {
  109. return nil
  110. }
  111. }
  112. func getAccounts(_ account: String?) -> [tableAccount] {
  113. let realm = try! Realm()
  114. let results : Results<tableAccount>
  115. if account == nil {
  116. results = realm.objects(tableAccount.self).sorted(byKeyPath: "account", ascending: true)
  117. } else {
  118. results = realm.objects(tableAccount.self).filter("account = %@", account!).sorted(byKeyPath: "account", ascending: true)
  119. }
  120. return Array(results)
  121. }
  122. // getCameraUploadFolderNameActiveAccount + getCameraUploadFolderPathActiveAccount
  123. func getCameraUploadFolderName(_ account: String, activeUrl : String?) -> String {
  124. let realm = try! Realm()
  125. let results = realm.objects(tableAccount.self).filter("account = %@", account)
  126. if (results.count > 0) {
  127. if results[0].cameraUploadFolderName.characters.count > 0 {
  128. if activeUrl == nil {
  129. return results[0].cameraUploadFolderName
  130. } else {
  131. return results[0].cameraUploadFolderPath
  132. }
  133. } else {
  134. if activeUrl == nil {
  135. return k_folderDefaultCameraUpload
  136. } else {
  137. return CCUtility.getHomeServerUrlActiveUrl(activeUrl!)
  138. }
  139. }
  140. }
  141. return ""
  142. }
  143. // getCameraUploadFolderNamePathActiveAccount
  144. func getCameraUploadFolderNamePath(_ account: String, activeUrl : String) -> String {
  145. let cameraFolderName = self.getCameraUploadFolderName(account, activeUrl: nil)
  146. let cameraFolderPath = self.getCameraUploadFolderName(account, activeUrl: activeUrl)
  147. let folderPhotos = CCUtility.stringAppendServerUrl(cameraFolderPath, addFileName: cameraFolderName)!
  148. return folderPhotos
  149. }
  150. //MARK: -
  151. //MARK: Table Activity
  152. func addActivityServer(_ listOfActivity: [OCActivity], account: String) {
  153. let realm = try! Realm()
  154. try! realm.write {
  155. for activity in listOfActivity {
  156. // Verify
  157. let results = realm.objects(tableActivity.self).filter("idActivity = %d", activity.idActivity)
  158. if (results.count > 0) {
  159. continue
  160. }
  161. // Add new Activity
  162. let addActivity = tableActivity()
  163. addActivity.account = account
  164. if activity.date != nil {
  165. addActivity.date = activity.date! as NSDate
  166. }
  167. addActivity.idActivity = Double(activity.idActivity)
  168. addActivity.link = activity.link
  169. addActivity.note = activity.subject
  170. addActivity.type = k_activityTypeInfo
  171. realm.add(addActivity)
  172. }
  173. }
  174. }
  175. func addActivityClient(_ file: String, fileID: String, action: String, selector: String, note: String, type: String, verbose: Bool, account: String?, activeUrl: String?) {
  176. var noteReplacing : String = ""
  177. if (activeUrl != nil) {
  178. noteReplacing = note.replacingOccurrences(of: "\(activeUrl!)\(webDAV)", with: "")
  179. }
  180. noteReplacing = note.replacingOccurrences(of: "\(k_domain_session_queue).", with: "")
  181. let realm = try! Realm()
  182. try! realm.write {
  183. // Add new Activity
  184. let addActivity = tableActivity()
  185. if (account != nil) {
  186. addActivity.account = account!
  187. }
  188. addActivity.action = action
  189. addActivity.file = file
  190. addActivity.fileID = fileID
  191. addActivity.note = noteReplacing
  192. addActivity.selector = selector
  193. addActivity.type = type
  194. addActivity.verbose = verbose
  195. realm.add(addActivity)
  196. }
  197. }
  198. func getActivityWithPredicate(_ predicate: NSPredicate) -> [tableActivity] {
  199. let realm = try! Realm()
  200. let results = realm.objects(tableActivity.self).filter(predicate).sorted(byKeyPath: "date", ascending: false)
  201. return Array(results)
  202. }
  203. //MARK: -
  204. //MARK: Table Automatic Upload
  205. func addAutomaticUpload(_ metadataNet: CCMetadataNet, account: String) -> Bool {
  206. let realm = try! Realm()
  207. // Verify if exists
  208. let results = realm.objects(tableAutomaticUpload.self).filter("account = %@ AND assetLocalIdentifier = %@", account, metadataNet.assetLocalIdentifier)
  209. if (results.count > 0) {
  210. return false
  211. }
  212. try! realm.write {
  213. // Add new AutomaticUpload
  214. let addAutomaticUpload = tableAutomaticUpload()
  215. addAutomaticUpload.account = account
  216. addAutomaticUpload.assetLocalIdentifier = metadataNet.assetLocalIdentifier
  217. addAutomaticUpload.fileName = metadataNet.fileName
  218. addAutomaticUpload.selector = metadataNet.selector
  219. if (metadataNet.selectorPost != nil) {
  220. addAutomaticUpload.selectorPost = metadataNet.selectorPost
  221. }
  222. addAutomaticUpload.serverUrl = metadataNet.serverUrl
  223. addAutomaticUpload.session = metadataNet.session
  224. addAutomaticUpload.priority = metadataNet.priority
  225. realm.add(addAutomaticUpload)
  226. }
  227. return true
  228. }
  229. func getAutomaticUploadForAccount(_ account: String, selector: String) -> CCMetadataNet? {
  230. let realm = try! Realm()
  231. // Verify if exists
  232. let results = realm.objects(tableAutomaticUpload.self).filter("account = %@ AND selector = %@ AND lock == false", account, selector)
  233. if (results.count == 0) {
  234. return nil
  235. }
  236. let metadataNet = CCMetadataNet()
  237. metadataNet.action = actionUploadAsset
  238. metadataNet.assetLocalIdentifier = results[0].assetLocalIdentifier
  239. metadataNet.fileName = results[0].fileName
  240. metadataNet.priority = results[0].priority
  241. metadataNet.selector = results[0].selector
  242. metadataNet.selectorPost = results[0].selectorPost
  243. metadataNet.serverUrl = results[0].serverUrl
  244. metadataNet.session = results[0].session
  245. metadataNet.taskStatus = Int(k_taskStatusResume)
  246. // Lock True
  247. try! realm.write {
  248. results[0].lock = true
  249. }
  250. return metadataNet
  251. }
  252. func getLockAutomaticUploadForAccount(_ account: String) -> [tableAutomaticUpload] {
  253. let realm = try! Realm()
  254. let results = realm.objects(tableAutomaticUpload.self).filter("account = %@ AND lock = true", account)
  255. return Array(results)
  256. }
  257. func unlockAutomaticUploadForAccount(_ account: String, assetLocalIdentifier: String) {
  258. let realm = try! Realm()
  259. let results = realm.objects(tableAutomaticUpload.self).filter("account = %@ AND assetLocalIdentifier = %@", account, assetLocalIdentifier)
  260. if (results.count > 0) {
  261. // Lock False
  262. try! realm.write {
  263. results[0].lock = false
  264. }
  265. }
  266. }
  267. func deleteAutomaticUploadForAccount(_ account: String, assetLocalIdentifier: String) {
  268. let realm = try! Realm()
  269. let results = realm.objects(tableAutomaticUpload.self).filter("account = %@ AND assetLocalIdentifier = %@", account, assetLocalIdentifier)
  270. if (results.count > 0) {
  271. try! realm.write {
  272. realm.delete(results)
  273. }
  274. }
  275. }
  276. func countAutomaticUploadForAccount(_ account: String, selector: String?) -> Int {
  277. let realm = try! Realm()
  278. let results : Results<tableAutomaticUpload>
  279. if (selector == nil) {
  280. results = realm.objects(tableAutomaticUpload.self).filter("account = %@", account)
  281. } else {
  282. results = realm.objects(tableAutomaticUpload.self).filter("account = %@ AND selector = %@", account, selector!)
  283. }
  284. return results.count
  285. }
  286. //MARK: -
  287. //MARK: Table Capabilities
  288. func addCapabilities(_ capabilities: OCCapabilities, account: String) {
  289. let realm = try! Realm()
  290. let results = realm.objects(tableCapabilities.self).filter("account = %@", account)
  291. try! realm.write {
  292. var resultCapabilities = tableCapabilities()
  293. if (results.count > 0) {
  294. resultCapabilities = results[0]
  295. }
  296. resultCapabilities.account = account
  297. resultCapabilities.themingBackground = capabilities.themingBackground
  298. resultCapabilities.themingColor = capabilities.themingColor
  299. resultCapabilities.themingLogo = capabilities.themingLogo
  300. resultCapabilities.themingName = capabilities.themingName
  301. resultCapabilities.themingSlogan = capabilities.themingSlogan
  302. resultCapabilities.themingUrl = capabilities.themingUrl
  303. resultCapabilities.versionMajor = capabilities.versionMajor
  304. resultCapabilities.versionMinor = capabilities.versionMinor
  305. resultCapabilities.versionMicro = capabilities.versionMicro
  306. resultCapabilities.versionString = capabilities.versionString
  307. if (results.count == 0) {
  308. realm.add(resultCapabilities)
  309. }
  310. }
  311. }
  312. func getCapabilitesForAccount(_ account: String) -> tableCapabilities? {
  313. let realm = try! Realm()
  314. let results = realm.objects(tableCapabilities.self).filter("account = %@", account)
  315. if (results.count > 0) {
  316. return results[0]
  317. } else {
  318. return nil
  319. }
  320. }
  321. func getServerVersionAccount(_ account: String) -> Int {
  322. let realm = try! Realm()
  323. let results = realm.objects(tableCapabilities.self).filter("account = %@", account)
  324. if (results.count > 0) {
  325. return results[0].versionMajor
  326. } else {
  327. return 0
  328. }
  329. }
  330. //MARK: -
  331. //MARK: Table Certificates
  332. func addCertificates(_ certificateLocation: String) {
  333. let realm = try! Realm()
  334. try! realm.write {
  335. let addCertificates = tableCertificates()
  336. addCertificates.certificateLocation = certificateLocation
  337. realm.add(addCertificates)
  338. }
  339. }
  340. func getCertificatesLocation(_ localCertificatesFolder: String) -> [String] {
  341. let realm = try! Realm()
  342. let results = realm.objects(tableCertificates.self)
  343. var arraycertificatePath = [String]()
  344. for result in results {
  345. arraycertificatePath.append("\(localCertificatesFolder)\(result.certificateLocation)")
  346. }
  347. return arraycertificatePath
  348. }
  349. //MARK: -
  350. //MARK: Table External Sites
  351. func addExternalSites(_ externalSites: OCExternalSites, account: String) {
  352. let realm = try! Realm()
  353. try! realm.write {
  354. let addExternalSite = tableExternalSites()
  355. addExternalSite.account = account
  356. addExternalSite.idExternalSite = externalSites.idExternalSite
  357. addExternalSite.icon = externalSites.icon
  358. addExternalSite.lang = externalSites.lang
  359. addExternalSite.name = externalSites.name
  360. addExternalSite.url = externalSites.url
  361. addExternalSite.type = externalSites.type
  362. realm.add(addExternalSite)
  363. }
  364. }
  365. func deleteExternalSitesForAccount(_ account: String) {
  366. let realm = try! Realm()
  367. let results = realm.objects(tableExternalSites.self).filter("account = %@", account)
  368. try! realm.write {
  369. realm.delete(results)
  370. }
  371. }
  372. func getAllExternalSitesWithPredicate(_ predicate: NSPredicate) -> [tableExternalSites] {
  373. let realm = try! Realm()
  374. let results = realm.objects(tableExternalSites.self).filter(predicate).sorted(byKeyPath: "idExternalSite", ascending: true)
  375. return Array(results)
  376. }
  377. //MARK: -
  378. //MARK: Table GPS
  379. func addGeocoderLocation(_ location: String, placemarkAdministrativeArea: String, placemarkCountry: String, placemarkLocality: String, placemarkPostalCode: String, placemarkThoroughfare: String, latitude: String, longitude: String) {
  380. let realm = try! Realm()
  381. // Verify if exists
  382. let results = realm.objects(tableGPS.self).filter("latitude = %@ AND longitude = %@", latitude, longitude)
  383. if (results.count > 0) {
  384. return
  385. }
  386. try! realm.write {
  387. // Add new GPS
  388. let addGPS = tableGPS()
  389. addGPS.location = location
  390. addGPS.placemarkAdministrativeArea = placemarkAdministrativeArea
  391. addGPS.placemarkCountry = placemarkCountry
  392. addGPS.placemarkLocality = placemarkLocality
  393. addGPS.placemarkPostalCode = placemarkPostalCode
  394. addGPS.placemarkThoroughfare = placemarkThoroughfare
  395. addGPS.latitude = latitude
  396. addGPS.longitude = longitude
  397. realm.add(addGPS)
  398. }
  399. }
  400. func getLocationFromGeoLatitude(_ latitude: String, longitude: String) -> String? {
  401. let realm = try! Realm()
  402. let results = realm.objects(tableGPS.self).filter("latitude = %@ AND longitude = %@", latitude, longitude)
  403. if (results.count == 0) {
  404. return nil
  405. } else {
  406. return results[0].location
  407. }
  408. }
  409. //MARK: -
  410. //MARK: Table Share
  411. func addShareLink(_ share: String, fileName: String, serverUrl: String, account: String) -> [String:String] {
  412. let realm = try! Realm()
  413. // Verify if exists
  414. let results = realm.objects(tableShare.self).filter("account = %@ AND fileName = %@ AND serverUrl = %@", account, fileName, serverUrl)
  415. if (results.count > 0) {
  416. try! realm.write {
  417. results[0].shareLink = share;
  418. }
  419. } else {
  420. // Add new record
  421. try! realm.write {
  422. let addShare = tableShare()
  423. addShare.account = account
  424. addShare.fileName = fileName
  425. addShare.serverUrl = serverUrl
  426. addShare.shareLink = share
  427. realm.add(addShare)
  428. }
  429. }
  430. return ["\(serverUrl)\(fileName)" : share]
  431. }
  432. func addShareUserAndGroup(_ share: String, fileName: String, serverUrl: String, account: String) -> [String:String] {
  433. let realm = try! Realm()
  434. // Verify if exists
  435. let results = realm.objects(tableShare.self).filter("account = %@ AND fileName = %@ AND serverUrl = %@", account, fileName, serverUrl)
  436. if (results.count > 0) {
  437. try! realm.write {
  438. results[0].shareUserAndGroup = share;
  439. }
  440. } else {
  441. // Add new record
  442. try! realm.write {
  443. let addShare = tableShare()
  444. addShare.account = account
  445. addShare.fileName = fileName
  446. addShare.serverUrl = serverUrl
  447. addShare.shareUserAndGroup = share
  448. realm.add(addShare)
  449. }
  450. }
  451. return ["\(serverUrl)\(fileName)" : share]
  452. }
  453. func unShare(_ share: String, fileName: String, serverUrl: String, sharesLink: [String:String], sharesUserAndGroup: [String:String], account: String) -> [Any] {
  454. var sharesLink = sharesLink
  455. var sharesUserAndGroup = sharesUserAndGroup
  456. let realm = try! Realm()
  457. let results = realm.objects(tableShare.self).filter("account = %@ AND (shareLink CONTAINS %@ OR shareUserAndGroup CONTAINS %@)", account, share, share)
  458. if (results.count > 0) {
  459. let result = results[0]
  460. realm.beginWrite()
  461. if (result.shareLink.contains(share)) {
  462. result.shareLink = ""
  463. }
  464. if (result.shareUserAndGroup.contains(share)) {
  465. var shares : [String] = result.shareUserAndGroup.components(separatedBy: ",")
  466. if let index = shares.index(of:share) {
  467. shares.remove(at: index)
  468. }
  469. result.shareUserAndGroup = shares.joined(separator: ",")
  470. }
  471. if (result.shareLink.characters.count > 0) {
  472. sharesLink.updateValue(result.shareLink, forKey:"\(serverUrl)\(fileName)")
  473. } else {
  474. sharesLink.removeValue(forKey: "\(serverUrl)\(fileName)")
  475. }
  476. if (result.shareUserAndGroup.characters.count > 0) {
  477. sharesUserAndGroup.updateValue(result.shareUserAndGroup, forKey:"\(serverUrl)\(fileName)")
  478. } else {
  479. sharesUserAndGroup.removeValue(forKey: "\(serverUrl)\(fileName)")
  480. }
  481. if (result.shareLink.characters.count == 0 && result.shareUserAndGroup.characters.count == 0) {
  482. realm.delete(result)
  483. }
  484. try! realm.commitWrite()
  485. }
  486. return [sharesLink, sharesUserAndGroup]
  487. }
  488. func removeShareActiveAccount(_ account: String) {
  489. let realm = try! Realm()
  490. let results = realm.objects(tableShare.self).filter("account = %@", account)
  491. try! realm.write {
  492. realm.delete(results)
  493. }
  494. }
  495. func updateShare(_ items: [String:OCSharedDto], account: String, activeUrl: String) -> [Any] {
  496. var sharesLink = [String:String]()
  497. var sharesUserAndGroup = [String:String]()
  498. self.removeShareActiveAccount(account)
  499. var itemsLink = [OCSharedDto]()
  500. var itemsUsersAndGroups = [OCSharedDto]()
  501. for (_, itemOCSharedDto) in items {
  502. if (itemOCSharedDto.shareType == Int(shareTypeLink.rawValue)) {
  503. itemsLink.append(itemOCSharedDto)
  504. }
  505. if (itemOCSharedDto.shareWith.characters.count > 0 && (itemOCSharedDto.shareType == Int(shareTypeUser.rawValue) || itemOCSharedDto.shareType == Int(shareTypeGroup.rawValue) || itemOCSharedDto.shareType == Int(shareTypeRemote.rawValue) )) {
  506. itemsUsersAndGroups.append(itemOCSharedDto)
  507. }
  508. }
  509. // Manage sharesLink
  510. for itemOCSharedDto in itemsLink {
  511. let fullPath = CCUtility.getHomeServerUrlActiveUrl(activeUrl) + "\(itemOCSharedDto.path!)"
  512. let fileName = NSString(string: fullPath).lastPathComponent
  513. var serverUrl = NSString(string: fullPath).substring(to: (fullPath.characters.count - fileName.characters.count - 1))
  514. if serverUrl.hasSuffix("/") {
  515. serverUrl = NSString(string: serverUrl).substring(to: (serverUrl.characters.count - 1))
  516. }
  517. if itemOCSharedDto.idRemoteShared > 0 {
  518. let sharesLinkReturn = self.addShareLink("\(itemOCSharedDto.idRemoteShared)", fileName: fileName, serverUrl: serverUrl, account: account)
  519. for (key,value) in sharesLinkReturn {
  520. sharesLink.updateValue(value, forKey:key)
  521. }
  522. }
  523. }
  524. // Manage sharesUserAndGroup
  525. var paths = [String:[String]]()
  526. for itemOCSharedDto in itemsUsersAndGroups {
  527. if paths[itemOCSharedDto.path] != nil {
  528. var share : [String] = paths[itemOCSharedDto.path]!
  529. share.append("\(itemOCSharedDto.idRemoteShared)")
  530. paths[itemOCSharedDto.path] = share
  531. } else {
  532. paths[itemOCSharedDto.path] = ["\(itemOCSharedDto.idRemoteShared)"]
  533. }
  534. }
  535. for (path, idsRemoteSharedArray) in paths {
  536. let idsRemoteShared = idsRemoteSharedArray.joined(separator: ",")
  537. print("[LOG] share \(String(describing: idsRemoteShared))")
  538. let fullPath = CCUtility.getHomeServerUrlActiveUrl(activeUrl) + "\(path)"
  539. let fileName = NSString(string: fullPath).lastPathComponent
  540. var serverUrl = NSString(string: fullPath).substring(to: (fullPath.characters.count - fileName.characters.count - 1))
  541. if serverUrl.hasSuffix("/") {
  542. serverUrl = NSString(string: serverUrl).substring(to: (serverUrl.characters.count - 1))
  543. }
  544. let sharesUserAndGroupReturn = self.addShareUserAndGroup(idsRemoteShared, fileName: fileName, serverUrl: serverUrl, account: account)
  545. for (key,value) in sharesUserAndGroupReturn {
  546. sharesUserAndGroup.updateValue(value, forKey:key)
  547. }
  548. }
  549. return [sharesLink, sharesUserAndGroup]
  550. }
  551. func getSharesAccount(_ account: String) -> [Any] {
  552. var sharesLink = [String:String]()
  553. var sharesUserAndGroup = [String:String]()
  554. let realm = try! Realm()
  555. let results = realm.objects(tableShare.self).filter("account = %@", account)
  556. for resultShare in results {
  557. if (resultShare.shareLink.characters.count > 0) {
  558. sharesLink = [resultShare.shareLink: "\(resultShare.serverUrl)\(resultShare.fileName)"]
  559. }
  560. if (resultShare.shareUserAndGroup.characters.count > 0) {
  561. sharesUserAndGroup = [resultShare.shareUserAndGroup: "\(resultShare.serverUrl)\(resultShare.fileName)"]
  562. }
  563. }
  564. return [sharesLink, sharesUserAndGroup]
  565. }
  566. //MARK: -
  567. }