NCCommunication.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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(path: String, user: String, password: String, completionHandler: @escaping (_ result: [NCFile]?, _ error: Error?) -> Void) {
  58. // URL
  59. var url: URLConvertible
  60. do {
  61. try url = path.asURL()
  62. } catch _ {
  63. completionHandler(nil, nil)
  64. return
  65. }
  66. // Headers
  67. var headers: HTTPHeaders = [.authorization(username: user, password: password)]
  68. headers.update(.userAgent(CCUtility.getUserAgent()))
  69. headers.update(.contentType("application/xml"))
  70. headers.update(name: "Depth", value: "1")
  71. // Parameters
  72. //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>"]
  73. // Method
  74. let method = HTTPMethod(rawValue: "PROPFIND")
  75. AF.request(url, method: method, parameters:[:], encoding: URLEncoding.httpBody, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData { (response) in
  76. switch response.result {
  77. case.failure(let error):
  78. completionHandler(nil, error)
  79. case .success( _):
  80. if let data = response.data {
  81. let xml = XML.parse(data)
  82. print("success")
  83. }
  84. print("success")
  85. }
  86. }
  87. }
  88. }