Skip to content

Commit 6a3de02

Browse files
committed
refactor: @nextcloud/files is now aware of public shares also now have @nextcloud/sharing
Removes the explicit handling of public shares as we can now use the functions from files library. Signed-off-by: Ferdinand Thiessen <[email protected]>
1 parent 29ac805 commit 6a3de02

File tree

7 files changed

+49
-84
lines changed

7 files changed

+49
-84
lines changed

lib/components/FilePicker/FilePicker.vue

+2-8
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ import { showError } from '../../toast'
7777
import { useDAVFiles } from '../../composables/dav'
7878
import { useMimeFilter } from '../../composables/mime'
7979
import { useFilesSettings } from '../../composables/filesSettings'
80-
import { useIsPublic } from '../../composables/isPublic'
8180
import { t } from '../../utils/l10n'
8281

8382
const props = withDefaults(defineProps<{
@@ -142,11 +141,6 @@ const emit = defineEmits<{
142141
(e: 'close', v?: Node[]): void
143142
}>()
144143

145-
/**
146-
* Whether we are on a public endpoint (e.g. public share)
147-
*/
148-
const { isPublic } = useIsPublic()
149-
150144
const isOpen = ref(true)
151145

152146
/**
@@ -218,7 +212,7 @@ watch([navigatedPath], () => {
218212
/**
219213
* The current path that should be picked from
220214
*/
221-
const currentPath = computed(() =>
215+
const currentPath = computed(() =>
222216
// Only use the path for the files view as favorites and recent only works on the root
223217
currentView.value === 'files' ? navigatedPath.value || props.path || savedPath.value : '/',
224218
)
@@ -236,7 +230,7 @@ const {
236230
isLoading,
237231
loadFiles,
238232
createDirectory,
239-
} = useDAVFiles(currentView, currentPath, isPublic)
233+
} = useDAVFiles(currentView, currentPath)
240234

241235
onMounted(() => loadFiles())
242236

lib/composables/dav.spec.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const waitRefLoaded = (isLoading: Ref<boolean>) => new Promise((resolve) => {
3939
const TestComponent = defineComponent({
4040
props: ['currentView', 'currentPath', 'isPublic'],
4141
setup(props) {
42-
const dav = useDAVFiles(toRef(props, 'currentView'), toRef(props, 'currentPath'), toRef(props, 'isPublic'))
42+
const dav = useDAVFiles(toRef(props, 'currentView'), toRef(props, 'currentPath'))
4343
return {
4444
...dav,
4545
}
@@ -161,13 +161,13 @@ describe('dav composable', () => {
161161
nextcloudFiles.davGetClient.mockImplementation(() => client)
162162
nextcloudFiles.davResultToNode.mockImplementation((v) => v)
163163

164-
const { getFile } = useDAVFiles(ref('files'), ref('/'), ref(false))
164+
const { getFile } = useDAVFiles(ref('files'), ref('/'))
165165

166166
const node = await getFile('/some/path/file.ext')
167167
expect(node).toEqual({ path: `${nextcloudFiles.davRootPath}/some/path/file.ext` })
168168
// Check mock usage
169169
expect(client.stat).toBeCalledWith(`${nextcloudFiles.davRootPath}/some/path/file.ext`, { details: true })
170-
expect(nextcloudFiles.davResultToNode).toBeCalledWith({ path: `${nextcloudFiles.davRootPath}/some/path/file.ext` }, nextcloudFiles.davRootPath, nextcloudFiles.davRemoteURL)
170+
expect(nextcloudFiles.davResultToNode).toBeCalledWith({ path: `${nextcloudFiles.davRootPath}/some/path/file.ext` })
171171
})
172172

173173
it('createDirectory works', async () => {
@@ -178,7 +178,7 @@ describe('dav composable', () => {
178178
nextcloudFiles.davGetClient.mockImplementation(() => client)
179179
nextcloudFiles.davResultToNode.mockImplementation((v) => v)
180180

181-
const { createDirectory } = useDAVFiles(ref('files'), ref('/foo/'), ref(false))
181+
const { createDirectory } = useDAVFiles(ref('files'), ref('/foo/'))
182182

183183
const node = await createDirectory('my-name')
184184
expect(node).toEqual({ path: `${nextcloudFiles.davRootPath}/foo/my-name` })
@@ -197,7 +197,7 @@ describe('dav composable', () => {
197197

198198
const view = ref<'files' | 'recent' | 'favorites'>('files')
199199
const path = ref('/')
200-
const { loadFiles, isLoading } = useDAVFiles(view, path, ref(false))
200+
const { loadFiles, isLoading } = useDAVFiles(view, path)
201201

202202
expect(isLoading.value).toBe(true)
203203
await loadFiles()
@@ -224,7 +224,7 @@ describe('dav composable', () => {
224224

225225
const view = ref<'files' | 'recent' | 'favorites'>('files')
226226
const path = ref('/')
227-
const { loadFiles, isLoading } = useDAVFiles(view, path, ref(false))
227+
const { loadFiles, isLoading } = useDAVFiles(view, path)
228228

229229
const abort = vi.spyOn(AbortController.prototype, 'abort')
230230

lib/composables/dav.ts

+12-39
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,28 @@ import type { Folder, Node } from '@nextcloud/files'
66
import type { ComputedRef, Ref } from 'vue'
77
import type { FileStat, ResponseDataDetailed, SearchResult } from 'webdav'
88

9-
import { davGetClient, davGetDefaultPropfind, davGetRecentSearch, davRemoteURL, davResultToNode, davRootPath, getFavoriteNodes } from '@nextcloud/files'
10-
import { generateRemoteUrl } from '@nextcloud/router'
9+
import { davGetClient, davGetDefaultPropfind, davGetRecentSearch, davResultToNode, davRootPath, getFavoriteNodes } from '@nextcloud/files'
1110
import { join } from 'path'
12-
import { computed, onMounted, ref, shallowRef, watch } from 'vue'
11+
import { onMounted, ref, shallowRef, watch } from 'vue'
1312
import { CancelablePromise } from 'cancelable-promise'
1413

1514
/**
1615
* Handle file loading using WebDAV
1716
*
1817
* @param currentView Reference to the current files view
1918
* @param currentPath Reference to the current files path
20-
* @param isPublicEndpoint True if the public `public.php` WebDAV endpoint should be used instead of `remote.php`
2119
*/
2220
export const useDAVFiles = function(
2321
currentView: Ref<'files'|'recent'|'favorites'> | ComputedRef<'files'|'recent'|'favorites'>,
2422
currentPath: Ref<string> | ComputedRef<string>,
25-
isPublicEndpoint: Ref<boolean> | ComputedRef<boolean>,
2623
) {
2724

28-
const defaultRootPath = computed(() => isPublicEndpoint.value ? '/' : davRootPath)
29-
30-
const defaultRemoteUrl = computed(() => {
31-
if (isPublicEndpoint.value) {
32-
return generateRemoteUrl('webdav').replace('/remote.php', '/public.php')
33-
}
34-
return davRemoteURL
35-
})
36-
3725
/**
3826
* The WebDAV client
3927
*/
40-
const client = computed(() => {
41-
if (isPublicEndpoint.value) {
42-
const token = (document.getElementById('sharingToken')! as HTMLInputElement).value
43-
const authorization = btoa(`${token}:null`)
44-
45-
return davGetClient(defaultRemoteUrl.value, {
46-
Authorization: `Basic ${authorization}`,
47-
})
48-
}
49-
50-
return davGetClient()
51-
})
28+
const client = davGetClient()
5229

53-
const resultToNode = (result: FileStat) => davResultToNode(result, defaultRootPath.value, defaultRemoteUrl.value)
30+
const resultToNode = (result: FileStat) => davResultToNode(result)
5431

5532
const getRecentNodes = (): CancelablePromise<Node[]> => {
5633
const controller = new AbortController()
@@ -59,7 +36,7 @@ export const useDAVFiles = function(
5936
return new CancelablePromise(async (resolve, reject, onCancel) => {
6037
onCancel(() => controller.abort())
6138
try {
62-
const { data } = await client.value.search('/', {
39+
const { data } = await client.search('/', {
6340
signal: controller.signal,
6441
details: true,
6542
data: davGetRecentSearch(lastTwoWeek),
@@ -77,16 +54,14 @@ export const useDAVFiles = function(
7754
return new CancelablePromise(async (resolve, reject, onCancel) => {
7855
onCancel(() => controller.abort())
7956
try {
80-
const results = await client.value.getDirectoryContents(`${defaultRootPath.value}${currentPath.value}`, {
57+
const results = await client.getDirectoryContents(join(davRootPath, currentPath.value), {
8158
signal: controller.signal,
8259
details: true,
8360
data: davGetDefaultPropfind(),
8461
}) as ResponseDataDetailed<FileStat[]>
8562
let nodes = results.data.map(resultToNode)
8663
// Hack for the public endpoint which always returns folder itself
87-
if (isPublicEndpoint.value) {
88-
nodes = nodes.filter((file) => file.path !== currentPath.value)
89-
}
64+
nodes = nodes.filter((file) => file.path !== currentPath.value)
9065
resolve(nodes)
9166
} catch (error) {
9267
reject(error)
@@ -120,12 +95,12 @@ export const useDAVFiles = function(
12095
/**
12196
* Create a new directory in the current path
12297
* @param name Name of the new directory
123-
* @return The created directory
98+
* @return {Promise<Folder>} The created directory
12499
*/
125100
async function createDirectory(name: string): Promise<Folder> {
126101
const path = join(currentPath.value, name)
127102

128-
await client.value.createDirectory(join(defaultRootPath.value, path))
103+
await client.createDirectory(join(davRootPath, path))
129104
const directory = await getFile(path) as Folder
130105
files.value = [...files.value, directory]
131106
return directory
@@ -137,10 +112,8 @@ export const useDAVFiles = function(
137112
* @param path The path of the file or folder
138113
* @param rootPath DAV root path, defaults to '/files/USERID'
139114
*/
140-
async function getFile(path: string, rootPath: string|undefined = undefined) {
141-
rootPath = rootPath ?? defaultRootPath.value
142-
143-
const { data } = await client.value.stat(`${rootPath}${path}`, {
115+
async function getFile(path: string, rootPath: string = davRootPath) {
116+
const { data } = await client.stat(join(rootPath, path), {
144117
details: true,
145118
data: davGetDefaultPropfind(),
146119
}) as ResponseDataDetailed<FileStat>
@@ -157,7 +130,7 @@ export const useDAVFiles = function(
157130
isLoading.value = true
158131

159132
if (currentView.value === 'favorites') {
160-
promise.value = getFavoriteNodes(client.value, currentPath.value, defaultRootPath.value)
133+
promise.value = getFavoriteNodes(client, currentPath.value)
161134
} else if (currentView.value === 'recent') {
162135
promise.value = getRecentNodes()
163136
} else {

lib/composables/filesSettings.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import type { ComputedRef, Ref } from 'vue'
77

88
import { loadState } from '@nextcloud/initial-state'
99
import { generateUrl } from '@nextcloud/router'
10+
import { isPublicShare } from '@nextcloud/sharing/public'
1011
import { toValue } from '@vueuse/core'
1112
import { computed, onMounted, ref } from 'vue'
1213

1314
import axios from '@nextcloud/axios'
1415

1516
import { t } from '../utils/l10n'
1617
import { showError } from '../toast'
17-
import { useIsPublic } from './isPublic'
1818

1919
interface OCAFilesUserConfig {
2020
show_hidden: boolean
@@ -49,10 +49,8 @@ export const useFilesSettings = () => {
4949
const sortFavoritesFirst = ref(filesUserState?.sort_favorites_first ?? true)
5050
const cropImagePreviews = ref(filesUserState?.crop_image_previews ?? true)
5151

52-
const { isPublic } = useIsPublic()
53-
5452
onMounted(async () => {
55-
if (!isPublic.value) {
53+
if (!isPublicShare()) {
5654
try {
5755
const { data } = await axios.get(generateUrl('/apps/files/api/v1/configs'))
5856

@@ -97,10 +95,8 @@ export const useFilesViews = (currentView?: FileListViews|Ref<FileListViews>|Com
9795
order: convertOrder(filesViewsState?.favorites?.sorting_direction ?? 'asc'),
9896
})
9997

100-
const { isPublic } = useIsPublic()
101-
10298
onMounted(async () => {
103-
if (!isPublic.value) {
99+
if (!isPublicShare()) {
104100
try {
105101
const { data } = await axios.get(generateUrl('/apps/files/api/v1/views'))
106102
filesViewConfig.value = {

lib/composables/isPublic.ts

-19
This file was deleted.

package-lock.json

+24-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@
5858
"@nextcloud/auth": "^2.3.0",
5959
"@nextcloud/axios": "^2.5.0",
6060
"@nextcloud/event-bus": "^3.3.1",
61-
"@nextcloud/files": "^3.4.1",
61+
"@nextcloud/files": "^3.5.1",
6262
"@nextcloud/initial-state": "^2.2.0",
6363
"@nextcloud/l10n": "^3.1.0",
6464
"@nextcloud/router": "^3.0.1",
65+
"@nextcloud/sharing": "^0.2.2",
6566
"@nextcloud/typings": "^1.8.0",
6667
"@types/toastify-js": "^1.12.3",
6768
"@vueuse/core": "^10.11.0",

0 commit comments

Comments
 (0)