NextcloudData.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // NextcloudData.swift
  3. // Widget
  4. //
  5. // Created by Marino Faggiana on 25/08/22.
  6. // Copyright © 2022 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import WidgetKit
  24. import NCCommunication
  25. struct NextcloudData: Identifiable, Codable, Hashable {
  26. var id: String
  27. var image: String
  28. var title: String
  29. var subTitle: String
  30. var url: URL
  31. }
  32. struct NextcloudDataEntry: TimelineEntry {
  33. let date: Date
  34. let nextcloudDatas: [NextcloudData]
  35. let isPlaceholder: Bool
  36. let footerText: String
  37. }
  38. let nextcloudDatasTest: [NextcloudData] = [
  39. .init(id: "0", image: "nextcloud", title: "title 1", subTitle: "subTitle - description 1", url: URL(string: "https://nextcloud.com/")!),
  40. .init(id: "1", image: "nextcloud", title: "title 2", subTitle: "subTitle - description 2", url: URL(string: "https://nextcloud.com/")!),
  41. .init(id: "2", image: "nextcloud", title: "title 3", subTitle: "subTitle - description 3", url: URL(string: "https://nextcloud.com/")!),
  42. .init(id: "3", image: "nextcloud", title: "title 4", subTitle: "subTitle - description 4", url: URL(string: "https://nextcloud.com/")!)
  43. ]
  44. func readNextcloudData(completion: @escaping (_ NextcloudDatas: [NextcloudData], _ isPlaceholder: Bool, _ footerText: String) -> Void) {
  45. guard let account = NCManageDatabase.shared.getActiveAccount() else {
  46. return completion(nextcloudDatasTest, true, NSLocalizedString("_no_active_account_", value: "No account found", comment: ""))
  47. }
  48. // NETWORKING
  49. let password = CCUtility.getPassword(account.account)!
  50. NCCommunicationCommon.shared.setup(
  51. account: account.account,
  52. user: account.user,
  53. userId: account.userId,
  54. password: password,
  55. urlBase: account.urlBase,
  56. userAgent: CCUtility.getUserAgent(),
  57. webDav: NCUtilityFileSystem.shared.getWebDAV(account: account.account),
  58. nextcloudVersion: 0,
  59. delegate: NCNetworking.shared)
  60. let requestBodyRecent =
  61. """
  62. <?xml version=\"1.0\"?>
  63. <d:searchrequest xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">
  64. <d:basicsearch>
  65. <d:select>
  66. <d:prop>
  67. <d:displayname/>
  68. <d:getcontenttype/>
  69. <d:resourcetype/>
  70. <d:getcontentlength/>
  71. <d:getlastmodified/>
  72. <d:getetag/>
  73. <d:quota-used-bytes/>
  74. <d:quota-available-bytes/>
  75. <permissions xmlns=\"http://owncloud.org/ns\"/>
  76. <id xmlns=\"http://owncloud.org/ns\"/>
  77. <fileid xmlns=\"http://owncloud.org/ns\"/>
  78. <size xmlns=\"http://owncloud.org/ns\"/>
  79. <favorite xmlns=\"http://owncloud.org/ns\"/>
  80. <creation_time xmlns=\"http://nextcloud.org/ns\"/>
  81. <upload_time xmlns=\"http://nextcloud.org/ns\"/>
  82. <is-encrypted xmlns=\"http://nextcloud.org/ns\"/>
  83. <mount-type xmlns=\"http://nextcloud.org/ns\"/>
  84. <owner-id xmlns=\"http://owncloud.org/ns\"/>
  85. <owner-display-name xmlns=\"http://owncloud.org/ns\"/>
  86. <comments-unread xmlns=\"http://owncloud.org/ns\"/>
  87. <has-preview xmlns=\"http://nextcloud.org/ns\"/>
  88. <trashbin-filename xmlns=\"http://nextcloud.org/ns\"/>
  89. <trashbin-original-location xmlns=\"http://nextcloud.org/ns\"/>
  90. <trashbin-deletion-time xmlns=\"http://nextcloud.org/ns\"/>
  91. </d:prop>
  92. </d:select>
  93. <d:from>
  94. <d:scope>
  95. <d:href>%@</d:href>
  96. <d:depth>infinity</d:depth>
  97. </d:scope>
  98. </d:from>
  99. <d:where>
  100. <d:lt>
  101. <d:prop>
  102. <d:getlastmodified/>
  103. </d:prop>
  104. <d:literal>%@</d:literal>
  105. </d:lt>
  106. </d:where>
  107. <d:orderby>
  108. <d:order>
  109. <d:prop>
  110. <d:getlastmodified/>
  111. </d:prop>
  112. <d:descending/>
  113. </d:order>
  114. </d:orderby>
  115. <d:limit>
  116. <d:nresults>4</d:nresults>
  117. </d:limit>
  118. </d:basicsearch>
  119. </d:searchrequest>
  120. """
  121. let dateFormatter = DateFormatter()
  122. dateFormatter.locale = Locale(identifier: "en_US_POSIX")
  123. dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
  124. let lessDateString = dateFormatter.string(from: Date())
  125. let requestBody = String(format: requestBodyRecent, "/files/" + account.userId, lessDateString)
  126. // LOG
  127. let levelLog = CCUtility.getLogLevel()
  128. let isSimulatorOrTestFlight = NCUtility.shared.isSimulatorOrTestFlight()
  129. let versionNextcloudiOS = String(format: NCBrandOptions.shared.textCopyrightNextcloudiOS, NCUtility.shared.getVersionApp())
  130. NCCommunicationCommon.shared.levelLog = levelLog
  131. if let pathDirectoryGroup = CCUtility.getDirectoryGroup()?.path {
  132. NCCommunicationCommon.shared.pathLog = pathDirectoryGroup
  133. }
  134. if isSimulatorOrTestFlight {
  135. NCCommunicationCommon.shared.writeLog("Start \(NCBrandOptions.shared.brand) widget session with level \(levelLog) " + versionNextcloudiOS + " (Simulator / TestFlight)")
  136. } else {
  137. NCCommunicationCommon.shared.writeLog("Start \(NCBrandOptions.shared.brand) widget session with level \(levelLog) " + versionNextcloudiOS)
  138. }
  139. NCCommunicationCommon.shared.writeLog("Start \(NCBrandOptions.shared.brand) widget [Auto upload]")
  140. NCAutoUpload.shared.initAutoUpload(viewController: nil) { items in
  141. NCCommunicationCommon.shared.writeLog("Completition \(NCBrandOptions.shared.brand) widget [Auto upload]")
  142. NCCommunication.shared.searchBodyRequest(serverUrl: account.urlBase, requestBody: requestBody, showHiddenFiles: CCUtility.getShowHiddenFiles()) { _, files, errorCode, errorDescription in
  143. var nextcloudDatas: [NextcloudData] = []
  144. for file in files {
  145. let subTitle = CCUtility.dateDiff(file.date as Date) + " · " + CCUtility.transformedSize(file.size)
  146. let nextcloudData = NextcloudData.init(id: file.ocId, image: "", title: file.fileName, subTitle: subTitle, url: URL(string: "https://nextcloud.com/")!)
  147. nextcloudDatas.append(nextcloudData)
  148. }
  149. if errorCode != 0 {
  150. completion(nextcloudDatasTest, true, errorDescription)
  151. } else if nextcloudDatas.isEmpty {
  152. completion(nextcloudDatasTest, true, "Auto upoload: \(items), \(Date().formatted())")
  153. } else {
  154. completion(nextcloudDatas, false, "Auto upoload: \(items), \(Date().formatted())")
  155. }
  156. }
  157. }
  158. }