@@ -39,6 +39,7 @@ import FolderMoveSvg from '@mdi/svg/svg/folder-move.svg?raw'
3939
4040import { MoveCopyAction , canCopy , canMove , getQueue } from './moveOrCopyActionUtils'
4141import logger from '../logger'
42+ import { getUniqueName } from '../utils/fileUtils'
4243
4344/**
4445 * Return the action that is possible for the given nodes
@@ -67,30 +68,6 @@ const getActionForNodes = (nodes: Node[]): MoveCopyAction => {
6768 * @return {Promise<void> } A promise that resolves when the copy/move is done
6869 */
6970export const handleCopyMoveNodeTo = async ( node : Node , destination : Folder , method : MoveCopyAction . COPY | MoveCopyAction . MOVE , overwrite = false ) => {
70- /**
71- * Create an unique name for a node
72- * @param node Node that is copied
73- * @param otherNodes Other nodes in the target directory to check for unique name
74- * @return Either the node basename, if unique, or the name with a `(copy N)` suffix that is unique
75- */
76- const makeUniqueName = ( node : Node , otherNodes : Node [ ] | FileStat [ ] ) => {
77- const basename = node . basename . slice ( 0 , node . basename . lastIndexOf ( '.' ) )
78- let index = 0
79-
80- const currentName = ( ) => {
81- switch ( index ) {
82- case 0 : return node . basename
83- case 1 : return `${ basename } (copy)${ node . extension ?? '' } `
84- default : return `${ basename } ${ t ( 'files' , '(copy %n)' , undefined , index ) } ${ node . extension ?? '' } ` // TRANSLATORS: Meaning it is the n'th copy of a file
85- }
86- }
87-
88- while ( otherNodes . some ( ( other : Node | FileStat ) => currentName ( ) === other . basename ) ) {
89- index += 1
90- }
91- return currentName ( )
92- }
93-
9471 if ( ! destination ) {
9572 return
9673 }
@@ -122,6 +99,13 @@ export const handleCopyMoveNodeTo = async (node: Node, destination: Folder, meth
12299
123100 const queue = getQueue ( )
124101 return await queue . add ( async ( ) => {
102+ const copySuffix = ( index : number ) => {
103+ if ( index === 1 ) {
104+ return t ( 'files' , '(copy)' ) // TRANSLATORS: Mark a file as a copy of another file
105+ }
106+ return t ( 'files' , '(copy %n)' , undefined , index ) // TRANSLATORS: Meaning it is the n'th copy of a file
107+ }
108+
125109 try {
126110 const client = davGetClient ( )
127111 const currentPath = join ( davRootPath , node . path )
@@ -132,7 +116,7 @@ export const handleCopyMoveNodeTo = async (node: Node, destination: Folder, meth
132116 // If we do not allow overwriting then find an unique name
133117 if ( ! overwrite ) {
134118 const otherNodes = await client . getDirectoryContents ( destinationPath ) as FileStat [ ]
135- target = makeUniqueName ( node , otherNodes )
119+ target = getUniqueName ( node . basename , otherNodes . map ( ( n ) => n . basename ) , copySuffix )
136120 }
137121 await client . copyFile ( currentPath , join ( destinationPath , target ) )
138122 // If the node is copied into current directory the view needs to be updated
0 commit comments