NCManageDatabase.swift 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277
  1. //
  2. // NCManageDatabase.swift
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 06/05/17.
  6. // Copyright © 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  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 RealmSwift
  24. class NCManageDatabase: NSObject {
  25. static let sharedInstance: NCManageDatabase = {
  26. let instance = NCManageDatabase()
  27. return instance
  28. }()
  29. override init() {
  30. let dirGroup = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: NCBrandOptions.sharedInstance.capabilitiesGroups)
  31. let config = Realm.Configuration(
  32. fileURL: dirGroup?.appendingPathComponent("\(appDatabaseNextcloud)/\(k_databaseDefault)"),
  33. schemaVersion: 3,
  34. migrationBlock: { migration, oldSchemaVersion in
  35. // We haven’t migrated anything yet, so oldSchemaVersion == 0
  36. if (oldSchemaVersion < 3) {
  37. // Nothing to do!
  38. // Realm will automatically detect new properties and removed properties
  39. // And will update the schema on disk automatically
  40. }
  41. })
  42. Realm.Configuration.defaultConfiguration = config
  43. _ = try! Realm()
  44. }
  45. //MARK: -
  46. //MARK: Utility Database
  47. func clearTable(_ table : Object.Type, account: String?) {
  48. let results : Results<Object>
  49. let realm = try! Realm()
  50. realm.beginWrite()
  51. if account != nil {
  52. results = realm.objects(table).filter("account = %@", account!)
  53. } else {
  54. results = realm.objects(table)
  55. }
  56. realm.delete(results)
  57. do {
  58. try realm.commitWrite()
  59. } catch let error {
  60. print("[LOG] Could not write to database: ", error)
  61. }
  62. }
  63. func removeDB() {
  64. let realmURL = Realm.Configuration.defaultConfiguration.fileURL!
  65. let realmURLs = [
  66. realmURL,
  67. realmURL.appendingPathExtension("lock"),
  68. realmURL.appendingPathExtension("note"),
  69. realmURL.appendingPathExtension("management")
  70. ]
  71. for URL in realmURLs {
  72. do {
  73. try FileManager.default.removeItem(at: URL)
  74. } catch let error {
  75. print("[LOG] Could not write to database: ", error)
  76. }
  77. }
  78. }
  79. func getThreadConfined(_ table: Object) -> Any {
  80. // id tradeReference = [[NCManageDatabase sharedInstance] getThreadConfined:metadata];
  81. return ThreadSafeReference(to: table)
  82. }
  83. func putThreadConfined(_ tableRef: Any) -> Object? {
  84. //tableMetadata *metadataThread = (tableMetadata *)[[NCManageDatabase sharedInstance] putThreadConfined:tradeReference];
  85. let realm = try! Realm()
  86. return realm.resolve(tableRef as! ThreadSafeReference<Object>)
  87. }
  88. func isTableInvalidated(_ table: Object) -> Bool {
  89. return table.isInvalidated
  90. }
  91. //MARK: -
  92. //MARK: Table Account
  93. func addAccount(_ account: String, url: String, user: String, password: String) {
  94. let realm = try! Realm()
  95. realm.beginWrite()
  96. let addAccount = tableAccount()
  97. addAccount.account = account
  98. // Brand
  99. if NCBrandOptions.sharedInstance.use_default_auto_upload {
  100. addAccount.autoUpload = true
  101. addAccount.autoUploadImage = true
  102. addAccount.autoUploadVideo = true
  103. addAccount.autoUploadWWAnVideo = true
  104. }
  105. addAccount.password = password
  106. addAccount.url = url
  107. addAccount.user = user
  108. realm.add(addAccount)
  109. do {
  110. try realm.commitWrite()
  111. } catch let error {
  112. print("[LOG] Could not write to database: ", error)
  113. }
  114. }
  115. func setAccountPassword(_ account: String, password: String) {
  116. let realm = try! Realm()
  117. realm.beginWrite()
  118. guard let result = realm.objects(tableAccount.self).filter("account = %@", account).first else {
  119. realm.cancelWrite()
  120. return
  121. }
  122. result.password = password
  123. do {
  124. try realm.commitWrite()
  125. } catch let error {
  126. print("[LOG] Could not write to database: ", error)
  127. }
  128. }
  129. func deleteAccount(_ account: String) {
  130. let realm = try! Realm()
  131. realm.beginWrite()
  132. guard let result = realm.objects(tableAccount.self).filter("account = %@", account).first else {
  133. realm.cancelWrite()
  134. return
  135. }
  136. realm.delete(result)
  137. do {
  138. try realm.commitWrite()
  139. } catch let error {
  140. print("[LOG] Could not write to database: ", error)
  141. }
  142. }
  143. func getAccountActive() -> tableAccount? {
  144. let realm = try! Realm()
  145. guard let result = realm.objects(tableAccount.self).filter("active = true").first else {
  146. return nil
  147. }
  148. return result
  149. }
  150. func getAccounts() -> [String] {
  151. let realm = try! Realm()
  152. let results : Results<tableAccount>
  153. var accounts = [String]()
  154. results = realm.objects(tableAccount.self).sorted(byKeyPath: "account", ascending: true)
  155. for result in results {
  156. accounts.append(result.account)
  157. }
  158. return Array(accounts)
  159. }
  160. func getAccountAutoUploadFileName() -> String {
  161. let realm = try! Realm()
  162. guard let result = realm.objects(tableAccount.self).filter("active = true").first else {
  163. return ""
  164. }
  165. if result.autoUploadFileName.characters.count > 0 {
  166. return result.autoUploadFileName
  167. } else {
  168. return NCBrandOptions.sharedInstance.folderDefaultAutoUpload
  169. }
  170. }
  171. func getAccountAutoUploadDirectory(_ activeUrl : String) -> String {
  172. let realm = try! Realm()
  173. guard let result = realm.objects(tableAccount.self).filter("active = true").first else {
  174. return ""
  175. }
  176. if result.autoUploadDirectory.characters.count > 0 {
  177. return result.autoUploadDirectory
  178. } else {
  179. return CCUtility.getHomeServerUrlActiveUrl(activeUrl)
  180. }
  181. }
  182. func getAccountAutoUploadPath(_ activeUrl : String) -> String {
  183. let cameraFileName = self.getAccountAutoUploadFileName()
  184. let cameraDirectory = self.getAccountAutoUploadDirectory(activeUrl)
  185. let folderPhotos = CCUtility.stringAppendServerUrl(cameraDirectory, addFileName: cameraFileName)!
  186. return folderPhotos
  187. }
  188. func setAccountActive(_ account: String) -> tableAccount {
  189. let realm = try! Realm()
  190. var activeAccount = tableAccount()
  191. do {
  192. try realm.write {
  193. let results = realm.objects(tableAccount.self)
  194. for result in results {
  195. if result.account == account {
  196. result.active = true
  197. activeAccount = result
  198. } else {
  199. result.active = false
  200. }
  201. }
  202. }
  203. } catch let error {
  204. print("Could not write to database: ", error)
  205. }
  206. return activeAccount
  207. }
  208. func setAccountAutoUploadFiled(_ field: String, state: Bool) {
  209. let realm = try! Realm()
  210. realm.beginWrite()
  211. guard let result = realm.objects(tableAccount.self).filter("active = true").first else {
  212. realm.cancelWrite()
  213. return
  214. }
  215. switch field {
  216. case "autoUpload":
  217. result.autoUpload = state
  218. case "autoUploadBackground":
  219. result.autoUploadBackground = state
  220. case "autoUploadCreateSubfolder":
  221. result.autoUploadCreateSubfolder = state
  222. case "autoUploadFull":
  223. result.autoUploadFull = state
  224. case "autoUploadImage":
  225. result.autoUploadImage = state
  226. case "autoUploadVideo":
  227. result.autoUploadVideo = state
  228. case "autoUploadWWAnPhoto":
  229. result.autoUploadWWAnPhoto = state
  230. case "autoUploadWWAnVideo":
  231. result.autoUploadWWAnVideo = state
  232. default:
  233. print("No founfd field")
  234. }
  235. do {
  236. try realm.commitWrite()
  237. } catch let error {
  238. print("[LOG] Could not write to database: ", error)
  239. }
  240. }
  241. func setAccountAutoUploadFileName(_ fileName: String?) {
  242. let realm = try! Realm()
  243. var fileName : String? = fileName
  244. if fileName == nil {
  245. fileName = self.getAccountAutoUploadFileName()
  246. }
  247. do {
  248. try realm.write {
  249. let result = realm.objects(tableAccount.self).filter("active = true").first
  250. if result != nil {
  251. result?.autoUploadFileName = fileName!
  252. }
  253. }
  254. } catch let error {
  255. print("Could not write to database: ", error)
  256. }
  257. }
  258. func setAccountAutoUploadDirectory(_ serverUrl: String?, activeUrl: String) {
  259. let realm = try! Realm()
  260. var serverUrl : String? = serverUrl
  261. if serverUrl == nil {
  262. serverUrl = self.getAccountAutoUploadDirectory(activeUrl)
  263. }
  264. do {
  265. try realm.write {
  266. let result = realm.objects(tableAccount.self).filter("active = true").first
  267. if result != nil {
  268. result?.autoUploadDirectory = serverUrl!
  269. }
  270. }
  271. } catch let error {
  272. print("Could not write to database: ", error)
  273. }
  274. }
  275. func setAccountsUserProfile(_ userProfile: OCUserProfile) {
  276. guard let tblAccount = self.getAccountActive() else {
  277. return
  278. }
  279. let realm = try! Realm()
  280. do {
  281. try realm.write {
  282. guard let result = realm.objects(tableAccount.self).filter("account = %@", tblAccount.account).first else {
  283. return
  284. }
  285. result.enabled = userProfile.enabled
  286. result.address = userProfile.address
  287. result.displayName = userProfile.displayName
  288. result.email = userProfile.email
  289. result.phone = userProfile.phone
  290. result.twitter = userProfile.twitter
  291. result.webpage = userProfile.webpage
  292. result.quota = userProfile.quota
  293. result.quotaFree = userProfile.quotaFree
  294. result.quotaRelative = userProfile.quotaRelative
  295. result.quotaTotal = userProfile.quotaTotal
  296. result.quotaUsed = userProfile.quotaUsed
  297. }
  298. } catch let error {
  299. print("Could not write to database: ", error)
  300. }
  301. }
  302. //MARK: -
  303. //MARK: Table Activity
  304. func getActivity(predicate: NSPredicate) -> [tableActivity] {
  305. let realm = try! Realm()
  306. let results = realm.objects(tableActivity.self).filter(predicate).sorted(byKeyPath: "date", ascending: false)
  307. return Array(results.map { tableActivity.init(value:$0) })
  308. }
  309. func addActivityServer(_ listOfActivity: [OCActivity]) {
  310. guard let tableAccount = self.getAccountActive() else {
  311. return
  312. }
  313. let realm = try! Realm()
  314. do {
  315. try realm.write {
  316. for activity in listOfActivity {
  317. if realm.objects(tableActivity.self).filter("idActivity = %d", activity.idActivity).first == nil {
  318. // Add new Activity
  319. let addActivity = tableActivity()
  320. addActivity.account = tableAccount.account
  321. if activity.date != nil {
  322. addActivity.date = activity.date! as NSDate
  323. }
  324. addActivity.idActivity = Double(activity.idActivity)
  325. addActivity.link = activity.link
  326. addActivity.note = activity.subject
  327. addActivity.type = k_activityTypeInfo
  328. realm.add(addActivity)
  329. }
  330. }
  331. }
  332. } catch let error {
  333. print("[LOG] Could not write to database: ", error)
  334. }
  335. }
  336. func addActivityClient(_ file: String, fileID: String, action: String, selector: String, note: String, type: String, verbose: Bool, activeUrl: String?) {
  337. guard let tableAccount = self.getAccountActive() else {
  338. return
  339. }
  340. var noteReplacing : String = ""
  341. if (activeUrl != nil) {
  342. noteReplacing = note.replacingOccurrences(of: "\(activeUrl!)\(webDAV)", with: "")
  343. }
  344. noteReplacing = note.replacingOccurrences(of: "\(k_domain_session_queue).", with: "")
  345. let realm = try! Realm()
  346. if realm.isInWriteTransaction {
  347. print("[LOG] Could not write to database, addActivityClient is already in write transaction")
  348. } else {
  349. do {
  350. try realm.write {
  351. // Add new Activity
  352. let addActivity = tableActivity()
  353. addActivity.account = tableAccount.account
  354. addActivity.action = action
  355. addActivity.file = file
  356. addActivity.fileID = fileID
  357. addActivity.note = noteReplacing
  358. addActivity.selector = selector
  359. addActivity.type = type
  360. addActivity.verbose = verbose
  361. realm.add(addActivity)
  362. }
  363. } catch let error {
  364. print("[LOG] Could not write to database: ", error)
  365. }
  366. }
  367. }
  368. //MARK: -
  369. //MARK: Table Auto Upload
  370. func addAutoUpload(metadataNet: CCMetadataNet) -> Bool {
  371. guard let tableAccount = self.getAccountActive() else {
  372. return false
  373. }
  374. let realm = try! Realm()
  375. realm.beginWrite()
  376. if realm.objects(tableAutoUpload.self).filter("account = %@ AND assetLocalIdentifier = %@", tableAccount.account, metadataNet.assetLocalIdentifier).first == nil {
  377. // Add new Auto Upload
  378. let addAutoUpload = tableAutoUpload()
  379. addAutoUpload.account = tableAccount.account
  380. addAutoUpload.assetLocalIdentifier = metadataNet.assetLocalIdentifier
  381. addAutoUpload.fileName = metadataNet.fileName
  382. addAutoUpload.selector = metadataNet.selector
  383. if (metadataNet.selectorPost != nil) {
  384. addAutoUpload.selectorPost = metadataNet.selectorPost
  385. }
  386. addAutoUpload.serverUrl = metadataNet.serverUrl
  387. addAutoUpload.session = metadataNet.session
  388. addAutoUpload.priority = metadataNet.priority
  389. realm.add(addAutoUpload)
  390. do {
  391. try realm.commitWrite()
  392. } catch let error {
  393. print("[LOG] Could not write to database: ", error)
  394. return false
  395. }
  396. }
  397. return true
  398. }
  399. func addAutoUpload(metadatasNet: [CCMetadataNet]) {
  400. guard let tableAccount = self.getAccountActive() else {
  401. return
  402. }
  403. let realm = try! Realm()
  404. do {
  405. try realm.write {
  406. for metadataNet in metadatasNet {
  407. if realm.objects(tableAutoUpload.self).filter("account = %@ AND assetLocalIdentifier = %@", tableAccount.account, metadataNet.assetLocalIdentifier).first == nil {
  408. // Add new Auto Upload
  409. let addAutoUpload = tableAutoUpload()
  410. addAutoUpload.account = tableAccount.account
  411. addAutoUpload.assetLocalIdentifier = metadataNet.assetLocalIdentifier
  412. addAutoUpload.fileName = metadataNet.fileName
  413. addAutoUpload.selector = metadataNet.selector
  414. if (metadataNet.selectorPost != nil) {
  415. addAutoUpload.selectorPost = metadataNet.selectorPost
  416. }
  417. addAutoUpload.serverUrl = metadataNet.serverUrl
  418. addAutoUpload.session = metadataNet.session
  419. addAutoUpload.priority = metadataNet.priority
  420. realm.add(addAutoUpload)
  421. }
  422. }
  423. }
  424. } catch let error {
  425. print("[LOG] Could not write to database: ", error)
  426. }
  427. }
  428. func getAutoUpload(selector: String) -> CCMetadataNet? {
  429. guard let tableAccount = self.getAccountActive() else {
  430. return nil
  431. }
  432. let realm = try! Realm()
  433. realm.beginWrite()
  434. guard let result = realm.objects(tableAutoUpload.self).filter("account = %@ AND selector = %@ AND lock == false", tableAccount.account, selector).first else {
  435. realm.cancelWrite()
  436. return nil
  437. }
  438. let metadataNet = CCMetadataNet()
  439. metadataNet.action = actionUploadAsset
  440. metadataNet.assetLocalIdentifier = result.assetLocalIdentifier
  441. metadataNet.fileName = result.fileName
  442. metadataNet.priority = result.priority
  443. metadataNet.selector = result.selector
  444. metadataNet.selectorPost = result.selectorPost
  445. metadataNet.serverUrl = result.serverUrl
  446. metadataNet.session = result.session
  447. metadataNet.taskStatus = Int(k_taskStatusResume)
  448. // Lock
  449. result.lock = true
  450. do {
  451. try realm.commitWrite()
  452. } catch let error {
  453. print("[LOG] Could not write to database: ", error)
  454. return nil
  455. }
  456. return metadataNet
  457. }
  458. func getLockAutoUpload() -> [tableAutoUpload]? {
  459. guard let tableAccount = self.getAccountActive() else {
  460. return nil
  461. }
  462. let realm = try! Realm()
  463. let results = realm.objects(tableAutoUpload.self).filter("account = %@ AND lock = true", tableAccount.account)
  464. return Array(results.map { tableAutoUpload.init(value:$0) })
  465. }
  466. func unlockAutoUpload(assetLocalIdentifier: String) {
  467. guard let tableAccount = self.getAccountActive() else {
  468. return
  469. }
  470. let realm = try! Realm()
  471. realm.beginWrite()
  472. guard let result = realm.objects(tableAutoUpload.self).filter("account = %@ AND assetLocalIdentifier = %@", tableAccount.account, assetLocalIdentifier).first else {
  473. realm.cancelWrite()
  474. return
  475. }
  476. // UnLock
  477. result.lock = false
  478. do {
  479. try realm.commitWrite()
  480. } catch let error {
  481. print("[LOG] Could not write to database: ", error)
  482. }
  483. }
  484. func deleteAutoUpload(assetLocalIdentifier: String) {
  485. guard let tableAccount = self.getAccountActive() else {
  486. return
  487. }
  488. let realm = try! Realm()
  489. do {
  490. try realm.write {
  491. let result = realm.objects(tableAutoUpload.self).filter("account = %@ AND assetLocalIdentifier = %@", tableAccount.account, assetLocalIdentifier).first
  492. if result != nil {
  493. realm.delete(result!)
  494. }
  495. }
  496. } catch let error {
  497. print("Could not write to database: ", error)
  498. }
  499. }
  500. func countAutoUpload(session: String?) -> Int {
  501. guard let tableAccount = self.getAccountActive() else {
  502. return 0
  503. }
  504. let realm = try! Realm()
  505. let results : Results<tableAutoUpload>
  506. if (session == nil) {
  507. results = realm.objects(tableAutoUpload.self).filter("account = %@", tableAccount.account)
  508. } else {
  509. results = realm.objects(tableAutoUpload.self).filter("account = %@ AND session = %@", tableAccount.account, session!)
  510. }
  511. return results.count
  512. }
  513. //MARK: -
  514. //MARK: Table Capabilities
  515. func addCapabilities(_ capabilities: OCCapabilities) {
  516. guard let tableAccount = self.getAccountActive() else {
  517. return
  518. }
  519. let realm = try! Realm()
  520. do {
  521. try realm.write {
  522. let result = realm.objects(tableCapabilities.self).filter("account = %@", tableAccount.account).first
  523. var resultCapabilities = tableCapabilities()
  524. if result != nil {
  525. resultCapabilities = result!
  526. }
  527. resultCapabilities.account = tableAccount.account
  528. resultCapabilities.themingBackground = capabilities.themingBackground
  529. resultCapabilities.themingColor = capabilities.themingColor
  530. resultCapabilities.themingLogo = capabilities.themingLogo
  531. resultCapabilities.themingName = capabilities.themingName
  532. resultCapabilities.themingSlogan = capabilities.themingSlogan
  533. resultCapabilities.themingUrl = capabilities.themingUrl
  534. resultCapabilities.versionMajor = capabilities.versionMajor
  535. resultCapabilities.versionMinor = capabilities.versionMinor
  536. resultCapabilities.versionMicro = capabilities.versionMicro
  537. resultCapabilities.versionString = capabilities.versionString
  538. if result == nil {
  539. realm.add(resultCapabilities)
  540. }
  541. }
  542. } catch let error {
  543. print("Could not write to database: ", error)
  544. }
  545. }
  546. func getCapabilites() -> tableCapabilities? {
  547. guard let tableAccount = self.getAccountActive() else {
  548. return nil
  549. }
  550. let realm = try! Realm()
  551. return realm.objects(tableCapabilities.self).filter("account = %@", tableAccount.account).first
  552. }
  553. func getServerVersion() -> Int {
  554. guard let tableAccount = self.getAccountActive() else {
  555. return 0
  556. }
  557. let realm = try! Realm()
  558. guard let result = realm.objects(tableCapabilities.self).filter("account = %@", tableAccount.account).first else {
  559. return 0
  560. }
  561. return result.versionMajor
  562. }
  563. //MARK: -
  564. //MARK: Table Certificates
  565. func addCertificates(_ certificateLocation: String) {
  566. let realm = try! Realm()
  567. do {
  568. try realm.write {
  569. let addCertificates = tableCertificates()
  570. addCertificates.certificateLocation = certificateLocation
  571. realm.add(addCertificates)
  572. }
  573. } catch let error {
  574. print("Could not write to database: ", error)
  575. }
  576. }
  577. func getCertificatesLocation(_ localCertificatesFolder: String) -> [String] {
  578. let realm = try! Realm()
  579. let results = realm.objects(tableCertificates.self)
  580. var arraycertificatePath = [String]()
  581. for result in results {
  582. arraycertificatePath.append("\(localCertificatesFolder)\(result.certificateLocation)")
  583. }
  584. return arraycertificatePath
  585. }
  586. //MARK: -
  587. //MARK: Table Directory
  588. func addDirectory(serverUrl: String, permissions: String) -> String {
  589. guard let tableAccount = self.getAccountActive() else {
  590. return ""
  591. }
  592. let realm = try! Realm()
  593. var directoryID: String = ""
  594. do {
  595. try realm.write {
  596. let result = realm.objects(tableDirectory.self).filter("account = %@ AND serverUrl = %@", tableAccount.account, serverUrl).first
  597. if result == nil || (result?.isInvalidated)! {
  598. let addDirectory = tableDirectory()
  599. addDirectory.account = tableAccount.account
  600. directoryID = NSUUID().uuidString
  601. addDirectory.directoryID = directoryID
  602. addDirectory.permissions = permissions
  603. addDirectory.serverUrl = serverUrl
  604. realm.add(addDirectory, update: true)
  605. } else {
  606. result?.permissions = permissions
  607. directoryID = result!.directoryID
  608. realm.add(result!, update: true)
  609. }
  610. }
  611. } catch let error {
  612. print("Could not write to database: ", error)
  613. }
  614. return directoryID
  615. }
  616. func deleteDirectoryAndSubDirectory(serverUrl: String) {
  617. guard let tableAccount = self.getAccountActive() else {
  618. return
  619. }
  620. let realm = try! Realm()
  621. let results = realm.objects(tableDirectory.self).filter("account = %@ AND serverUrl BEGINSWITH %@", tableAccount.account, serverUrl)
  622. for result in results {
  623. // delete metadata
  624. self.deleteMetadata(predicate: NSPredicate(format: "directoryID = %@", result.directoryID), clearDateReadDirectoryID: result.directoryID)
  625. // delete local file
  626. self.deleteLocalFile(predicate: NSPredicate(format: "fileID = %@", result.fileID))
  627. /*
  628. // remove if in session
  629. if ([recordMetadata.session length] >0) {
  630. if (recordMetadata.sessionTaskIdentifier >= 0)
  631. [[CCNetworking sharedNetworking] settingSession:recordMetadata.session sessionTaskIdentifier:[recordMetadata.sessionTaskIdentifier integerValue] taskStatus: k_taskStatusCancel];
  632. if (recordMetadata.sessionTaskIdentifierPlist >= 0)
  633. [[CCNetworking sharedNetworking] settingSession:recordMetadata.session sessionTaskIdentifier:[recordMetadata.sessionTaskIdentifierPlist integerValue] taskStatus: k_taskStatusCancel];
  634. }
  635. */
  636. }
  637. // Delete table Dirrectory
  638. do {
  639. try realm.write {
  640. realm.delete(results)
  641. }
  642. } catch let error {
  643. print("Could not write to database: ", error)
  644. }
  645. }
  646. func setDirectory(serverUrl: String, serverUrlTo: String?, etag: String?) {
  647. guard let tableAccount = self.getAccountActive() else {
  648. return
  649. }
  650. let realm = try! Realm()
  651. do {
  652. try realm.write {
  653. guard let result = realm.objects(tableDirectory.self).filter("account = %@ AND serverUrl = %@", tableAccount.account, serverUrl).first else {
  654. return
  655. }
  656. if serverUrlTo != nil {
  657. result.serverUrl = serverUrlTo!
  658. }
  659. if etag != nil {
  660. result.etag = etag!
  661. }
  662. }
  663. } catch let error {
  664. print("Could not write to database: ", error)
  665. }
  666. }
  667. func clearDateRead(serverUrl: String?, directoryID: String?) {
  668. guard let tableAccount = self.getAccountActive() else {
  669. return
  670. }
  671. let realm = try! Realm()
  672. do {
  673. try realm.write {
  674. var predicate = NSPredicate()
  675. if serverUrl != nil {
  676. predicate = NSPredicate(format: "account = %@ AND serverUrl = %@", tableAccount.account, serverUrl!)
  677. }
  678. if directoryID != nil {
  679. predicate = NSPredicate(format: "account = %@ AND directoryID = %@", tableAccount.account, directoryID!)
  680. }
  681. guard let result = realm.objects(tableDirectory.self).filter(predicate).first else {
  682. return
  683. }
  684. result.dateReadDirectory = nil
  685. result.etag = ""
  686. realm.add(result, update: true)
  687. }
  688. } catch let error {
  689. print("Could not write to database: ", error)
  690. }
  691. }
  692. func getTableDirectory(predicate: NSPredicate) -> tableDirectory? {
  693. guard self.getAccountActive() != nil else {
  694. return nil
  695. }
  696. let realm = try! Realm()
  697. guard let result = realm.objects(tableDirectory.self).filter(predicate).first else {
  698. return nil
  699. }
  700. return tableDirectory.init(value: result)
  701. }
  702. func getTablesDirectory(predicate: NSPredicate, sorted: String?, ascending: Bool) -> [tableDirectory]? {
  703. guard self.getAccountActive() != nil else {
  704. return nil
  705. }
  706. let realm = try! Realm()
  707. let results : Results<tableDirectory>
  708. if sorted == nil {
  709. results = realm.objects(tableDirectory.self).filter(predicate)
  710. } else {
  711. results = realm.objects(tableDirectory.self).filter(predicate).sorted(byKeyPath: sorted!, ascending: ascending)
  712. }
  713. if (results.count > 0) {
  714. return Array(results.map { tableDirectory.init(value:$0) })
  715. } else {
  716. return nil
  717. }
  718. }
  719. func getDirectoryID(_ serverUrl: String) -> String {
  720. guard let tableAccount = self.getAccountActive() else {
  721. return ""
  722. }
  723. let realm = try! Realm()
  724. guard let result = realm.objects(tableDirectory.self).filter("account = %@ AND serverUrl = %@", tableAccount.account,serverUrl).first else {
  725. return self.addDirectory(serverUrl: serverUrl, permissions: "")
  726. }
  727. return result.directoryID
  728. }
  729. func getServerUrl(_ directoryID: String) -> String {
  730. guard let tableAccount = self.getAccountActive() else {
  731. return ""
  732. }
  733. let realm = try! Realm()
  734. guard let result = realm.objects(tableDirectory.self).filter("account = %@ AND directoryID = %@", tableAccount.account, directoryID).first else {
  735. return ""
  736. }
  737. return result.serverUrl
  738. }
  739. func setDateReadDirectory(directoryID: String) {
  740. guard let tableAccount = self.getAccountActive() else {
  741. return
  742. }
  743. let realm = try! Realm()
  744. realm.beginWrite()
  745. guard let result = realm.objects(tableDirectory.self).filter("account = %@ AND directoryID = %@", tableAccount.account, directoryID).first else {
  746. realm.cancelWrite()
  747. return
  748. }
  749. result.dateReadDirectory = NSDate()
  750. do {
  751. try realm.commitWrite()
  752. } catch let error {
  753. print("[LOG] Could not write to database: ", error)
  754. }
  755. }
  756. func setClearAllDateReadDirectory() {
  757. guard self.getAccountActive() != nil else {
  758. return
  759. }
  760. let realm = try! Realm()
  761. do {
  762. try realm.write {
  763. let results = realm.objects(tableDirectory.self)
  764. for result in results {
  765. result.dateReadDirectory = nil;
  766. result.etag = ""
  767. }
  768. }
  769. } catch let error {
  770. print("Could not write to database: ", error)
  771. }
  772. }
  773. func setDirectoryLock(serverUrl: String, lock: Bool) -> Bool {
  774. guard let tableAccount = self.getAccountActive() else {
  775. return false
  776. }
  777. let realm = try! Realm()
  778. var update = false
  779. do {
  780. try realm.write {
  781. guard let result = realm.objects(tableDirectory.self).filter("account = %@ AND serverUrl = %@", tableAccount.account, serverUrl).first else {
  782. return
  783. }
  784. result.lock = lock
  785. update = true
  786. }
  787. } catch let error {
  788. print("Could not write to database: ", error)
  789. return false
  790. }
  791. return update
  792. }
  793. func setAllDirectoryUnLock() {
  794. guard let tableAccount = self.getAccountActive() else {
  795. return
  796. }
  797. let realm = try! Realm()
  798. do {
  799. try realm.write {
  800. let results = realm.objects(tableDirectory.self).filter("account = %@", tableAccount.account)
  801. for result in results {
  802. result.lock = false;
  803. }
  804. }
  805. } catch let error {
  806. print("Could not write to database: ", error)
  807. }
  808. }
  809. //MARK: -
  810. //MARK: Table External Sites
  811. func addExternalSites(_ externalSites: OCExternalSites) {
  812. guard let tableAccount = self.getAccountActive() else {
  813. return
  814. }
  815. let realm = try! Realm()
  816. do {
  817. try realm.write {
  818. let addExternalSite = tableExternalSites()
  819. addExternalSite.account = tableAccount.account
  820. addExternalSite.idExternalSite = externalSites.idExternalSite
  821. addExternalSite.icon = externalSites.icon
  822. addExternalSite.lang = externalSites.lang
  823. addExternalSite.name = externalSites.name
  824. addExternalSite.url = externalSites.url
  825. addExternalSite.type = externalSites.type
  826. realm.add(addExternalSite)
  827. }
  828. } catch let error {
  829. print("Could not write to database: ", error)
  830. }
  831. }
  832. func deleteExternalSites() {
  833. guard let tableAccount = self.getAccountActive() else {
  834. return
  835. }
  836. let realm = try! Realm()
  837. do {
  838. try realm.write {
  839. let results = realm.objects(tableExternalSites.self).filter("account = %@", tableAccount.account)
  840. realm.delete(results)
  841. }
  842. } catch let error {
  843. print("Could not write to database: ", error)
  844. }
  845. }
  846. func getAllExternalSites(predicate: NSPredicate) -> [tableExternalSites] {
  847. let realm = try! Realm()
  848. let results = realm.objects(tableExternalSites.self).filter(predicate).sorted(byKeyPath: "idExternalSite", ascending: true)
  849. return Array(results)
  850. }
  851. //MARK: -
  852. //MARK: Table GPS
  853. func addGeocoderLocation(_ location: String, placemarkAdministrativeArea: String, placemarkCountry: String, placemarkLocality: String, placemarkPostalCode: String, placemarkThoroughfare: String, latitude: String, longitude: String) {
  854. let realm = try! Realm()
  855. realm.beginWrite()
  856. // Verify if exists
  857. guard realm.objects(tableGPS.self).filter("latitude = %@ AND longitude = %@", latitude, longitude).first == nil else {
  858. realm.cancelWrite()
  859. return
  860. }
  861. // Add new GPS
  862. let addGPS = tableGPS()
  863. addGPS.latitude = latitude
  864. addGPS.location = location
  865. addGPS.longitude = longitude
  866. addGPS.placemarkAdministrativeArea = placemarkAdministrativeArea
  867. addGPS.placemarkCountry = placemarkCountry
  868. addGPS.placemarkLocality = placemarkLocality
  869. addGPS.placemarkPostalCode = placemarkPostalCode
  870. addGPS.placemarkThoroughfare = placemarkThoroughfare
  871. realm.add(addGPS)
  872. do {
  873. try realm.commitWrite()
  874. } catch let error {
  875. print("[LOG] Could not write to database: ", error)
  876. }
  877. }
  878. func getLocationFromGeoLatitude(_ latitude: String, longitude: String) -> String? {
  879. let realm = try! Realm()
  880. let result = realm.objects(tableGPS.self).filter("latitude = %@ AND longitude = %@", latitude, longitude).first
  881. if result == nil {
  882. return nil
  883. } else {
  884. return result!.location
  885. }
  886. }
  887. //MARK: -
  888. //MARK: Table LocalFile
  889. func addLocalFile(metadata: tableMetadata) {
  890. guard let tableAccount = self.getAccountActive() else {
  891. return
  892. }
  893. let realm = try! Realm()
  894. do {
  895. try realm.write {
  896. let addLocaFile = tableLocalFile()
  897. addLocaFile.account = tableAccount.account
  898. addLocaFile.date = metadata.date
  899. addLocaFile.etag = metadata.etag
  900. addLocaFile.exifDate = NSDate()
  901. addLocaFile.exifLatitude = "-1"
  902. addLocaFile.exifLongitude = "-1"
  903. addLocaFile.fileID = metadata.fileID
  904. addLocaFile.fileName = metadata.fileName
  905. addLocaFile.fileNamePrint = metadata.fileNamePrint
  906. addLocaFile.size = metadata.size
  907. realm.add(addLocaFile, update: true)
  908. }
  909. } catch let error {
  910. print("Could not write to database: ", error)
  911. }
  912. }
  913. func deleteLocalFile(predicate: NSPredicate) {
  914. guard self.getAccountActive() != nil else {
  915. return
  916. }
  917. let realm = try! Realm()
  918. do {
  919. try realm.write {
  920. let results = realm.objects(tableLocalFile.self).filter(predicate)
  921. realm.delete(results)
  922. }
  923. } catch let error {
  924. print("Could not write to database: ", error)
  925. }
  926. }
  927. func setLocalFile(fileID: String, date: NSDate?, exifDate: NSDate?, exifLatitude: String?, exifLongitude: String?, fileName: String?, fileNamePrint: String?) {
  928. guard self.getAccountActive() != nil else {
  929. return
  930. }
  931. let realm = try! Realm()
  932. do {
  933. try realm.write {
  934. let result = realm.objects(tableLocalFile.self).filter("fileID = %@", fileID).first
  935. if result != nil {
  936. if date != nil {
  937. result?.date = date!
  938. }
  939. if exifDate != nil {
  940. result?.exifDate = exifDate!
  941. }
  942. if exifLatitude != nil {
  943. result?.exifLatitude = exifLatitude!
  944. }
  945. if exifLongitude != nil {
  946. result?.exifLongitude = exifLongitude!
  947. }
  948. if fileName != nil {
  949. result?.fileName = fileName!
  950. }
  951. if fileNamePrint != nil {
  952. result?.fileNamePrint = fileNamePrint!
  953. }
  954. }
  955. }
  956. } catch let error {
  957. print("Could not write to database: ", error)
  958. }
  959. }
  960. func getTableLocalFile(predicate: NSPredicate) -> tableLocalFile? {
  961. guard self.getAccountActive() != nil else {
  962. return nil
  963. }
  964. let realm = try! Realm()
  965. let result = realm.objects(tableLocalFile.self).filter(predicate).first
  966. if result != nil {
  967. return tableLocalFile.init(value: result!)
  968. } else {
  969. return nil
  970. }
  971. }
  972. //MARK: -
  973. //MARK: Table Metadata
  974. func addMetadata(_ metadata: tableMetadata, activeUrl: String, serverUrl: String) -> tableMetadata? {
  975. guard self.getAccountActive() != nil else {
  976. return nil
  977. }
  978. let autoUploadFileName = self.getAccountAutoUploadFileName()
  979. let autoUploadDirectory = self.getAccountAutoUploadDirectory(activeUrl)
  980. let realm = try! Realm()
  981. realm.beginWrite()
  982. if (metadata.realm == nil) {
  983. let metadataWithIcon = CCUtility.insertTypeFileIconName(metadata, serverUrl: serverUrl, autoUploadFileName: autoUploadFileName, autoUploadDirectory: autoUploadDirectory)
  984. realm.add(metadataWithIcon!, update: true)
  985. } else {
  986. realm.add(metadata, update: true)
  987. }
  988. let metadataCopy = tableMetadata.init(value: metadata)
  989. do {
  990. try realm.commitWrite()
  991. } catch let error {
  992. print("[LOG] Could not write to database: ", error)
  993. return nil
  994. }
  995. self.setDateReadDirectory(directoryID: metadataCopy.directoryID)
  996. return metadataCopy
  997. }
  998. func addMetadatas(_ metadatas: [tableMetadata], activeUrl: String, serverUrl: String) -> [tableMetadata] {
  999. let autoUploadFileName = self.getAccountAutoUploadFileName()
  1000. let autoUploadDirectory = self.getAccountAutoUploadDirectory(activeUrl)
  1001. var arrayMetadatas = [tableMetadata]()
  1002. let realm = try! Realm()
  1003. do {
  1004. try realm.write {
  1005. for metadata in metadatas {
  1006. if metadata.realm == nil {
  1007. let metadataWithIcon = CCUtility.insertTypeFileIconName(metadata, serverUrl: serverUrl, autoUploadFileName: autoUploadFileName, autoUploadDirectory: autoUploadDirectory)
  1008. realm.add(metadataWithIcon!, update: true)
  1009. } else {
  1010. realm.add(metadata, update: true)
  1011. }
  1012. arrayMetadatas.append(tableMetadata.init(value: metadata))
  1013. }
  1014. }
  1015. } catch let error {
  1016. print("Could not write to database: ", error)
  1017. }
  1018. let directoryID = self.getDirectoryID(serverUrl)
  1019. self.setDateReadDirectory(directoryID: directoryID)
  1020. return arrayMetadatas
  1021. }
  1022. func deleteMetadata(predicate: NSPredicate, clearDateReadDirectoryID: String?) {
  1023. guard self.getAccountActive() != nil else {
  1024. return
  1025. }
  1026. var directoriesID = [String]()
  1027. let realm = try! Realm()
  1028. realm.beginWrite()
  1029. let results = realm.objects(tableMetadata.self).filter(predicate)
  1030. if clearDateReadDirectoryID == nil {
  1031. for result in results {
  1032. directoriesID.append(result.directoryID)
  1033. }
  1034. } else {
  1035. directoriesID.append(clearDateReadDirectoryID!)
  1036. }
  1037. realm.delete(results)
  1038. do {
  1039. try realm.commitWrite()
  1040. } catch let error {
  1041. print("[LOG] Could not write to database: ", error)
  1042. }
  1043. for directoryID in directoriesID {
  1044. self.setDateReadDirectory(directoryID: directoryID)
  1045. }
  1046. }
  1047. func moveMetadata(fileName: String, directoryID: String, directoryIDTo: String) {
  1048. guard let tableAccount = self.getAccountActive() else {
  1049. return
  1050. }
  1051. let realm = try! Realm()
  1052. do {
  1053. try realm.write {
  1054. let results = realm.objects(tableMetadata.self).filter("account = %@ AND fileName = %@ AND directoryID = %@", tableAccount.account, fileName, directoryID)
  1055. for result in results {
  1056. result.directoryID = directoryIDTo
  1057. }
  1058. }
  1059. } catch let error {
  1060. print("Could not write to database: ", error)
  1061. }
  1062. self.setDateReadDirectory(directoryID: directoryID)
  1063. self.setDateReadDirectory(directoryID: directoryIDTo)
  1064. }
  1065. func updateMetadata(_ metadata: tableMetadata, activeUrl: String) -> tableMetadata? {
  1066. let autoUploadFileName = self.getAccountAutoUploadFileName()
  1067. let autoUploadDirectory = self.getAccountAutoUploadDirectory(activeUrl)
  1068. let serverUrl = self.getServerUrl(metadata.directoryID)
  1069. let directoryID = metadata.directoryID
  1070. let metadataWithIcon = CCUtility.insertTypeFileIconName(metadata, serverUrl: serverUrl, autoUploadFileName: autoUploadFileName, autoUploadDirectory: autoUploadDirectory)
  1071. let realm = try! Realm()
  1072. do {
  1073. try realm.write {
  1074. realm.add(metadataWithIcon!, update: true)
  1075. }
  1076. } catch let error {
  1077. print("Could not write to database: ", error)
  1078. return nil
  1079. }
  1080. self.setDateReadDirectory(directoryID: directoryID)
  1081. return tableMetadata.init(value: metadata)
  1082. }
  1083. func setMetadataSession(_ session: String?, sessionError: String?, sessionSelector: String?, sessionSelectorPost: String?, sessionTaskIdentifier: Int, sessionTaskIdentifierPlist: Int, predicate: NSPredicate) {
  1084. guard self.getAccountActive() != nil else {
  1085. return
  1086. }
  1087. var directoryID : String? = nil
  1088. let realm = try! Realm()
  1089. realm.beginWrite()
  1090. guard let result = realm.objects(tableMetadata.self).filter(predicate).first else {
  1091. realm.cancelWrite()
  1092. return
  1093. }
  1094. if session != nil {
  1095. result.session = session!
  1096. }
  1097. if sessionError != nil {
  1098. result.sessionError = sessionError!
  1099. }
  1100. if sessionSelector != nil {
  1101. result.sessionSelector = sessionSelector!
  1102. }
  1103. if sessionSelectorPost != nil {
  1104. result.sessionSelectorPost = sessionSelectorPost!
  1105. }
  1106. if sessionTaskIdentifier != Int(k_taskIdentifierNULL) {
  1107. result.sessionTaskIdentifier = sessionTaskIdentifier
  1108. }
  1109. if sessionTaskIdentifierPlist != Int(k_taskIdentifierNULL) {
  1110. result.sessionTaskIdentifierPlist = sessionTaskIdentifierPlist
  1111. }
  1112. directoryID = result.directoryID
  1113. do {
  1114. try realm.commitWrite()
  1115. } catch let error {
  1116. print("[LOG] Could not write to database: ", error)
  1117. }
  1118. if directoryID != nil {
  1119. // Update Date Read Directory
  1120. self.setDateReadDirectory(directoryID: directoryID!)
  1121. }
  1122. }
  1123. func setMetadataFavorite(fileID: String, favorite: Bool) {
  1124. guard let tableAccount = self.getAccountActive() else {
  1125. return
  1126. }
  1127. var directoryID: String? = nil
  1128. let realm = try! Realm()
  1129. realm.beginWrite()
  1130. guard let result = realm.objects(tableMetadata.self).filter("account = %@ AND fileID = %@", tableAccount.account, fileID).first else {
  1131. realm.cancelWrite()
  1132. return
  1133. }
  1134. result.favorite = favorite
  1135. directoryID = result.directoryID
  1136. do {
  1137. try realm.commitWrite()
  1138. } catch let error {
  1139. print("[LOG] Could not write to database: ", error)
  1140. }
  1141. if directoryID != nil {
  1142. // Update Date Read Directory
  1143. self.setDateReadDirectory(directoryID: directoryID!)
  1144. }
  1145. }
  1146. func setMetadataStatus(fileID: String, status: Double) {
  1147. guard let tableAccount = self.getAccountActive() else {
  1148. return
  1149. }
  1150. var directoryID: String? = nil
  1151. let realm = try! Realm()
  1152. realm.beginWrite()
  1153. guard let result = realm.objects(tableMetadata.self).filter("account = %@ AND fileID = %@", tableAccount.account, fileID).first else {
  1154. realm.cancelWrite()
  1155. return
  1156. }
  1157. result.status = status
  1158. directoryID = result.directoryID
  1159. do {
  1160. try realm.commitWrite()
  1161. } catch let error {
  1162. print("[LOG] Could not write to database: ", error)
  1163. }
  1164. if directoryID != nil {
  1165. // Update Date Read Directory
  1166. self.setDateReadDirectory(directoryID: directoryID!)
  1167. }
  1168. }
  1169. func getMetadata(predicate: NSPredicate) -> tableMetadata? {
  1170. guard self.getAccountActive() != nil else {
  1171. return nil
  1172. }
  1173. let realm = try! Realm()
  1174. let result = realm.objects(tableMetadata.self).filter(predicate).first
  1175. if result != nil {
  1176. return tableMetadata.init(value: result!)
  1177. } else {
  1178. return nil
  1179. }
  1180. }
  1181. func getMetadatas(predicate: NSPredicate, sorted: String?, ascending: Bool) -> [tableMetadata]? {
  1182. guard self.getAccountActive() != nil else {
  1183. return nil
  1184. }
  1185. let realm = try! Realm()
  1186. let results : Results<tableMetadata>
  1187. if sorted == nil {
  1188. results = realm.objects(tableMetadata.self).filter(predicate)
  1189. } else {
  1190. var sorted: String = sorted!
  1191. if sorted == "fileName" {
  1192. sorted = "fileNamePrint"
  1193. }
  1194. results = realm.objects(tableMetadata.self).filter(predicate).sorted(byKeyPath: sorted, ascending: ascending)
  1195. }
  1196. if (results.count > 0) {
  1197. return Array(results.map { tableMetadata.init(value:$0) })
  1198. } else {
  1199. return nil
  1200. }
  1201. }
  1202. func getMetadataAtIndex(predicate: NSPredicate, sorted: String?, ascending: Bool, index: Int) -> tableMetadata? {
  1203. guard self.getAccountActive() != nil else {
  1204. return nil
  1205. }
  1206. let realm = try! Realm()
  1207. let results = realm.objects(tableMetadata.self).filter(predicate).sorted(byKeyPath: sorted!, ascending: ascending)
  1208. if (results.count > 0 && results.count > index) {
  1209. return tableMetadata.init(value: results[index])
  1210. } else {
  1211. return nil
  1212. }
  1213. }
  1214. func getMetadataFromFileName(_ fileName: String, directoryID: String) -> tableMetadata? {
  1215. guard let tableAccount = self.getAccountActive() else {
  1216. return nil
  1217. }
  1218. let realm = try! Realm()
  1219. guard let result = realm.objects(tableMetadata.self).filter("account = %@ AND directoryID = %@ AND (fileName = %@ OR fileNameData = %@)", tableAccount.account, directoryID, fileName, fileName).first else {
  1220. return nil
  1221. }
  1222. return tableMetadata.init(value: result)
  1223. }
  1224. func getTableMetadataDownload() -> [tableMetadata]? {
  1225. guard let tableAccount = self.getAccountActive() else {
  1226. return nil
  1227. }
  1228. let predicate = NSPredicate(format: "account = %@ AND (session = %@ OR session = %@) AND (sessionTaskIdentifier != %i OR sessionTaskIdentifierPlist != %i)", tableAccount.account, k_download_session, k_download_session_foreground, k_taskIdentifierDone, k_taskIdentifierDone)
  1229. return self.getMetadatas(predicate: predicate, sorted: nil, ascending: false)
  1230. }
  1231. func getTableMetadataDownloadWWan() -> [tableMetadata]? {
  1232. guard let tableAccount = self.getAccountActive() else {
  1233. return nil
  1234. }
  1235. let predicate = NSPredicate(format: "account = %@ AND session = %@ AND (sessionTaskIdentifier != %i OR sessionTaskIdentifierPlist != %i)", tableAccount.account, k_download_session_wwan, k_taskIdentifierDone, k_taskIdentifierDone)
  1236. return self.getMetadatas(predicate: predicate, sorted: nil, ascending: false)
  1237. }
  1238. func getTableMetadataUpload() -> [tableMetadata]? {
  1239. guard let tableAccount = self.getAccountActive() else {
  1240. return nil
  1241. }
  1242. let predicate = NSPredicate(format: "account = %@ AND (session = %@ OR session = %@) AND (sessionTaskIdentifier != %i OR sessionTaskIdentifierPlist != %i)", tableAccount.account, k_upload_session, k_upload_session_foreground, k_taskIdentifierDone, k_taskIdentifierDone)
  1243. return self.getMetadatas(predicate: predicate, sorted: nil, ascending: false)
  1244. }
  1245. func getTableMetadataUploadWWan() -> [tableMetadata]? {
  1246. guard let tableAccount = self.getAccountActive() else {
  1247. return nil
  1248. }
  1249. let predicate = NSPredicate(format: "account = %@ AND session = %@ AND (sessionTaskIdentifier != %i OR sessionTaskIdentifierPlist != %i)", tableAccount.account, k_upload_session_wwan, k_taskIdentifierDone, k_taskIdentifierDone)
  1250. return self.getMetadatas(predicate: predicate, sorted: nil, ascending: false)
  1251. }
  1252. func getTableMetadatasPhotos(serverUrl: String) -> [tableMetadata]? {
  1253. guard let tableAccount = self.getAccountActive() else {
  1254. return nil
  1255. }
  1256. let realm = try! Realm()
  1257. let directories = realm.objects(tableDirectory.self).filter(NSPredicate(format: "account = %@ AND serverUrl BEGINSWITH %@", tableAccount.account, serverUrl)).sorted(byKeyPath: "serverUrl", ascending: true)
  1258. let directoriesID = Array(directories.map { $0.directoryID })
  1259. let metadatas = realm.objects(tableMetadata.self).filter(NSPredicate(format: "account = %@ AND session = '' AND type = 'file' AND (typeFile = %@ OR typeFile = %@) AND directoryID IN %@", tableAccount.account, k_metadataTypeFile_image, k_metadataTypeFile_video, directoriesID)).sorted(byKeyPath: "date", ascending: false)
  1260. return Array(metadatas.map { tableMetadata.init(value:$0) })
  1261. }
  1262. //MARK: -
  1263. //MARK: Table Photo Library
  1264. func addPhotoLibrary(_ assets: [PHAsset]) {
  1265. guard let tableAccount = self.getAccountActive() else {
  1266. return
  1267. }
  1268. let realm = try! Realm()
  1269. if realm.isInWriteTransaction {
  1270. print("[LOG] Could not write to database, addPhotoLibrary is already in write transaction")
  1271. } else {
  1272. do {
  1273. try realm.write {
  1274. var creationDate = ""
  1275. var modificationDate = ""
  1276. for asset in assets {
  1277. let addRecord = tablePhotoLibrary()
  1278. addRecord.account = tableAccount.account
  1279. addRecord.assetLocalIdentifier = asset.localIdentifier
  1280. addRecord.mediaType = asset.mediaType.rawValue
  1281. if asset.creationDate != nil {
  1282. addRecord.creationDate = asset.creationDate! as NSDate
  1283. creationDate = String(describing: addRecord.creationDate!)
  1284. } else {
  1285. creationDate = ""
  1286. }
  1287. if asset.modificationDate != nil {
  1288. addRecord.modificationDate = asset.modificationDate! as NSDate
  1289. modificationDate = String(describing: addRecord.modificationDate!)
  1290. } else {
  1291. modificationDate = ""
  1292. }
  1293. addRecord.idAsset = "\(tableAccount.account)\(asset.localIdentifier)\(creationDate)\(modificationDate)"
  1294. realm.add(addRecord, update: true)
  1295. }
  1296. }
  1297. } catch let error {
  1298. print("Could not write to database: ", error)
  1299. }
  1300. }
  1301. }
  1302. func getPhotoLibraryIdAsset(image: Bool, video: Bool) -> [String]? {
  1303. guard let tableAccount = self.getAccountActive() else {
  1304. return nil
  1305. }
  1306. let realm = try! Realm()
  1307. var predicate = NSPredicate()
  1308. if (image && video) {
  1309. predicate = NSPredicate(format: "account = %@ AND (mediaType = %i || mediaType = %i)", tableAccount.account, PHAssetMediaType.image.rawValue, PHAssetMediaType.video.rawValue)
  1310. } else if (image) {
  1311. predicate = NSPredicate(format: "account = %@ AND mediaType = %i", tableAccount.account, PHAssetMediaType.image.rawValue)
  1312. } else if (video) {
  1313. predicate = NSPredicate(format: "account = %@ AND mediaType = %i", tableAccount.account, PHAssetMediaType.video.rawValue)
  1314. }
  1315. let results = realm.objects(tablePhotoLibrary.self).filter(predicate)
  1316. let idsAsset = results.map { $0.idAsset }
  1317. return Array(idsAsset)
  1318. }
  1319. //MARK: -
  1320. //MARK: Table Share
  1321. func addShareLink(_ share: String, fileName: String, serverUrl: String) -> [String:String]? {
  1322. guard let tableAccount = self.getAccountActive() else {
  1323. return nil
  1324. }
  1325. let realm = try! Realm()
  1326. realm.beginWrite()
  1327. // Verify if exists
  1328. let result = realm.objects(tableShare.self).filter("account = %@ AND fileName = %@ AND serverUrl = %@", tableAccount.account, fileName, serverUrl).first
  1329. if result != nil {
  1330. result?.shareLink = share
  1331. } else {
  1332. // Add new record
  1333. let addShare = tableShare()
  1334. addShare.account = tableAccount.account
  1335. addShare.fileName = fileName
  1336. addShare.serverUrl = serverUrl
  1337. addShare.shareLink = share
  1338. realm.add(addShare)
  1339. }
  1340. do {
  1341. try realm.commitWrite()
  1342. } catch let error {
  1343. print("[LOG] Could not write to database: ", error)
  1344. }
  1345. return ["\(serverUrl)\(fileName)" : share]
  1346. }
  1347. func addShareUserAndGroup(_ share: String, fileName: String, serverUrl: String) -> [String:String]? {
  1348. guard let tableAccount = self.getAccountActive() else {
  1349. return nil
  1350. }
  1351. let realm = try! Realm()
  1352. realm.beginWrite()
  1353. // Verify if exists
  1354. let result = realm.objects(tableShare.self).filter("account = %@ AND fileName = %@ AND serverUrl = %@", tableAccount.account, fileName, serverUrl).first
  1355. if result != nil {
  1356. result?.shareUserAndGroup = share
  1357. } else {
  1358. // Add new record
  1359. let addShare = tableShare()
  1360. addShare.account = tableAccount.account
  1361. addShare.fileName = fileName
  1362. addShare.serverUrl = serverUrl
  1363. addShare.shareUserAndGroup = share
  1364. realm.add(addShare)
  1365. }
  1366. do {
  1367. try realm.commitWrite()
  1368. } catch let error {
  1369. print("[LOG] Could not write to database: ", error)
  1370. }
  1371. return ["\(serverUrl)\(fileName)" : share]
  1372. }
  1373. func unShare(_ share: String, fileName: String, serverUrl: String, sharesLink: [String:String], sharesUserAndGroup: [String:String]) -> [Any]? {
  1374. guard let tableAccount = self.getAccountActive() else {
  1375. return nil
  1376. }
  1377. var sharesLink = sharesLink
  1378. var sharesUserAndGroup = sharesUserAndGroup
  1379. let realm = try! Realm()
  1380. realm.beginWrite()
  1381. let results = realm.objects(tableShare.self).filter("account = %@ AND (shareLink CONTAINS %@ OR shareUserAndGroup CONTAINS %@)", tableAccount.account, share, share)
  1382. if (results.count > 0) {
  1383. let result = results[0]
  1384. if (result.shareLink.contains(share)) {
  1385. result.shareLink = ""
  1386. }
  1387. if (result.shareUserAndGroup.contains(share)) {
  1388. var shares : [String] = result.shareUserAndGroup.components(separatedBy: ",")
  1389. if let index = shares.index(of:share) {
  1390. shares.remove(at: index)
  1391. }
  1392. result.shareUserAndGroup = shares.joined(separator: ",")
  1393. }
  1394. if (result.shareLink.characters.count > 0) {
  1395. sharesLink.updateValue(result.shareLink, forKey:"\(serverUrl)\(fileName)")
  1396. } else {
  1397. sharesLink.removeValue(forKey: "\(serverUrl)\(fileName)")
  1398. }
  1399. if (result.shareUserAndGroup.characters.count > 0) {
  1400. sharesUserAndGroup.updateValue(result.shareUserAndGroup, forKey:"\(serverUrl)\(fileName)")
  1401. } else {
  1402. sharesUserAndGroup.removeValue(forKey: "\(serverUrl)\(fileName)")
  1403. }
  1404. if (result.shareLink.characters.count == 0 && result.shareUserAndGroup.characters.count == 0) {
  1405. realm.delete(result)
  1406. }
  1407. }
  1408. do {
  1409. try realm.commitWrite()
  1410. } catch let error {
  1411. print("[LOG] Could not write to database: ", error)
  1412. }
  1413. return [sharesLink, sharesUserAndGroup]
  1414. }
  1415. func removeShareActiveAccount() {
  1416. guard let tableAccount = self.getAccountActive() else {
  1417. return
  1418. }
  1419. let realm = try! Realm()
  1420. do {
  1421. try realm.write {
  1422. let results = realm.objects(tableShare.self).filter("account = %@", tableAccount.account)
  1423. realm.delete(results)
  1424. }
  1425. } catch let error {
  1426. print("Could not write to database: ", error)
  1427. }
  1428. }
  1429. func updateShare(_ items: [String:OCSharedDto], activeUrl: String) -> [Any]? {
  1430. guard self.getAccountActive() != nil else {
  1431. return nil
  1432. }
  1433. var sharesLink = [String:String]()
  1434. var sharesUserAndGroup = [String:String]()
  1435. self.removeShareActiveAccount()
  1436. var itemsLink = [OCSharedDto]()
  1437. var itemsUsersAndGroups = [OCSharedDto]()
  1438. for (_, itemOCSharedDto) in items {
  1439. if (itemOCSharedDto.shareType == Int(shareTypeLink.rawValue)) {
  1440. itemsLink.append(itemOCSharedDto)
  1441. }
  1442. if (itemOCSharedDto.shareWith.characters.count > 0 && (itemOCSharedDto.shareType == Int(shareTypeUser.rawValue) || itemOCSharedDto.shareType == Int(shareTypeGroup.rawValue) || itemOCSharedDto.shareType == Int(shareTypeRemote.rawValue) )) {
  1443. itemsUsersAndGroups.append(itemOCSharedDto)
  1444. }
  1445. }
  1446. // Manage sharesLink
  1447. for itemOCSharedDto in itemsLink {
  1448. let fullPath = CCUtility.getHomeServerUrlActiveUrl(activeUrl) + "\(itemOCSharedDto.path!)"
  1449. let fileName = NSString(string: fullPath).lastPathComponent
  1450. var serverUrl = NSString(string: fullPath).substring(to: (fullPath.characters.count - fileName.characters.count - 1))
  1451. if serverUrl.hasSuffix("/") {
  1452. serverUrl = NSString(string: serverUrl).substring(to: (serverUrl.characters.count - 1))
  1453. }
  1454. if itemOCSharedDto.idRemoteShared > 0 {
  1455. let sharesLinkReturn = self.addShareLink("\(itemOCSharedDto.idRemoteShared)", fileName: fileName, serverUrl: serverUrl)
  1456. if sharesLinkReturn != nil {
  1457. for (key,value) in sharesLinkReturn! {
  1458. sharesLink.updateValue(value, forKey:key)
  1459. }
  1460. }
  1461. }
  1462. }
  1463. // Manage sharesUserAndGroup
  1464. var paths = [String:[String]]()
  1465. for itemOCSharedDto in itemsUsersAndGroups {
  1466. if paths[itemOCSharedDto.path] != nil {
  1467. var share : [String] = paths[itemOCSharedDto.path]!
  1468. share.append("\(itemOCSharedDto.idRemoteShared)")
  1469. paths[itemOCSharedDto.path] = share
  1470. } else {
  1471. paths[itemOCSharedDto.path] = ["\(itemOCSharedDto.idRemoteShared)"]
  1472. }
  1473. }
  1474. for (path, idsRemoteSharedArray) in paths {
  1475. let idsRemoteShared = idsRemoteSharedArray.joined(separator: ",")
  1476. print("[LOG] share \(String(describing: idsRemoteShared))")
  1477. let fullPath = CCUtility.getHomeServerUrlActiveUrl(activeUrl) + "\(path)"
  1478. let fileName = NSString(string: fullPath).lastPathComponent
  1479. var serverUrl = NSString(string: fullPath).substring(to: (fullPath.characters.count - fileName.characters.count - 1))
  1480. if serverUrl.hasSuffix("/") {
  1481. serverUrl = NSString(string: serverUrl).substring(to: (serverUrl.characters.count - 1))
  1482. }
  1483. let sharesUserAndGroupReturn = self.addShareUserAndGroup(idsRemoteShared, fileName: fileName, serverUrl: serverUrl)
  1484. if sharesUserAndGroupReturn != nil {
  1485. for (key,value) in sharesUserAndGroupReturn! {
  1486. sharesUserAndGroup.updateValue(value, forKey:key)
  1487. }
  1488. }
  1489. }
  1490. return [sharesLink, sharesUserAndGroup]
  1491. }
  1492. func getShares() -> [Any]? {
  1493. guard let tableAccount = self.getAccountActive() else {
  1494. return nil
  1495. }
  1496. var sharesLink = [String:String]()
  1497. var sharesUserAndGroup = [String:String]()
  1498. let realm = try! Realm()
  1499. let results = realm.objects(tableShare.self).filter("account = %@", tableAccount.account)
  1500. for resultShare in results {
  1501. if (resultShare.shareLink.characters.count > 0) {
  1502. sharesLink = [resultShare.shareLink: "\(resultShare.serverUrl)\(resultShare.fileName)"]
  1503. }
  1504. if (resultShare.shareUserAndGroup.characters.count > 0) {
  1505. sharesUserAndGroup = [resultShare.shareUserAndGroup: "\(resultShare.serverUrl)\(resultShare.fileName)"]
  1506. }
  1507. }
  1508. return [sharesLink, sharesUserAndGroup]
  1509. }
  1510. func getTableShares() -> [tableShare]? {
  1511. guard let tableAccount = self.getAccountActive() else {
  1512. return nil
  1513. }
  1514. let realm = try! Realm()
  1515. let results = realm.objects(tableShare.self).filter("account = %@", tableAccount.account).sorted(byKeyPath: "fileName", ascending: true)
  1516. return Array(results)
  1517. }
  1518. //MARK: -
  1519. //MARK: Migrate func
  1520. func addTableAccountFromCoredata(_ table: TableAccount) {
  1521. let realm = try! Realm()
  1522. realm.beginWrite()
  1523. let results = realm.objects(tableAccount.self).filter("account = %@", table.account!)
  1524. if (results.count == 0) {
  1525. let addAccount = tableAccount()
  1526. addAccount.account = table.account!
  1527. if table.active == 1 {
  1528. addAccount.active = true
  1529. }
  1530. if table.cameraUpload == 1 {
  1531. addAccount.autoUpload = true
  1532. }
  1533. if table.cameraUploadBackground == 1 {
  1534. addAccount.autoUploadBackground = true
  1535. }
  1536. if table.cameraUploadCreateSubfolder == 1 {
  1537. addAccount.autoUploadCreateSubfolder = true
  1538. }
  1539. if table.cameraUploadFolderName != nil {
  1540. addAccount.autoUploadFileName = table.cameraUploadFolderName!
  1541. }
  1542. if table.cameraUploadFolderPath != nil {
  1543. addAccount.autoUploadDirectory = table.cameraUploadFolderPath!
  1544. }
  1545. if table.cameraUploadFull == 1 {
  1546. addAccount.autoUploadFull = true
  1547. }
  1548. if table.cameraUploadPhoto == 1 {
  1549. addAccount.autoUploadImage = true
  1550. }
  1551. if table.cameraUploadVideo == 1 {
  1552. addAccount.autoUploadVideo = true
  1553. }
  1554. if table.cameraUploadWWAnPhoto == 1 {
  1555. addAccount.autoUploadWWAnPhoto = true
  1556. }
  1557. if table.cameraUploadWWAnVideo == 1 {
  1558. addAccount.autoUploadWWAnVideo = true
  1559. }
  1560. addAccount.password = table.password!
  1561. addAccount.url = table.url!
  1562. addAccount.user = table.user!
  1563. realm.add(addAccount)
  1564. }
  1565. do {
  1566. try realm.commitWrite()
  1567. } catch let error {
  1568. print("[LOG] Could not write to database: ", error)
  1569. }
  1570. }
  1571. func addTableDirectoryFromCoredata(_ table: TableDirectory) {
  1572. let realm = try! Realm()
  1573. realm.beginWrite()
  1574. let results = realm.objects(tableDirectory.self).filter("directoryID = %@", table.directoryID!)
  1575. if (results.count == 0) {
  1576. let addDirectory = tableDirectory()
  1577. addDirectory.account = table.account!
  1578. addDirectory.directoryID = table.directoryID!
  1579. addDirectory.etag = table.rev!
  1580. if table.favorite == 1 {
  1581. addDirectory.favorite = true
  1582. }
  1583. addDirectory.fileID = table.fileID!
  1584. if table.lock == 1 {
  1585. addDirectory.lock = true
  1586. }
  1587. addDirectory.permissions = table.permissions!
  1588. addDirectory.serverUrl = table.serverUrl!
  1589. realm.add(addDirectory)
  1590. }
  1591. do {
  1592. try realm.commitWrite()
  1593. } catch let error {
  1594. print("[LOG] Could not write to database: ", error)
  1595. }
  1596. }
  1597. func addTableLocalFileFromCoredata(_ table: TableLocalFile) {
  1598. let realm = try! Realm()
  1599. realm.beginWrite()
  1600. let results = realm.objects(tableLocalFile.self).filter("fileID = %@", table.fileID!)
  1601. if (results.count == 0) {
  1602. let addLocalFile = tableLocalFile()
  1603. addLocalFile.account = table.account!
  1604. if table.date != nil {
  1605. addLocalFile.date = table.date! as NSDate
  1606. } else {
  1607. addLocalFile.date = NSDate()
  1608. }
  1609. addLocalFile.etag = table.rev!
  1610. if table.exifDate != nil {
  1611. addLocalFile.exifDate = table.exifDate! as NSDate
  1612. }
  1613. addLocalFile.exifLatitude = table.exifLatitude!
  1614. addLocalFile.exifLongitude = table.exifLongitude!
  1615. if table.favorite == 1 {
  1616. addLocalFile.favorite = true
  1617. }
  1618. addLocalFile.fileID = table.fileID!
  1619. addLocalFile.fileName = table.fileName!
  1620. addLocalFile.fileNamePrint = table.fileNamePrint!
  1621. addLocalFile.size = table.size as! Double
  1622. realm.add(addLocalFile)
  1623. }
  1624. do {
  1625. try realm.commitWrite()
  1626. } catch let error {
  1627. print("[LOG] Could not write to database: ", error)
  1628. }
  1629. }
  1630. //MARK: -
  1631. }