You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

externalStorage.ts 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com>
  3. *
  4. * @author John Molakvoæ <skjnldsv@protonmail.com>
  5. *
  6. * @license AGPL-3.0-or-later
  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. // eslint-disable-next-line n/no-extraneous-import
  23. import type { AxiosResponse } from 'axios'
  24. import type { ContentsWithRoot } from '@nextcloud/files'
  25. import type { OCSResponse } from '@nextcloud/typings/ocs'
  26. import { Folder, Permission } from '@nextcloud/files'
  27. import { generateOcsUrl, generateRemoteUrl, generateUrl } from '@nextcloud/router'
  28. import { getCurrentUser } from '@nextcloud/auth'
  29. import axios from '@nextcloud/axios'
  30. import { STORAGE_STATUS } from '../utils/credentialsUtils'
  31. export const rootPath = `/files/${getCurrentUser()?.uid}`
  32. export type StorageConfig = {
  33. applicableUsers?: string[]
  34. applicableGroups?: string[]
  35. authMechanism: string
  36. backend: string
  37. backendOptions: Record<string, string>
  38. can_edit: boolean
  39. id: number
  40. mountOptions?: Record<string, string>
  41. mountPoint: string
  42. priority: number
  43. status: number
  44. statusMessage: string
  45. type: 'system' | 'user'
  46. userProvided: boolean
  47. }
  48. /**
  49. * https://github.com/nextcloud/server/blob/ac2bc2384efe3c15ff987b87a7432bc60d545c67/apps/files_external/lib/Controller/ApiController.php#L71-L97
  50. */
  51. export type MountEntry = {
  52. name: string
  53. path: string,
  54. type: 'dir',
  55. backend: 'SFTP',
  56. scope: 'system' | 'personal',
  57. permissions: number,
  58. id: number,
  59. class: string
  60. config: StorageConfig
  61. }
  62. const entryToFolder = (ocsEntry: MountEntry): Folder => {
  63. const path = (ocsEntry.path + '/' + ocsEntry.name).replace(/^\//gm, '')
  64. return new Folder({
  65. id: ocsEntry.id,
  66. source: generateRemoteUrl('dav' + rootPath + '/' + path),
  67. root: rootPath,
  68. owner: getCurrentUser()?.uid || null,
  69. permissions: ocsEntry.config.status !== STORAGE_STATUS.SUCCESS
  70. ? Permission.NONE
  71. : ocsEntry?.permissions || Permission.READ,
  72. attributes: {
  73. displayName: path,
  74. ...ocsEntry,
  75. },
  76. })
  77. }
  78. export const getContents = async (): Promise<ContentsWithRoot> => {
  79. const response = await axios.get(generateOcsUrl('apps/files_external/api/v1/mounts')) as AxiosResponse<OCSResponse<MountEntry[]>>
  80. const contents = response.data.ocs.data.map(entryToFolder)
  81. return {
  82. folder: new Folder({
  83. id: 0,
  84. source: generateRemoteUrl('dav' + rootPath),
  85. root: rootPath,
  86. owner: getCurrentUser()?.uid || null,
  87. permissions: Permission.READ,
  88. }),
  89. contents,
  90. }
  91. }
  92. export const getStatus = function(id: number, global = true) {
  93. const type = global ? 'userglobalstorages' : 'userstorages'
  94. return axios.get(generateUrl(`apps/files_external/${type}/${id}?testOnly=false`)) as Promise<AxiosResponse<StorageConfig>>
  95. }