ShareRequests.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * @copyright Copyright (c) 2019 John Molakvoæ <skjnldsv@protonmail.com>
  3. *
  4. * @author John Molakvoæ <skjnldsv@protonmail.com>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. // TODO: remove when ie not supported
  23. import 'url-search-params-polyfill'
  24. import { generateOcsUrl } from '@nextcloud/router'
  25. import axios from '@nextcloud/axios'
  26. import Share from '../models/Share'
  27. const shareUrl = generateOcsUrl('apps/files_sharing/api/v1', 2) + 'shares'
  28. const headers = {
  29. 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  30. }
  31. export default {
  32. methods: {
  33. /**
  34. * Create a new share
  35. *
  36. * @param {Object} data destructuring object
  37. * @param {string} data.path path to the file/folder which should be shared
  38. * @param {number} data.shareType 0 = user; 1 = group; 3 = public link; 6 = federated cloud share
  39. * @param {string} data.shareWith user/group id with which the file should be shared (optional for shareType > 1)
  40. * @param {boolean} [data.publicUpload=false] allow public upload to a public shared folder
  41. * @param {string} [data.password] password to protect public link Share with
  42. * @param {number} [data.permissions=31] 1 = read; 2 = update; 4 = create; 8 = delete; 16 = share; 31 = all (default: 31, for public shares: 1)
  43. * @param {boolean} [data.sendPasswordByTalk=false] send the password via a talk conversation
  44. * @param {string} [data.expireDate=''] expire the shareautomatically after
  45. * @param {string} [data.label=''] custom label
  46. * @returns {Share} the new share
  47. * @throws {Error}
  48. */
  49. async createShare({ path, permissions, shareType, shareWith, publicUpload, password, sendPasswordByTalk, expireDate, label }) {
  50. try {
  51. const request = await axios.post(shareUrl, { path, permissions, shareType, shareWith, publicUpload, password, sendPasswordByTalk, expireDate, label })
  52. if (!('ocs' in request.data)) {
  53. throw request
  54. }
  55. return new Share(request.data.ocs.data)
  56. } catch (error) {
  57. console.error('Error while creating share', error)
  58. OC.Notification.showTemporary(t('files_sharing', 'Error creating the share'), { type: 'error' })
  59. throw error
  60. }
  61. },
  62. /**
  63. * Delete a share
  64. *
  65. * @param {number} id share id
  66. * @throws {Error}
  67. */
  68. async deleteShare(id) {
  69. try {
  70. const request = await axios.delete(shareUrl + `/${id}`)
  71. if (!('ocs' in request.data)) {
  72. throw request
  73. }
  74. return true
  75. } catch (error) {
  76. console.error('Error while deleting share', error)
  77. OC.Notification.showTemporary(t('files_sharing', 'Error deleting the share'), { type: 'error' })
  78. throw error
  79. }
  80. },
  81. /**
  82. * Update a share
  83. *
  84. * @param {number} id share id
  85. * @param {Object} data destructuring object
  86. * @param {string} data.property property to update
  87. * @param {any} data.value value to set
  88. */
  89. async updateShare(id, { property, value }) {
  90. try {
  91. // ocs api requires x-www-form-urlencoded
  92. const data = new URLSearchParams()
  93. data.append(property, value)
  94. const request = await axios.put(shareUrl + `/${id}`, { [property]: value }, headers)
  95. if (!('ocs' in request.data)) {
  96. throw request
  97. }
  98. return true
  99. } catch (error) {
  100. console.error('Error while updating share', error)
  101. OC.Notification.showTemporary(t('files_sharing', 'Error updating the share'), { type: 'error' })
  102. const message = error.response.data.ocs.meta.message
  103. throw new Error(`${property}, ${message}`)
  104. }
  105. }
  106. }
  107. }