NextcloudData.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. let imageSize:CGFloat = 30
  26. let spacingImageUpload:CGFloat = 10
  27. struct NextcloudDataEntry: TimelineEntry {
  28. let date: Date
  29. let recentDatas: [RecentData]
  30. let isPlaceholder: Bool
  31. let footerText: String
  32. }
  33. struct RecentData: Identifiable, Hashable {
  34. var id: String
  35. var image: UIImage
  36. var title: String
  37. var subTitle: String
  38. var url: URL
  39. }
  40. let recentDatasTest: [RecentData] = [
  41. .init(id: "1", image: UIImage(named: "nextcloud")!, title: "title1", subTitle: "subTitle-description1", url: URL(string: "https://nextcloud.com/")!),
  42. .init(id: "2", image: UIImage(named: "nextcloud")!, title: "title2", subTitle: "subTitle-description2", url: URL(string: "https://nextcloud.com/")!),
  43. .init(id: "3", image: UIImage(named: "nextcloud")!, title: "title3", subTitle: "subTitle-description3", url: URL(string: "https://nextcloud.com/")!),
  44. .init(id: "4", image: UIImage(named: "nextcloud")!, title: "title4", subTitle: "subTitle-description4", url: URL(string: "https://nextcloud.com/")!),
  45. .init(id: "5", image: UIImage(named: "nextcloud")!, title: "title5", subTitle: "subTitle-description5", url: URL(string: "https://nextcloud.com/")!),
  46. .init(id: "6", image: UIImage(named: "nextcloud")!, title: "title6", subTitle: "subTitle-description6", url: URL(string: "https://nextcloud.com/")!)
  47. ]
  48. func getDataEntry(isPreview: Bool, displaySize: CGSize, completion: @escaping (_ entry: NextcloudDataEntry) -> Void) {
  49. if isPreview {
  50. return completion(NextcloudDataEntry(date: Date(), recentDatas: recentDatasTest, isPlaceholder: true, footerText: ""))
  51. }
  52. guard let account = NCManageDatabase.shared.getActiveAccount() else {
  53. return completion(NextcloudDataEntry(date: Date(), recentDatas: recentDatasTest, isPlaceholder: true, footerText: NSLocalizedString("_no_active_account_", value: "No account found", comment: "")))
  54. }
  55. func isLive(file: NCCommunicationFile, files: [NCCommunicationFile]) -> Bool {
  56. if file.ext.lowercased() != "mov" { return false }
  57. if files.filter({ ($0.fileNameWithoutExt == file.fileNameWithoutExt) && ($0.ext.lowercased() == "jpg") }).first != nil {
  58. return true
  59. }
  60. return false
  61. }
  62. // NETWORKING
  63. let password = CCUtility.getPassword(account.account)!
  64. NCCommunicationCommon.shared.setup(
  65. account: account.account,
  66. user: account.user,
  67. userId: account.userId,
  68. password: password,
  69. urlBase: account.urlBase,
  70. userAgent: CCUtility.getUserAgent(),
  71. webDav: NCUtilityFileSystem.shared.getWebDAV(account: account.account),
  72. nextcloudVersion: 0,
  73. delegate: NCNetworking.shared)
  74. let requestBodyRecent =
  75. """
  76. <?xml version=\"1.0\"?>
  77. <d:searchrequest xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">
  78. <d:basicsearch>
  79. <d:select>
  80. <d:prop>
  81. <d:displayname/>
  82. <d:getcontenttype/>
  83. <d:resourcetype/>
  84. <d:getcontentlength/>
  85. <d:getlastmodified/>
  86. <d:getetag/>
  87. <d:quota-used-bytes/>
  88. <d:quota-available-bytes/>
  89. <permissions xmlns=\"http://owncloud.org/ns\"/>
  90. <id xmlns=\"http://owncloud.org/ns\"/>
  91. <fileid xmlns=\"http://owncloud.org/ns\"/>
  92. <size xmlns=\"http://owncloud.org/ns\"/>
  93. <favorite xmlns=\"http://owncloud.org/ns\"/>
  94. <creation_time xmlns=\"http://nextcloud.org/ns\"/>
  95. <upload_time xmlns=\"http://nextcloud.org/ns\"/>
  96. <is-encrypted xmlns=\"http://nextcloud.org/ns\"/>
  97. <mount-type xmlns=\"http://nextcloud.org/ns\"/>
  98. <owner-id xmlns=\"http://owncloud.org/ns\"/>
  99. <owner-display-name xmlns=\"http://owncloud.org/ns\"/>
  100. <comments-unread xmlns=\"http://owncloud.org/ns\"/>
  101. <has-preview xmlns=\"http://nextcloud.org/ns\"/>
  102. <trashbin-filename xmlns=\"http://nextcloud.org/ns\"/>
  103. <trashbin-original-location xmlns=\"http://nextcloud.org/ns\"/>
  104. <trashbin-deletion-time xmlns=\"http://nextcloud.org/ns\"/>
  105. </d:prop>
  106. </d:select>
  107. <d:from>
  108. <d:scope>
  109. <d:href>%@</d:href>
  110. <d:depth>infinity</d:depth>
  111. </d:scope>
  112. </d:from>
  113. <d:where>
  114. <d:lt>
  115. <d:prop>
  116. <d:getlastmodified/>
  117. </d:prop>
  118. <d:literal>%@</d:literal>
  119. </d:lt>
  120. </d:where>
  121. <d:orderby>
  122. <d:order>
  123. <d:prop>
  124. <d:getlastmodified/>
  125. </d:prop>
  126. <d:descending/>
  127. </d:order>
  128. </d:orderby>
  129. <d:limit>
  130. <d:nresults>50</d:nresults>
  131. </d:limit>
  132. </d:basicsearch>
  133. </d:searchrequest>
  134. """
  135. let dateFormatter = DateFormatter()
  136. dateFormatter.locale = Locale(identifier: "en_US_POSIX")
  137. dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
  138. let lessDateString = dateFormatter.string(from: Date())
  139. let requestBody = String(format: requestBodyRecent, "/files/" + account.userId, lessDateString)
  140. // LOG
  141. let levelLog = CCUtility.getLogLevel()
  142. let isSimulatorOrTestFlight = NCUtility.shared.isSimulatorOrTestFlight()
  143. let versionNextcloudiOS = String(format: NCBrandOptions.shared.textCopyrightNextcloudiOS, NCUtility.shared.getVersionApp())
  144. NCCommunicationCommon.shared.levelLog = levelLog
  145. if let pathDirectoryGroup = CCUtility.getDirectoryGroup()?.path {
  146. NCCommunicationCommon.shared.pathLog = pathDirectoryGroup
  147. }
  148. if isSimulatorOrTestFlight {
  149. NCCommunicationCommon.shared.writeLog("Start \(NCBrandOptions.shared.brand) widget session with level \(levelLog) " + versionNextcloudiOS + " (Simulator / TestFlight)")
  150. } else {
  151. NCCommunicationCommon.shared.writeLog("Start \(NCBrandOptions.shared.brand) widget session with level \(levelLog) " + versionNextcloudiOS)
  152. }
  153. NCCommunicationCommon.shared.writeLog("Start \(NCBrandOptions.shared.brand) widget [Auto upload]")
  154. NCAutoUpload.shared.initAutoUpload(viewController: nil) { _ in
  155. NCCommunicationCommon.shared.writeLog("Completition \(NCBrandOptions.shared.brand) widget [Auto upload]")
  156. NCCommunication.shared.searchBodyRequest(serverUrl: account.urlBase, requestBody: requestBody, showHiddenFiles: CCUtility.getShowHiddenFiles()) { _, files, errorCode, errorDescription in
  157. // Get recent files
  158. var recentDatas: [RecentData] = []
  159. for file in files {
  160. guard !file.directory else { continue }
  161. guard !isLive(file: file, files: files) else { continue }
  162. let metadata = NCManageDatabase.shared.convertNCFileToMetadata(file, isEncrypted: false, account: account.account)
  163. let subTitle = CCUtility.dateDiff(metadata.date as Date) + " · " + CCUtility.transformedSize(metadata.size)
  164. // url: nextcloud://open-file?path=Talk/IMG_0000123.jpg&user=marinofaggiana&link=https://cloud.nextcloud.com/f/123
  165. guard var path = NCUtilityFileSystem.shared.getPath(path: metadata.path, user: metadata.user, fileName: metadata.fileName).urlEncoded else { continue }
  166. if path.first == "/" { path = String(path.dropFirst())}
  167. guard let user = metadata.user.urlEncoded else { continue }
  168. let link = metadata.urlBase + "/f/" + metadata.fileId
  169. let urlString = "nextcloud://open-file?path=\(path)&user=\(user)&link=\(link)"
  170. guard let url = URL(string: urlString) else { continue }
  171. // Recent Data
  172. let image:UIImage = NCUtilityGUI.shared.createFilePreviewImage(metadata: metadata, size: imageSize * 3, createPreview: false) ?? UIImage(named: "file")!
  173. let recentData = RecentData.init(id: metadata.ocId, image: image, title: metadata.fileName, subTitle: subTitle, url: url)
  174. recentDatas.append(recentData)
  175. if recentDatas.count == 6 { break}
  176. }
  177. let fileInUpload = NCManageDatabase.shared.getNumMetadatasInUpload()
  178. // Completion
  179. if errorCode != 0 {
  180. completion(NextcloudDataEntry(date: Date(), recentDatas: recentDatasTest, isPlaceholder: true, footerText: errorDescription))
  181. } else if recentDatas.isEmpty {
  182. completion(NextcloudDataEntry(date: Date(), recentDatas: recentDatasTest, isPlaceholder: true, footerText: "\(Date().formatted())"))
  183. } else {
  184. completion(NextcloudDataEntry(date: Date(), recentDatas: recentDatas, isPlaceholder: false, footerText: "\(Date().formatted())"))
  185. }
  186. }
  187. }
  188. }