Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion __mocks__/@nextcloud/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
37 changes: 34 additions & 3 deletions __tests__/dav/dav.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})

Expand Down Expand Up @@ -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')
Expand All @@ -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')
})
Expand Down Expand Up @@ -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', () => {
Expand Down
9 changes: 8 additions & 1 deletion lib/dav/dav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Loading