Skip to content

Commit

Permalink
Merge pull request #847 from nextcloud-libraries/fix/handle-webdav-en…
Browse files Browse the repository at this point in the history
…dpoint
  • Loading branch information
skjnldsv authored Dec 21, 2023
2 parents 2feb511 + a5c7260 commit 2351742
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
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

0 comments on commit 2351742

Please sign in to comment.