NCCommunication.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // NCCommunication.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 12/10/19.
  6. // Copyright © 2018 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 Foundation
  24. import Alamofire
  25. import SwiftyXMLParser
  26. class NCCommunication: NSObject {
  27. @objc static let sharedInstance: NCCommunication = {
  28. let instance = NCCommunication()
  29. return instance
  30. }()
  31. //MARK: - webDAV
  32. @objc func readFolder(serverUrl: String, account: String, user: String, password: String, depth: String, userAgent: String, completionHandler: @escaping (_ result: [NCFile], _ account: String,_ error: Error?) -> Void) {
  33. var files = [NCFile]()
  34. let dataFile =
  35. """
  36. <?xml version=\"1.0\" encoding=\"UTF-8\"?>
  37. <d:propfind xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">
  38. <d:prop>"
  39. <d:displayname/>
  40. <d:getcontenttype/>
  41. <d:resourcetype/>
  42. <d:getcontentlength/>
  43. <d:getlastmodified/>
  44. <d:creationdate/>
  45. <d:getetag/>
  46. <d:quota-used-bytes/>
  47. <d:quota-available-bytes/>
  48. <permissions xmlns=\"http://owncloud.org/ns\"/>
  49. <id xmlns=\"http://owncloud.org/ns\"/>
  50. <fileid xmlns=\"http://owncloud.org/ns\"/>
  51. <size xmlns=\"http://owncloud.org/ns\"/>
  52. <favorite xmlns=\"http://owncloud.org/ns\"/>
  53. <is-encrypted xmlns=\"http://nextcloud.org/ns\"/>
  54. <mount-type xmlns=\"http://nextcloud.org/ns\"/>
  55. <owner-id xmlns=\"http://owncloud.org/ns\"/>
  56. <owner-display-name xmlns=\"http://owncloud.org/ns\"/>
  57. <comments-unread xmlns=\"http://owncloud.org/ns\"/>
  58. <has-preview xmlns=\"http://nextcloud.org/ns\"/>
  59. <trashbin-filename xmlns=\"http://nextcloud.org/ns\"/>
  60. <trashbin-original-location xmlns=\"http://nextcloud.org/ns\"/>
  61. <trashbin-deletion-time xmlns=\"http://nextcloud.org/ns\"/>"
  62. </d:prop>
  63. </d:propfind>
  64. """
  65. // url
  66. var url: URLConvertible
  67. do {
  68. try url = serverUrl.asURL()
  69. } catch let error {
  70. completionHandler(files, account, error)
  71. return
  72. }
  73. // Headers
  74. var headers: HTTPHeaders = [.authorization(username: user, password: password)]
  75. headers.update(.userAgent(userAgent))
  76. headers.update(.contentType("application/xml"))
  77. headers.update(name: "Depth", value: depth)
  78. // Parameters
  79. //let parameters: Parameters = ["":"<?xml version=\"1.0\" encoding=\"UTF-8\"?><d:propfind xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\"><d:prop>" + NCResourceList + "</d:prop></d:propfind>"]
  80. // Method
  81. let method = HTTPMethod(rawValue: "PROPFIND")
  82. AF.request(url, method: method, parameters:[:], encoding: URLEncoding.httpBody, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData { (response) in
  83. switch response.result {
  84. case.failure(let error):
  85. completionHandler(files, account, error)
  86. case .success( _):
  87. if let data = response.data {
  88. let xml = XML.parse(data)
  89. let elements = xml["d:multistatus", "d:response"]
  90. for element in elements {
  91. let file = NCFile()
  92. if let href = element["d:href"].text {
  93. file.path = href.removingPercentEncoding ?? ""
  94. }
  95. let propstat = element["d:propstat"][0]
  96. if let getetag = propstat["d:prop", "d:getetag"].text {
  97. file.etag = getetag.replacingOccurrences(of: "\"", with: "")
  98. }
  99. if let getlastmodified = propstat["d:prop", "d:getlastmodified"].text {
  100. if let date = NCCommunicationCommon.sharedInstance.convertDate(getlastmodified, format: "EEE, dd MMM y HH:mm:ss zzz") {
  101. file.date = date
  102. }
  103. }
  104. if let quotaavailablebytes = propstat["d:prop", "d:quota-available-bytes"].text {
  105. file.quotaAvailableBytes = Double(quotaavailablebytes) ?? 0
  106. }
  107. if let quotausedbytes = propstat["d:prop", "d:quota-used-bytes"].text {
  108. file.quotaUsedBytes = Double(quotausedbytes) ?? 0
  109. }
  110. files.append(file)
  111. }
  112. }
  113. completionHandler(files, account, nil)
  114. }
  115. }
  116. }
  117. //MARK: - Download
  118. @objc func download(serverUrl: String, fileName: String, fileNamePathDestination: String, account: String, user: String, password: String, userAgent: String, completionHandler: @escaping (_ account: String,_ error: Error?) -> Void) {
  119. // url
  120. var serverUrl = serverUrl
  121. var url: URLConvertible
  122. do {
  123. if serverUrl.last == "/" {
  124. serverUrl = serverUrl + fileName
  125. } else {
  126. serverUrl = serverUrl + "/" + fileName
  127. }
  128. try url = serverUrl.asURL()
  129. } catch let error {
  130. completionHandler(account, error)
  131. return
  132. }
  133. // Destination
  134. var destination: Alamofire.DownloadRequest.Destination?
  135. if let fileNamePathDestinationURL = URL(string: fileNamePathDestination) {
  136. let destinationFile: DownloadRequest.Destination = { _, _ in
  137. return (fileNamePathDestinationURL, [.removePreviousFile, .createIntermediateDirectories])
  138. }
  139. destination = destinationFile
  140. }
  141. // Headers
  142. var headers: HTTPHeaders = [.authorization(username: user, password: password)]
  143. headers.update(.userAgent(userAgent))
  144. AF.download(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil, to: destination).downloadProgress { progress in
  145. //self.postProgress(progress: progress)
  146. } .responseData { response in
  147. /*
  148. if let data = response.result.value {
  149. // let image = UIImage(data: data)
  150. }
  151. */
  152. }
  153. }
  154. }