NCManageDatabase.swift 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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. addActivity.date = activity.date! as NSDate
  165. addActivity.idActivity = Double(activity.idActivity)
  166. addActivity.link = activity.link
  167. addActivity.note = activity.subject
  168. addActivity.type = k_activityTypeInfo
  169. realm.add(addActivity)
  170. }
  171. }
  172. }
  173. func addActivityClient(_ file: String, fileID: String, action: String, selector: String, note: String, type: String, verbose: Bool, account: String?, activeUrl: String?) {
  174. var noteReplacing : String = ""
  175. if (activeUrl != nil) {
  176. noteReplacing = note.replacingOccurrences(of: "\(activeUrl!)\(webDAV)", with: "")
  177. }
  178. noteReplacing = note.replacingOccurrences(of: "\(k_domain_session_queue).", with: "")
  179. let realm = try! Realm()
  180. try! realm.write {
  181. // Add new Activity
  182. let addActivity = tableActivity()
  183. if (account != nil) {
  184. addActivity.account = account!
  185. }
  186. addActivity.action = action
  187. addActivity.file = file
  188. addActivity.fileID = fileID
  189. addActivity.note = noteReplacing
  190. addActivity.selector = selector
  191. addActivity.type = type
  192. addActivity.verbose = verbose
  193. realm.add(addActivity)
  194. }
  195. }
  196. func getActivityWithPredicate(_ predicate: NSPredicate) -> [tableActivity] {
  197. let realm = try! Realm()
  198. let results = realm.objects(tableActivity.self).filter(predicate).sorted(byKeyPath: "date", ascending: false)
  199. return Array(results)
  200. }
  201. //MARK: -
  202. //MARK: Table Automatic Upload
  203. func addAutomaticUpload(_ metadataNet: CCMetadataNet, account: String) -> Bool {
  204. let realm = try! Realm()
  205. // Verify if exists
  206. let results = realm.objects(tableAutomaticUpload.self).filter("account = %@ AND assetLocalIdentifier = %@", account, metadataNet.assetLocalIdentifier)
  207. if (results.count > 0) {
  208. return false
  209. }
  210. try! realm.write {
  211. // Add new AutomaticUpload
  212. let addAutomaticUpload = tableAutomaticUpload()
  213. addAutomaticUpload.account = account
  214. addAutomaticUpload.assetLocalIdentifier = metadataNet.assetLocalIdentifier
  215. addAutomaticUpload.fileName = metadataNet.fileName
  216. addAutomaticUpload.selector = metadataNet.selector
  217. if (metadataNet.selectorPost != nil) {
  218. addAutomaticUpload.selectorPost = metadataNet.selectorPost
  219. }
  220. addAutomaticUpload.serverUrl = metadataNet.serverUrl
  221. addAutomaticUpload.session = metadataNet.session
  222. addAutomaticUpload.priority = metadataNet.priority
  223. realm.add(addAutomaticUpload)
  224. }
  225. return true
  226. }
  227. func getAutomaticUploadForAccount(_ account: String, selector: String) -> CCMetadataNet? {
  228. let realm = try! Realm()
  229. // Verify if exists
  230. let results = realm.objects(tableAutomaticUpload.self).filter("account = %@ AND selector = %@ AND lock == false", account, selector)
  231. if (results.count == 0) {
  232. return nil
  233. }
  234. let metadataNet = CCMetadataNet()
  235. metadataNet.action = actionUploadAsset
  236. metadataNet.assetLocalIdentifier = results[0].assetLocalIdentifier
  237. metadataNet.fileName = results[0].fileName
  238. metadataNet.priority = results[0].priority
  239. metadataNet.selector = results[0].selector
  240. metadataNet.selectorPost = results[0].selectorPost
  241. metadataNet.serverUrl = results[0].serverUrl
  242. metadataNet.session = results[0].session
  243. metadataNet.taskStatus = Int(k_taskStatusResume)
  244. // Lock True
  245. try! realm.write {
  246. results[0].lock = true
  247. }
  248. return metadataNet
  249. }
  250. func getLockAutomaticUploadForAccount(_ account: String) -> [tableAutomaticUpload] {
  251. let realm = try! Realm()
  252. let results = realm.objects(tableAutomaticUpload.self).filter("account = %@ AND lock = true", account)
  253. return Array(results)
  254. }
  255. func unlockAutomaticUploadForAccount(_ account: String, assetLocalIdentifier: String) {
  256. let realm = try! Realm()
  257. let results = realm.objects(tableAutomaticUpload.self).filter("account = %@ AND assetLocalIdentifier = %@", account, assetLocalIdentifier)
  258. if (results.count > 0) {
  259. // Lock False
  260. try! realm.write {
  261. results[0].lock = false
  262. }
  263. }
  264. }
  265. func deleteAutomaticUploadForAccount(_ account: String, assetLocalIdentifier: String) {
  266. let realm = try! Realm()
  267. let results = realm.objects(tableAutomaticUpload.self).filter("account = %@ AND assetLocalIdentifier = %@", account, assetLocalIdentifier)
  268. if (results.count > 0) {
  269. try! realm.write {
  270. realm.delete(results)
  271. }
  272. }
  273. }
  274. func countAutomaticUploadForAccount(_ account: String, selector: String?) -> Int {
  275. let realm = try! Realm()
  276. let results : Results<tableAutomaticUpload>
  277. if (selector == nil) {
  278. results = realm.objects(tableAutomaticUpload.self).filter("account = %@", account)
  279. } else {
  280. results = realm.objects(tableAutomaticUpload.self).filter("account = %@ AND selector = %@", account, selector!)
  281. }
  282. return results.count
  283. }
  284. //MARK: -
  285. //MARK: Table Capabilities
  286. func addCapabilities(_ capabilities: OCCapabilities, account: String) {
  287. let realm = try! Realm()
  288. let results = realm.objects(tableCapabilities.self).filter("account = %@", account)
  289. try! realm.write {
  290. var resultCapabilities = tableCapabilities()
  291. if (results.count > 0) {
  292. resultCapabilities = results[0]
  293. }
  294. resultCapabilities.account = account
  295. resultCapabilities.themingBackground = capabilities.themingBackground
  296. resultCapabilities.themingColor = capabilities.themingColor
  297. resultCapabilities.themingLogo = capabilities.themingLogo
  298. resultCapabilities.themingName = capabilities.themingName
  299. resultCapabilities.themingSlogan = capabilities.themingSlogan
  300. resultCapabilities.themingUrl = capabilities.themingUrl
  301. resultCapabilities.versionMajor = capabilities.versionMajor
  302. resultCapabilities.versionMinor = capabilities.versionMinor
  303. resultCapabilities.versionMicro = capabilities.versionMicro
  304. resultCapabilities.versionString = capabilities.versionString
  305. if (results.count == 0) {
  306. realm.add(resultCapabilities)
  307. }
  308. }
  309. }
  310. func getCapabilitesForAccount(_ account: String) -> tableCapabilities? {
  311. let realm = try! Realm()
  312. let results = realm.objects(tableCapabilities.self).filter("account = %@", account)
  313. if (results.count > 0) {
  314. return results[0]
  315. } else {
  316. return nil
  317. }
  318. }
  319. func getServerVersionAccount(_ account: String) -> Int {
  320. let realm = try! Realm()
  321. let results = realm.objects(tableCapabilities.self).filter("account = %@", account)
  322. if (results.count > 0) {
  323. return results[0].versionMajor
  324. } else {
  325. return 0
  326. }
  327. }
  328. //MARK: -
  329. //MARK: Table Certificates
  330. func addCertificates(_ certificateLocation: String) {
  331. let realm = try! Realm()
  332. try! realm.write {
  333. let addCertificates = tableCertificates()
  334. addCertificates.certificateLocation = certificateLocation
  335. realm.add(addCertificates)
  336. }
  337. }
  338. func getCertificatesLocation(_ localCertificatesFolder: String) -> [String] {
  339. let realm = try! Realm()
  340. let results = realm.objects(tableCertificates.self)
  341. var arraycertificatePath = [String]()
  342. for result in results {
  343. arraycertificatePath.append("\(localCertificatesFolder)\(result.certificateLocation)")
  344. }
  345. return arraycertificatePath
  346. }
  347. //MARK: -
  348. //MARK: Table External Sites
  349. func addExternalSites(_ externalSites: OCExternalSites, account: String) {
  350. let realm = try! Realm()
  351. try! realm.write {
  352. let addExternalSite = tableExternalSites()
  353. addExternalSite.account = account
  354. addExternalSite.idExternalSite = externalSites.idExternalSite
  355. addExternalSite.icon = externalSites.icon
  356. addExternalSite.lang = externalSites.lang
  357. addExternalSite.name = externalSites.name
  358. addExternalSite.url = externalSites.url
  359. addExternalSite.type = externalSites.type
  360. realm.add(addExternalSite)
  361. }
  362. }
  363. func deleteExternalSitesForAccount(_ account: String) {
  364. let realm = try! Realm()
  365. let results = realm.objects(tableExternalSites.self).filter("account = %@", account)
  366. try! realm.write {
  367. realm.delete(results)
  368. }
  369. }
  370. func getAllExternalSitesWithPredicate(_ predicate: NSPredicate) -> [tableExternalSites] {
  371. let realm = try! Realm()
  372. let results = realm.objects(tableExternalSites.self).filter(predicate).sorted(byKeyPath: "idExternalSite", ascending: true)
  373. return Array(results)
  374. }
  375. //MARK: -
  376. //MARK: Table GPS
  377. func addGeocoderLocation(_ location: String, placemarkAdministrativeArea: String, placemarkCountry: String, placemarkLocality: String, placemarkPostalCode: String, placemarkThoroughfare: String, latitude: String, longitude: String) {
  378. let realm = try! Realm()
  379. // Verify if exists
  380. let results = realm.objects(tableGPS.self).filter("latitude = %@ AND longitude = %@", latitude, longitude)
  381. if (results.count > 0) {
  382. return
  383. }
  384. try! realm.write {
  385. // Add new GPS
  386. let addGPS = tableGPS()
  387. addGPS.location = location
  388. addGPS.placemarkAdministrativeArea = placemarkAdministrativeArea
  389. addGPS.placemarkCountry = placemarkCountry
  390. addGPS.placemarkLocality = placemarkLocality
  391. addGPS.placemarkPostalCode = placemarkPostalCode
  392. addGPS.placemarkThoroughfare = placemarkThoroughfare
  393. addGPS.latitude = latitude
  394. addGPS.longitude = longitude
  395. realm.add(addGPS)
  396. }
  397. }
  398. func getLocationFromGeoLatitude(_ latitude: String, longitude: String) -> String? {
  399. let realm = try! Realm()
  400. let results = realm.objects(tableGPS.self).filter("latitude = %@ AND longitude = %@", latitude, longitude)
  401. if (results.count == 0) {
  402. return nil
  403. } else {
  404. return results[0].location
  405. }
  406. }
  407. //MARK: -
  408. //MARK: Table Share
  409. func addShareLink(_ share: String, fileName: String, serverUrl: String, account: String) -> [String:String] {
  410. let realm = try! Realm()
  411. // Verify if exists
  412. let results = realm.objects(tableShare.self).filter("account = %@ AND fileName = %@ AND serverUrl = %@", account, fileName, serverUrl)
  413. if (results.count > 0) {
  414. try! realm.write {
  415. results[0].shareLink = share;
  416. }
  417. } else {
  418. // Add new record
  419. try! realm.write {
  420. let addShare = tableShare()
  421. addShare.account = account
  422. addShare.fileName = fileName
  423. addShare.serverUrl = serverUrl
  424. addShare.shareLink = share
  425. realm.add(addShare)
  426. }
  427. }
  428. return ["\(serverUrl)\(fileName)" : share]
  429. }
  430. func addShareUserAndGroup(_ share: String, fileName: String, serverUrl: String, account: String) -> [String:String] {
  431. let realm = try! Realm()
  432. // Verify if exists
  433. let results = realm.objects(tableShare.self).filter("account = %@ AND fileName = %@ AND serverUrl = %@", account, fileName, serverUrl)
  434. if (results.count > 0) {
  435. try! realm.write {
  436. results[0].shareUserAndGroup = share;
  437. }
  438. } else {
  439. // Add new record
  440. try! realm.write {
  441. let addShare = tableShare()
  442. addShare.account = account
  443. addShare.fileName = fileName
  444. addShare.serverUrl = serverUrl
  445. addShare.shareUserAndGroup = share
  446. realm.add(addShare)
  447. }
  448. }
  449. return ["\(serverUrl)\(fileName)" : share]
  450. }
  451. func unShare(_ share: String, fileName: String, serverUrl: String, sharesLink: [String:String], sharesUserAndGroup: [String:String], account: String) -> [Any] {
  452. var sharesLink = sharesLink
  453. var sharesUserAndGroup = sharesUserAndGroup
  454. let realm = try! Realm()
  455. let results = realm.objects(tableShare.self).filter("account = %@ AND (shareLink CONTAINS %@ OR shareUserAndGroup CONTAINS %@)", account, share, share)
  456. if (results.count > 0) {
  457. let result = results[0]
  458. realm.beginWrite()
  459. if (result.shareLink.contains(share)) {
  460. result.shareLink = ""
  461. }
  462. if (result.shareUserAndGroup.contains(share)) {
  463. var shares : [String] = result.shareUserAndGroup.components(separatedBy: ",")
  464. if let index = shares.index(of:share) {
  465. shares.remove(at: index)
  466. }
  467. result.shareUserAndGroup = shares.joined(separator: ",")
  468. }
  469. if (result.shareLink.characters.count > 0) {
  470. sharesLink.updateValue(result.shareLink, forKey:"\(serverUrl)\(fileName)")
  471. } else {
  472. sharesLink.removeValue(forKey: "\(serverUrl)\(fileName)")
  473. }
  474. if (result.shareUserAndGroup.characters.count > 0) {
  475. sharesUserAndGroup.updateValue(result.shareUserAndGroup, forKey:"\(serverUrl)\(fileName)")
  476. } else {
  477. sharesUserAndGroup.removeValue(forKey: "\(serverUrl)\(fileName)")
  478. }
  479. if (result.shareLink.characters.count == 0 && result.shareUserAndGroup.characters.count == 0) {
  480. realm.delete(result)
  481. }
  482. try! realm.commitWrite()
  483. }
  484. return [sharesLink, sharesUserAndGroup]
  485. }
  486. func removeShareActiveAccount(_ account: String) {
  487. let realm = try! Realm()
  488. let results = realm.objects(tableShare.self).filter("account = %@", account)
  489. try! realm.write {
  490. realm.delete(results)
  491. }
  492. }
  493. func updateShare(_ items: [String:OCSharedDto], account: String, activeUrl: String) -> [Any] {
  494. var sharesLink = [String:String]()
  495. var sharesUserAndGroup = [String:String]()
  496. self.removeShareActiveAccount(account)
  497. var itemsLink = [OCSharedDto]()
  498. var itemsUsersAndGroups = [OCSharedDto]()
  499. for (_, itemOCSharedDto) in items {
  500. if (itemOCSharedDto.shareType == Int(shareTypeLink.rawValue)) {
  501. itemsLink.append(itemOCSharedDto)
  502. }
  503. if (itemOCSharedDto.shareWith.characters.count > 0 && (itemOCSharedDto.shareType == Int(shareTypeUser.rawValue) || itemOCSharedDto.shareType == Int(shareTypeGroup.rawValue) || itemOCSharedDto.shareType == Int(shareTypeRemote.rawValue) )) {
  504. itemsUsersAndGroups.append(itemOCSharedDto)
  505. }
  506. }
  507. // Manage sharesLink
  508. for itemOCSharedDto in itemsLink {
  509. let fullPath = CCUtility.getHomeServerUrlActiveUrl(activeUrl) + "\(itemOCSharedDto.path!)"
  510. let fileName = NSString(string: fullPath).lastPathComponent
  511. var serverUrl = NSString(string: fullPath).substring(to: (fullPath.characters.count - fileName.characters.count - 1))
  512. if serverUrl.hasSuffix("/") {
  513. serverUrl = NSString(string: serverUrl).substring(to: (serverUrl.characters.count - 1))
  514. }
  515. if itemOCSharedDto.idRemoteShared > 0 {
  516. let sharesLinkReturn = self.addShareLink("\(itemOCSharedDto.idRemoteShared)", fileName: fileName, serverUrl: serverUrl, account: account)
  517. for (key,value) in sharesLinkReturn {
  518. sharesLink.updateValue(value, forKey:key)
  519. }
  520. }
  521. }
  522. // Manage sharesUserAndGroup
  523. var paths = [String:[String]]()
  524. for itemOCSharedDto in itemsUsersAndGroups {
  525. if paths[itemOCSharedDto.path] != nil {
  526. var share : [String] = paths[itemOCSharedDto.path]!
  527. share.append("\(itemOCSharedDto.idRemoteShared)")
  528. paths[itemOCSharedDto.path] = share
  529. } else {
  530. paths[itemOCSharedDto.path] = ["\(itemOCSharedDto.idRemoteShared)"]
  531. }
  532. }
  533. for (path, idsRemoteSharedArray) in paths {
  534. let idsRemoteShared = idsRemoteSharedArray.joined(separator: ",")
  535. print("[LOG] share \(String(describing: idsRemoteShared))")
  536. let fullPath = CCUtility.getHomeServerUrlActiveUrl(activeUrl) + "\(path)"
  537. let fileName = NSString(string: fullPath).lastPathComponent
  538. var serverUrl = NSString(string: fullPath).substring(to: (fullPath.characters.count - fileName.characters.count - 1))
  539. if serverUrl.hasSuffix("/") {
  540. serverUrl = NSString(string: serverUrl).substring(to: (serverUrl.characters.count - 1))
  541. }
  542. let sharesUserAndGroupReturn = self.addShareUserAndGroup(idsRemoteShared, fileName: fileName, serverUrl: serverUrl, account: account)
  543. for (key,value) in sharesUserAndGroupReturn {
  544. sharesUserAndGroup.updateValue(value, forKey:key)
  545. }
  546. }
  547. return [sharesLink, sharesUserAndGroup]
  548. }
  549. func getSharesAccount(_ account: String) -> [Any] {
  550. var sharesLink = [String:String]()
  551. var sharesUserAndGroup = [String:String]()
  552. let realm = try! Realm()
  553. let results = realm.objects(tableShare.self).filter("account = %@", account)
  554. for resultShare in results {
  555. if (resultShare.shareLink.characters.count > 0) {
  556. sharesLink = [resultShare.shareLink: "\(resultShare.serverUrl)\(resultShare.fileName)"]
  557. }
  558. if (resultShare.shareUserAndGroup.characters.count > 0) {
  559. sharesUserAndGroup = [resultShare.shareUserAndGroup: "\(resultShare.serverUrl)\(resultShare.fileName)"]
  560. }
  561. }
  562. return [sharesLink, sharesUserAndGroup]
  563. }
  564. //MARK: -
  565. }