Skip to content

Commit

Permalink
fix: Ensure displayname is a string
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>

[skip ci]
  • Loading branch information
susnux authored and skjnldsv committed Aug 2, 2024
1 parent ec4ec05 commit 7083ee9
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 58 deletions.
20 changes: 18 additions & 2 deletions apps/files/src/components/FileEntryMixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,24 @@ export default defineComponent({
* Either the nodes filename or a custom display name (e.g. for shares)
*/
displayName() {
const ext = this.extension
const name = String(this.source.attributes.displayname || this.source.basename)
return this.source.displayname
},
/**
* The display name without extension
*/
basename() {
if (this.extension === '') {
return this.displayName
}
return this.displayName.slice(0, 0 - this.extension.length)
},
/**
* The extension of the file
*/
extension() {
if (this.source.type === FileType.Folder) {
return ''
}

return extname(this.displayName)
},
Expand Down
6 changes: 6 additions & 0 deletions apps/files/src/services/Files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ export const resultToNode = function(node: FileStat): File | Folder {
? hashCode(source)
: props?.fileid as number || 0

// TODO remove this hack with nextcloud-files v3.7
// just needed because of a bug in the webdav client
if (node.props?.displayname !== undefined) {
node.props.displayname = String(node.props.displayname)
}

const nodeData = {
id,
source,
Expand Down
36 changes: 1 addition & 35 deletions apps/files/src/views/FilesList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ import type { UserConfig } from '../types.ts'
import { getCapabilities } from '@nextcloud/capabilities'
import { showError } from '@nextcloud/dialogs'
import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus'
import { Folder, Node, Permission } from '@nextcloud/files'
import { Folder, Node, Permission, sortNodes } from '@nextcloud/files'
import { loadState } from '@nextcloud/initial-state'
import { translate as t, translatePlural as n } from '@nextcloud/l10n'
import { ShareType } from '@nextcloud/sharing'
Expand Down Expand Up @@ -272,40 +272,6 @@ export default defineComponent({
return this.filesStore.getNode(source) as Folder
},

/**
* Directory content sorting parameters
* Provided by an extra computed property for caching
*/
sortingParameters() {
const identifiers = [
// 1: Sort favorites first if enabled
...(this.userConfig.sort_favorites_first ? [v => v.attributes?.favorite !== 1] : []),
// 2: Sort folders first if sorting by name
...(this.userConfig.sort_folders_first ? [v => v.type !== 'folder'] : []),
// 3: Use sorting mode if NOT basename (to be able to use displayName too)
...(this.sortingMode !== 'basename' ? [v => v[this.sortingMode]] : []),
// 4: Use displayname if available, fallback to name
v => v.attributes?.displayname || v.basename,
// 5: Finally, use basename if all previous sorting methods failed
v => v.basename,
]
const orders = [
// (for 1): always sort favorites before normal files
...(this.userConfig.sort_favorites_first ? ['asc'] : []),
// (for 2): always sort folders before files
...(this.userConfig.sort_folders_first ? ['asc'] : []),
// (for 3): Reverse if sorting by mtime as mtime higher means edited more recent -> lower
...(this.sortingMode === 'mtime' ? [this.isAscSorting ? 'desc' : 'asc'] : []),
// (also for 3 so make sure not to conflict with 2 and 3)
...(this.sortingMode !== 'mtime' && this.sortingMode !== 'basename' ? [this.isAscSorting ? 'asc' : 'desc'] : []),
// for 4: use configured sorting direction
this.isAscSorting ? 'asc' : 'desc',
// for 5: use configured sorting direction
this.isAscSorting ? 'asc' : 'desc',
]
return [identifiers, orders] as const
},

/**
* The current directory contents.
*/
Expand Down
25 changes: 25 additions & 0 deletions cypress/e2e/files/files_sorting.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,31 @@ describe('Files: Sorting the file list', { testIsolation: true }, () => {
})
})

/**
* Regression test of https://github.com/nextcloud/server/issues/45829
*/
it('Filesnames with numbers are sorted by name ascending by default', () => {
cy.uploadContent(currentUser, new Blob(), 'text/plain', '/name.txt')
.uploadContent(currentUser, new Blob(), 'text/plain', '/name_03.txt')
.uploadContent(currentUser, new Blob(), 'text/plain', '/name_02.txt')
.uploadContent(currentUser, new Blob(), 'text/plain', '/name_01.txt')
cy.login(currentUser)
cy.visit('/apps/files')

cy.get('[data-cy-files-list-row]').each(($row, index) => {
switch (index) {
case 0: expect($row.attr('data-cy-files-list-row-name')).to.eq('name.txt')
break
case 1: expect($row.attr('data-cy-files-list-row-name')).to.eq('name_01.txt')
break
case 2: expect($row.attr('data-cy-files-list-row-name')).to.eq('name_02.txt')
break
case 3: expect($row.attr('data-cy-files-list-row-name')).to.eq('name_03.txt')
break
}
})
})

it('Can sort by size', () => {
cy.uploadContent(currentUser, new Blob(), 'text/plain', '/1 tiny.txt')
.uploadContent(currentUser, new Blob(['a'.repeat(1024)]), 'text/plain', '/z big.txt')
Expand Down
120 changes: 99 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7083ee9

Please sign in to comment.