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

[stable28] fix(files): Only add copy suffix before file extension for files (not folders) #44434

Merged
merged 2 commits into from
Apr 18, 2024
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
11 changes: 9 additions & 2 deletions apps/files/src/actions/moveOrCopyAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,14 @@ export const handleCopyMoveNodeTo = async (node: Node, destination: Folder, meth
// If we do not allow overwriting then find an unique name
if (!overwrite) {
const otherNodes = await client.getDirectoryContents(destinationPath) as FileStat[]
target = getUniqueName(node.basename, otherNodes.map((n) => n.basename), copySuffix)
target = getUniqueName(
node.basename,
otherNodes.map((n) => n.basename),
{
suffix: copySuffix,
ignoreFileExtension: node.type === FileType.Folder,
},
)
}
await client.copyFile(currentPath, join(destinationPath, target))
// If the node is copied into current directory the view needs to be updated
Expand Down Expand Up @@ -150,7 +157,7 @@ export const handleCopyMoveNodeTo = async (node: Node, destination: Folder, meth
}
} catch (error) {
// User cancelled
showError(t('files','Move cancelled'))
showError(t('files', 'Move cancelled'))
return
}
}
Expand Down
26 changes: 21 additions & 5 deletions apps/files/src/utils/fileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
Expand All @@ -28,15 +28,31 @@ import { basename, extname } from 'path'
* Create an unique file name
* @param name The initial name to use
* @param otherNames Other names that are already used
* @param suffix A function that takes an index an returns a suffix to add, defaults to '(index)'
* @param options Optional parameters for tuning the behavior
* @param options.suffix A function that takes an index and returns a suffix to add to the file name, defaults to '(index)'
* @param options.ignoreFileExtension Set to true to ignore the file extension when adding the suffix (when getting a unique directory name)
* @return Either the initial name, if unique, or the name with the suffix so that the name is unique
*/
export const getUniqueName = (name: string, otherNames: string[], suffix = (n: number) => `(${n})`): string => {
export const getUniqueName = (
name: string,
otherNames: string[],
options: {
suffix?: (i: number) => string,
ignoreFileExtension?: boolean,
} = {},
): string => {
const opts = {
suffix: (n: number) => `(${n})`,
ignoreFileExtension: false,
...options,
}

let newName = name
let i = 1
while (otherNames.includes(newName)) {
const ext = extname(name)
newName = `${basename(name, ext)} ${suffix(i++)}${ext}`
const ext = opts.ignoreFileExtension ? '' : extname(name)
const base = basename(name, ext)
newName = `${base} ${opts.suffix(i++)}${ext}`
}
return newName
}
Expand Down
28 changes: 27 additions & 1 deletion cypress/e2e/files/files_copy-move.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ describe('Files: Move or copy files', { testIsolation: true }, () => {
getRowForFile('new-folder').should('not.exist')
})

// This was a bug previously
/**
* Test for https://github.com/nextcloud/server/issues/41768
*/
it('Can move a file to folder with similar name', () => {
cy.uploadContent(currentUser, new Blob(), 'text/plain', '/original')
.mkdir(currentUser, '/original folder')
Expand Down Expand Up @@ -180,6 +182,30 @@ describe('Files: Move or copy files', { testIsolation: true }, () => {
getRowForFile('original (copy 2).txt').should('be.visible')
})

/**
* Test that a copied folder with a dot will be renamed correctly ('foo.bar' -> 'foo.bar (copy)')
* Test for: https://github.com/nextcloud/server/issues/43843
*/
it('Can copy a folder to same folder', () => {
cy.mkdir(currentUser, '/foo.bar')
cy.login(currentUser)
cy.visit('/apps/files')

// intercept the copy so we can wait for it
cy.intercept('COPY', /\/remote.php\/dav\/files\//).as('copyFile')

getRowForFile('foo.bar').should('be.visible')
triggerActionForFile('foo.bar', 'move-copy')

// click copy
cy.get('.file-picker').contains('button', 'Copy').should('be.visible').click()

cy.wait('@copyFile')

getRowForFile('foo.bar').should('be.visible')
getRowForFile('foo.bar (copy)').should('be.visible')
})

/** Test for https://github.com/nextcloud/server/issues/43329 */
context('escaping file and folder names', () => {
it('Can handle files with special characters', () => {
Expand Down
4 changes: 2 additions & 2 deletions dist/files-init.js

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions dist/files-init.js.LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,28 @@
*
*/

/**
* @copyright Copyright (c) 2021 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

/**
* @copyright Copyright (c) 2022 John Molakvoæ <skjnldsv@protonmail.com>
*
Expand Down
2 changes: 1 addition & 1 deletion dist/files-init.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/files-main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/files-main.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/files_versions-files_versions.js.LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
Expand Down
2 changes: 1 addition & 1 deletion dist/files_versions-files_versions.js.map

Large diffs are not rendered by default.

Loading