NextcloudData.swift 9.6 KB

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