diff options
author | Ferdinand Thiessen <opensource@fthiessen.de> | 2024-03-22 18:30:52 +0100 |
---|---|---|
committer | Ferdinand Thiessen <opensource@fthiessen.de> | 2024-03-22 19:13:48 +0100 |
commit | b0b2811211658f5977e86c225f9026fcc03893a5 (patch) | |
tree | 108158e887b89d629a6ef12bd9c091c502c51396 /apps/files | |
parent | f3f73ba25552b030d4634a63a3ccc06e59d7bb77 (diff) | |
download | nextcloud-server-b0b2811211658f5977e86c225f9026fcc03893a5.tar.gz nextcloud-server-b0b2811211658f5977e86c225f9026fcc03893a5.zip |
fix(files): When copying nodes only add the copy suffix for file before file extension
Co-authored-by: Pytal <24800714+Pytal@users.noreply.github.com>
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
Diffstat (limited to 'apps/files')
-rw-r--r-- | apps/files/src/actions/moveOrCopyAction.ts | 11 | ||||
-rw-r--r-- | apps/files/src/utils/fileUtils.ts | 26 |
2 files changed, 30 insertions, 7 deletions
diff --git a/apps/files/src/actions/moveOrCopyAction.ts b/apps/files/src/actions/moveOrCopyAction.ts index 1483568ec0e..61be2d946ac 100644 --- a/apps/files/src/actions/moveOrCopyAction.ts +++ b/apps/files/src/actions/moveOrCopyAction.ts @@ -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 @@ -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 } } diff --git a/apps/files/src/utils/fileUtils.ts b/apps/files/src/utils/fileUtils.ts index 1f7e3078da9..fe94fc9fb43 100644 --- a/apps/files/src/utils/fileUtils.ts +++ b/apps/files/src/utils/fileUtils.ts @@ -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 @@ -28,15 +28,31 @@ import { translate as t, translatePlural as n } from '@nextcloud/l10n' * 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 } |