-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1378 from nextcloud-libraries/fix/current-folder
fix(FilePicker): Cleanup DAV handling and properly handle `currentFolder`
- Loading branch information
Showing
5 changed files
with
144 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
||
const nextcloudFiles = vi.hoisted(() => ({ | ||
davResultToNode: vi.fn((v) => v), | ||
davGetDefaultPropfind: vi.fn(() => 'propfind content'), | ||
davRootPath: '/root/path', | ||
})) | ||
vi.mock('@nextcloud/files', () => nextcloudFiles) | ||
|
||
describe('DAV utils', () => { | ||
beforeEach(() => { | ||
vi.resetModules() | ||
}) | ||
|
||
it('getFile works', async () => { | ||
const client = { | ||
stat: vi.fn((v) => Promise.resolve({ data: { path: v } })), | ||
getDirectoryContents: vi.fn(() => ({ data: [] })), | ||
} | ||
|
||
const { getFile } = await import('./dav') | ||
|
||
const node = await getFile(client, '/some/path/file.ext') | ||
expect(node).toEqual({ path: `${nextcloudFiles.davRootPath}/some/path/file.ext` }) | ||
// Check mock usage | ||
expect(client.stat).toBeCalledWith(`${nextcloudFiles.davRootPath}/some/path/file.ext`, { details: true, data: 'propfind content' }) | ||
expect(nextcloudFiles.davResultToNode).toBeCalledWith({ path: `${nextcloudFiles.davRootPath}/some/path/file.ext` }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import type { ContentsWithRoot, Node } from '@nextcloud/files' | ||
import type { FileStat, ResponseDataDetailed, SearchResult, WebDAVClient } from 'webdav' | ||
|
||
import { davGetDefaultPropfind, davGetRecentSearch, davResultToNode, davRootPath } from '@nextcloud/files' | ||
import { CancelablePromise } from 'cancelable-promise' | ||
import { join } from 'node:path' | ||
|
||
/** | ||
* Get the recently changed nodes from the last two weeks | ||
* @param client The WebDAV client | ||
*/ | ||
export function getRecentNodes(client: WebDAVClient): CancelablePromise<Node[]> { | ||
const controller = new AbortController() | ||
// unix timestamp in seconds, two weeks ago | ||
const lastTwoWeek = Math.round(Date.now() / 1000) - (60 * 60 * 24 * 14) | ||
return new CancelablePromise(async (resolve, reject, onCancel) => { | ||
onCancel(() => controller.abort()) | ||
try { | ||
const { data } = await client.search('/', { | ||
signal: controller.signal, | ||
details: true, | ||
data: davGetRecentSearch(lastTwoWeek), | ||
}) as ResponseDataDetailed<SearchResult> | ||
const nodes = data.results.map((result: FileStat) => davResultToNode(result)) | ||
resolve(nodes) | ||
} catch (error) { | ||
reject(error) | ||
} | ||
}) | ||
} | ||
|
||
/** | ||
* Get the directory content | ||
* @param client The WebDAV client | ||
* @param directoryPath The path to fetch | ||
*/ | ||
export function getNodes(client: WebDAVClient, directoryPath: string): CancelablePromise<ContentsWithRoot> { | ||
const controller = new AbortController() | ||
return new CancelablePromise(async (resolve, reject, onCancel) => { | ||
onCancel(() => controller.abort()) | ||
try { | ||
const results = await client.getDirectoryContents(join(davRootPath, directoryPath), { | ||
signal: controller.signal, | ||
details: true, | ||
includeSelf: true, | ||
data: davGetDefaultPropfind(), | ||
}) as ResponseDataDetailed<FileStat[]> | ||
const nodes = results.data.map((result: FileStat) => davResultToNode(result)) | ||
resolve({ | ||
contents: nodes.filter(({ path }) => path !== directoryPath), | ||
folder: nodes.find(({ path }) => path === directoryPath), | ||
}) | ||
} catch (error) { | ||
reject(error) | ||
} | ||
}) | ||
} | ||
|
||
/** | ||
* Get information for one file | ||
* | ||
* @param client The WebDAV client | ||
* @param path The path of the file or folder | ||
*/ | ||
export async function getFile(client: WebDAVClient, path: string) { | ||
const { data } = await client.stat(join(davRootPath, path), { | ||
details: true, | ||
data: davGetDefaultPropfind(), | ||
}) as ResponseDataDetailed<FileStat> | ||
return davResultToNode(data) | ||
} |