NCCommunication.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. let NCResource =
  32. """
  33. <d:displayname/>
  34. <d:getcontenttype/>
  35. <d:resourcetype/>
  36. <d:getcontentlength/>
  37. <d:getlastmodified/>
  38. <d:creationdate/>
  39. <d:getetag/>
  40. <d:quota-used-bytes/>
  41. <d:quota-available-bytes/>
  42. <permissions xmlns=\"http://owncloud.org/ns\"/>
  43. <id xmlns=\"http://owncloud.org/ns\"/>
  44. <fileid xmlns=\"http://owncloud.org/ns\"/>
  45. <size xmlns=\"http://owncloud.org/ns\"/>
  46. <favorite xmlns=\"http://owncloud.org/ns\"/>
  47. <is-encrypted xmlns=\"http://nextcloud.org/ns\"/>
  48. <mount-type xmlns=\"http://nextcloud.org/ns\"/>
  49. <owner-id xmlns=\"http://owncloud.org/ns\"/>
  50. <owner-display-name xmlns=\"http://owncloud.org/ns\"/>
  51. <comments-unread xmlns=\"http://owncloud.org/ns\"/>
  52. <has-preview xmlns=\"http://nextcloud.org/ns\"/>
  53. <trashbin-filename xmlns=\"http://nextcloud.org/ns\"/>
  54. <trashbin-original-location xmlns=\"http://nextcloud.org/ns\"/>
  55. <trashbin-deletion-time xmlns=\"http://nextcloud.org/ns\"/>"
  56. """
  57. @objc func readFolder(serverUrl: String, account: String, user: String, password: String, depth: String, userAgent: String, completionHandler: @escaping (_ result: [NCFile], _ account: String,_ error: Error?) -> Void) {
  58. var files = [NCFile]()
  59. // URL
  60. var url: URLConvertible
  61. do {
  62. try url = serverUrl.asURL()
  63. } catch let error {
  64. completionHandler(files, account, error)
  65. return
  66. }
  67. // Headers
  68. var headers: HTTPHeaders = [.authorization(username: user, password: password)]
  69. headers.update(.userAgent(userAgent))
  70. headers.update(.contentType("application/xml"))
  71. headers.update(name: "Depth", value: depth)
  72. // Parameters
  73. //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>" + NCResource + "</d:prop></d:propfind>"]
  74. // Method
  75. let method = HTTPMethod(rawValue: "PROPFIND")
  76. AF.request(url, method: method, parameters:[:], encoding: URLEncoding.httpBody, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData { (response) in
  77. switch response.result {
  78. case.failure(let error):
  79. completionHandler(files, account, error)
  80. case .success( _):
  81. if let data = response.data {
  82. let xml = XML.parse(data)
  83. let elements = xml["d:multistatus", "d:response"]
  84. for element in elements {
  85. let file = NCFile()
  86. if let href = element["d:href"].text {
  87. file.path = href.removingPercentEncoding ?? ""
  88. }
  89. let propstat = element["d:propstat"][0]
  90. if let getetag = propstat["d:prop", "d:getetag"].text {
  91. file.etag = getetag.replacingOccurrences(of: "\"", with: "")
  92. }
  93. if let getlastmodified = propstat["d:prop", "d:getlastmodified"].text {
  94. if let date = NCCommunicationCommon.sharedInstance.convertDate(getlastmodified, format: "EEE, dd MMM y HH:mm:ss zzz") {
  95. file.date = date
  96. }
  97. }
  98. if let quotaavailablebytes = propstat["d:prop", "d:quota-available-bytes"].text {
  99. file.quotaAvailableBytes = Double(quotaavailablebytes) ?? 0
  100. }
  101. if let quotausedbytes = propstat["d:prop", "d:quota-used-bytes"].text {
  102. file.quotaUsedBytes = Double(quotausedbytes) ?? 0
  103. }
  104. files.append(file)
  105. }
  106. }
  107. completionHandler(files, account, nil)
  108. }
  109. }
  110. }
  111. }