Skip to content

Commit

Permalink
fix: Node.move should also adjust the displayname
Browse files Browse the repository at this point in the history
The displayname is always set by Nextcloud,
so if it is not a custom one it is set to the basename.
This means on move (and rename) we need to update the displayname.

This is an alternative to nextcloud/server#46474

We should consider making the displayname a top-level attribute like basename.

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Jul 15, 2024
1 parent b9ff668 commit d4e9c31
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
65 changes: 65 additions & 0 deletions __tests__/files/node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,71 @@ describe('Root and paths detection', () => {
})
})

describe('Move and rename of a node', () => {

test('Move updates the basename', () => {
const file = new File({
source: 'https://cloud.example.com/dav/files/images/emma.jpeg',
mime: 'image/jpeg',
owner: 'emma',
root: '/dav',
})

expect(file.basename).toBe('emma.jpeg')
file.move('https://cloud.example.com/dav/files/images/jane.jpeg')
expect(file.basename).toBe('jane.jpeg')
})

test('Move updates the path', () => {
const file = new File({
source: 'https://cloud.example.com/dav/files/images/emma.jpeg',
mime: 'image/jpeg',
owner: 'emma',
root: '/dav',
})

expect(file.path).toBe('/files/images/emma.jpeg')
file.move('https://cloud.example.com/dav/files/pictures/emma.jpeg')
expect(file.path).toBe('/files/pictures/emma.jpeg')
})

test('Move updates the fallback displayname', () => {
const file = new File({
source: 'https://cloud.example.com/dav/files/images/emma.jpeg',
mime: 'image/jpeg',
owner: 'emma',
root: '/dav',
attributes: {
displayname: 'emma.jpeg',
},
})

expect(file.path).toBe('/files/images/emma.jpeg')
expect(file.attributes.displayname).toBe('emma.jpeg')
file.move('https://cloud.example.com/dav/files/pictures/jane.jpeg')
expect(file.path).toBe('/files/pictures/jane.jpeg')
expect(file.attributes.displayname).toBe('jane.jpeg')
})

test('Move does not updates custom displayname', () => {
const file = new File({
source: 'https://cloud.example.com/dav/files/images/emma.jpeg',
mime: 'image/jpeg',
owner: 'emma',
root: '/dav',
attributes: {
displayname: 'profile.jpeg',
},
})

expect(file.path).toBe('/files/images/emma.jpeg')
expect(file.attributes.displayname).toBe('profile.jpeg')
file.move('https://cloud.example.com/dav/files/pictures/jane.jpeg')
expect(file.path).toBe('/files/pictures/jane.jpeg')
expect(file.attributes.displayname).toBe('profile.jpeg')
})
})

describe('Undefined properties are allowed', () => {
test('File', () => {
expect(() => new File({
Expand Down
11 changes: 11 additions & 0 deletions lib/files/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,18 @@ export abstract class Node {
*/
move(destination: string) {
validateData({ ...this._data, source: destination }, this._knownDavService)
const oldBasename = this.basename

this._data.source = destination
// Check if the displayname and the (old) basename were the same
// meaning no special displayname was set but just a fallback to the basename by Nextclouds WebDAV server
if (this._attributes.displayname
&& this._attributes.displayname === oldBasename
&& this.basename !== oldBasename) {
// We have to assume that the displayname was not set but just a copy of the basename
// this can not be guaranteed, so to be sure users should better refetch the node
this._attributes.displayname = this.basename
}
this.updateMtime()
}

Expand Down

0 comments on commit d4e9c31

Please sign in to comment.