diff --git a/__mocks__/@nextcloud/router.js b/__mocks__/@nextcloud/router.js index 57dbea150..77f8da2d0 100644 --- a/__mocks__/@nextcloud/router.js +++ b/__mocks__/@nextcloud/router.js @@ -6,4 +6,4 @@ /** * @param {string} path The path */ -export const generateRemoteUrl = (path) => `https://localhost/${path}` +export const generateRemoteUrl = (path) => `https://localhost/remote.php/${path}` diff --git a/__tests__/dav/dav.spec.ts b/__tests__/dav/dav.spec.ts index 321afe1b2..1ce86dd7c 100644 --- a/__tests__/dav/dav.spec.ts +++ b/__tests__/dav/dav.spec.ts @@ -28,7 +28,7 @@ describe('DAV functions', () => { }) test('remote url is correct', () => { - expect(defaultRemoteURL).toBe('https://localhost/dav') + expect(defaultRemoteURL).toBe('https://localhost/remote.php/dav') }) }) @@ -63,7 +63,7 @@ describe('resultToNode', () => { expect(node.basename).toBe(result.basename) expect(node.displayname).toBe(result.props!.displayname) expect(node.extension).toBe('.md') - expect(node.source).toBe('https://localhost/dav/files/test/New folder/Neue Textdatei.md') + expect(node.source).toBe('https://localhost/remote.php/dav/files/test/New folder/Neue Textdatei.md') expect(node.root).toBe(defaultRootPath) expect(node.path).toBe('/New folder/Neue Textdatei.md') expect(node.dirname).toBe('/New folder') @@ -78,7 +78,7 @@ describe('resultToNode', () => { expect(node.basename).toBe(remoteResult.basename) expect(node.extension).toBe('.md') expect(node.root).toBe('/root') - expect(node.source).toBe('https://localhost/dav/root/New folder/Neue Textdatei.md') + expect(node.source).toBe('https://localhost/remote.php/dav/root/New folder/Neue Textdatei.md') expect(node.path).toBe('/New folder/Neue Textdatei.md') expect(node.dirname).toBe('/New folder') }) @@ -169,6 +169,37 @@ describe('resultToNode', () => { expect(node2.mtime).toBeUndefined() expect(node2.crtime).toBeUndefined() }) + + test('source strips remote URL path from filename (search result)', () => { + const searchResult: FileStat = { + ...result, + filename: '/remote.php/dav/files/test/New folder/Neue Textdatei.md', + } + const node = resultToNode(searchResult) + expect(node.source).toBe(`${defaultRemoteURL}/files/test/New folder/Neue Textdatei.md`) + }) + + test('source strips remote URL path from filename (search result) with custom remote URL and non-empty root path', () => { + const searchResult: FileStat = { + ...result, + filename: '/root-base/remote.php/dav/files/test/New folder/Neue Textdatei.md', + } + const node = resultToNode(searchResult, '/files/test', 'http://example.com/root-base/remote.php/dav') + expect(node.source).toBe('http://example.com/root-base/remote.php/dav/files/test/New folder/Neue Textdatei.md') + }) + + test('source strips remote URL path from filename (search result) with getRootURL used as remote URL', () => { + /* + * This is not a valid resultToNode usage because getRootURL is not a valid remote URL. + * However, it was used in several places with SEARCH result as a workaround for the SEARCH request filename format. + */ + const searchResult: FileStat = { + ...result, + filename: '/root-base/remote.php/dav/files/test/New folder/Neue Textdatei.md', + } + const node = resultToNode(searchResult, '/files/test', 'http://example.com/root-base') + expect(node.source).toBe('http://example.com/root-base/remote.php/dav/files/test/New folder/Neue Textdatei.md') + }) }) describe('DAV requests', () => { diff --git a/lib/dav/dav.ts b/lib/dav/dav.ts index 7f5dff49b..d52fc6cca 100644 --- a/lib/dav/dav.ts +++ b/lib/dav/dav.ts @@ -175,9 +175,16 @@ export const resultToNode = function(node: FileStat, filesRoot = defaultRootPath const mtime = new Date(Date.parse(node.lastmod)) const crtime = new Date(Date.parse(props.creationdate)) + // Some DAV endpoints return a full path in the filename including root (base) path and remote URL path + // For example, SEARCH request returns "/root-base/remote.php/dav/files/user/test.txt" instead of "/files/user/test.txt" + // Strip the remote URL path to normalize the source + const remotePath = new URL(remoteURL).pathname.replace(/\/$/, '') + const filenameWithoutRemotePath = node.filename.startsWith(remotePath) ? node.filename.slice(remotePath.length) : node.filename + const source = remoteURL + filenameWithoutRemotePath + const nodeData: NodeData = { id, - source: `${remoteURL}${node.filename}`, + source, mtime: !isNaN(mtime.getTime()) && mtime.getTime() !== 0 ? mtime : undefined, crtime: !isNaN(crtime.getTime()) && crtime.getTime() !== 0 ? crtime : undefined, mime: node.mime || 'application/octet-stream',