api.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import axios from 'nextcloud-axios'
  2. import { generateRemoteUrl } from 'nextcloud-router'
  3. const xmlToJson = (xml) => {
  4. let obj = {}
  5. if (xml.nodeType === 1) {
  6. if (xml.attributes.length > 0) {
  7. obj['@attributes'] = {}
  8. for (let j = 0; j < xml.attributes.length; j++) {
  9. const attribute = xml.attributes.item(j)
  10. obj['@attributes'][attribute.nodeName] = attribute.nodeValue
  11. }
  12. }
  13. } else if (xml.nodeType === 3) {
  14. obj = xml.nodeValue
  15. }
  16. if (xml.hasChildNodes()) {
  17. for (let i = 0; i < xml.childNodes.length; i++) {
  18. const item = xml.childNodes.item(i)
  19. const nodeName = item.nodeName
  20. if (typeof (obj[nodeName]) === 'undefined') {
  21. obj[nodeName] = xmlToJson(item)
  22. } else {
  23. if (typeof obj[nodeName].push === 'undefined') {
  24. var old = obj[nodeName]
  25. obj[nodeName] = []
  26. obj[nodeName].push(old)
  27. }
  28. obj[nodeName].push(xmlToJson(item))
  29. }
  30. }
  31. }
  32. return obj
  33. }
  34. const parseXml = (xml) => {
  35. let dom = null
  36. try {
  37. dom = (new DOMParser()).parseFromString(xml, 'text/xml')
  38. } catch (e) {
  39. console.error('Failed to parse xml document', e)
  40. }
  41. return dom
  42. }
  43. const xmlToTagList = (xml) => {
  44. const json = xmlToJson(parseXml(xml))
  45. const list = json['d:multistatus']['d:response']
  46. const result = []
  47. for (const index in list) {
  48. const tag = list[index]['d:propstat']
  49. if (tag['d:status']['#text'] !== 'HTTP/1.1 200 OK') {
  50. continue
  51. }
  52. result.push({
  53. id: tag['d:prop']['oc:id']['#text'],
  54. displayName: tag['d:prop']['oc:display-name']['#text'],
  55. canAssign: tag['d:prop']['oc:can-assign']['#text'] === 'true',
  56. userAssignable: tag['d:prop']['oc:user-assignable']['#text'] === 'true',
  57. userVisible: tag['d:prop']['oc:user-visible']['#text'] === 'true'
  58. })
  59. }
  60. return result
  61. }
  62. const searchTags = function() {
  63. return axios({
  64. method: 'PROPFIND',
  65. url: generateRemoteUrl('dav') + '/systemtags/',
  66. data: `<?xml version="1.0"?>
  67. <d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">
  68. <d:prop>
  69. <oc:id />
  70. <oc:display-name />
  71. <oc:user-visible />
  72. <oc:user-assignable />
  73. <oc:can-assign />
  74. </d:prop>
  75. </d:propfind>`
  76. }).then((response) => {
  77. return xmlToTagList(response.data)
  78. })
  79. }
  80. export {
  81. searchTags
  82. }