Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Node): Handle slash as root path for public webdav endpoint #847

Merged
merged 1 commit into from
Dec 21, 2023
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
12 changes: 12 additions & 0 deletions __tests__/files/node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,18 @@ describe('Root and paths detection', () => {
expect(file.dirname).toBe('/Photos')
})

test('Root with public webdav endpoint', () => {
const file = new File({
source: 'https://cloud.domain.com/public.php/webdav/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
root: '/',
})
expect(file.root).toBe('/')
expect(file.dirname).toBe('/Photos')
expect(file.path).toBe('/Photos/picture.jpg')
})

test('Root with ending slash is removed', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
Expand Down
22 changes: 18 additions & 4 deletions lib/files/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,16 @@ export abstract class Node {
*/
get dirname(): string {
if (this.root) {
let source = this.source
if (this.isDavRessource) {
// ensure we only work on the real path in case root is not distinct
source = source.split(this._knownDavService).pop()!
}
// Using replace would remove all part matching root
const firstMatch = this.source.indexOf(this.root)
return dirname(this.source.slice(firstMatch + this.root.length) || '/')
const firstMatch = source.indexOf(this.root)
// Ensure we do not remove the leading slash
const root = this.root.replace(/\/$/, '')
return dirname(source.slice(firstMatch + root.length) || '/')
}

// This should always be a valid URL
Expand Down Expand Up @@ -219,9 +226,16 @@ export abstract class Node {
*/
get path(): string {
if (this.root) {
let source = this.source
if (this.isDavRessource) {
// ensure we only work on the real path in case root is not distinct
source = source.split(this._knownDavService).pop()!
}
// Using replace would remove all part matching root
const firstMatch = this.source.indexOf(this.root)
return this.source.slice(firstMatch + this.root.length) || '/'
const firstMatch = source.indexOf(this.root)
// Ensure we do not remove the leading slash
const root = this.root.replace(/\/$/, '')
return source.slice(firstMatch + root.length) || '/'
}
return (this.dirname + '/' + this.basename).replace(/\/\//g, '/')
}
Expand Down
Loading