@@ -12,7 +12,8 @@ import {
1212 checkObject ,
1313 deleteObjects ,
1414 countObjectNameAppearances ,
15- renameObjects
15+ renameObjects ,
16+ copyObjects
1617} from './requests' ;
1718
1819let data : Contents . IModel = {
@@ -573,88 +574,83 @@ export class Drive implements Contents.IDrive {
573574 }
574575
575576 /**
576- * Copy a file into a given directory .
577+ * Helping function for copying an object .
577578 *
578- * @param path - The original file path.
579+ * @param copiedItemPath - The original file path.
579580 *
580- * @param toDir - The destination directory path .
581+ * @param toPath - The path where item will be copied .
581582 *
582- * @returns A promise which resolves with the new contents model when the
583+ * @param driveName - The name of the drive where content is moved.
584+ *
585+ * @returns A promise which resolves with the new name when the
583586 * file is copied.
584587 */
588+ async incrementCopyName (
589+ copiedItemPath : string ,
590+ toPath : string ,
591+ driveName : string
592+ ) {
593+ // extracting original file name
594+ const originalFileName = PathExt . basename ( copiedItemPath ) ;
585595
586- incrementCopyName ( contents : Contents . IModel , copiedItemPath : string ) : string {
587- const content : Array < Contents . IModel > = contents . content ;
588- let name : string = '' ;
589- let countText = 0 ;
590- let countDir = 0 ;
591- let countNotebook = 0 ;
592- let ext = undefined ;
593- const list1 = copiedItemPath . split ( '/' ) ;
594- const copiedItemName = list1 [ list1 . length - 1 ] ;
596+ // constructing new file name and path with -Copy string
597+ const newFileName =
598+ PathExt . extname ( originalFileName ) === ''
599+ ? originalFileName + '-Copy'
600+ : originalFileName . split ( '.' ) [ 0 ] +
601+ '-Copy.' +
602+ originalFileName . split ( '.' ) [ 1 ] ;
595603
596- const list2 = copiedItemName . split ( '.' ) ;
597- let rootName = list2 [ 0 ] ;
604+ const newFilePath = PathExt . join ( toPath , newFileName ) ;
605+ // copiedItemPath.substring(0, copiedItemPath.lastIndexOf('/') + 1) + newFileName ;
598606
599- content . forEach ( item => {
600- if ( item . name . includes ( rootName ) && item . name . includes ( '.txt' ) ) {
601- ext = '.txt' ;
602- if ( rootName . includes ( '-Copy' ) ) {
603- const list3 = rootName . split ( '-Copy' ) ;
604- countText = parseInt ( list3 [ 1 ] ) + 1 ;
605- rootName = list3 [ 0 ] ;
606- } else {
607- countText = countText + 1 ;
608- }
609- }
610- if ( item . name . includes ( rootName ) && item . name . includes ( '.ipynb' ) ) {
611- ext = '.ipynb' ;
612- if ( rootName . includes ( '-Copy' ) ) {
613- const list3 = rootName . split ( '-Copy' ) ;
614- countNotebook = parseInt ( list3 [ 1 ] ) + 1 ;
615- rootName = list3 [ 0 ] ;
616- } else {
617- countNotebook = countNotebook + 1 ;
618- }
619- } else if ( item . name . includes ( rootName ) ) {
620- if ( rootName . includes ( '-Copy' ) ) {
621- const list3 = rootName . split ( '-Copy' ) ;
622- countDir = parseInt ( list3 [ 1 ] ) + 1 ;
623- rootName = list3 [ 0 ] ;
624- } else {
625- countDir = countDir + 1 ;
626- }
627- }
628- } ) ;
607+ // getting incremented name of Copy in case of duplicates
608+ const incrementedName = await this . incrementName ( newFilePath , driveName ) ;
629609
630- if ( ext === '.txt' ) {
631- name = rootName + '-Copy' + countText + ext ;
632- }
633- if ( ext === 'ipynb' ) {
634- name = rootName + '-Copy' + countText + ext ;
635- } else if ( ext === undefined ) {
636- name = rootName + '-Copy' + countDir ;
637- }
638-
639- return name ;
610+ return incrementedName ;
640611 }
612+
613+ /**
614+ * Copy a file into a given directory.
615+ *
616+ * @param path - The original file path.
617+ *
618+ * @param toDir - The destination directory path.
619+ *
620+ * @returns A promise which resolves with the new contents model when the
621+ * file is copied.
622+ */
641623 async copy (
642- fromFile : string ,
624+ path : string ,
643625 toDir : string ,
644626 options : Contents . ICreateOptions = { }
645627 ) : Promise < Contents . IModel > {
646- /*const settings = this.serverSettings;
647- const url = this._getUrl(toDir);
648- const init = {
649- method: 'POST',
650- body: JSON.stringify({ copy_from: fromFile })
651- };
652- const response = await ServerConnection.makeRequest(url, init, settings);
653- if (response.status !== 201) {
654- const err = await ServerConnection.ResponseError.create(response);
655- throw err;
656- }
657- const data = await response.json();*/
628+ // extract current drive name
629+ const currentDrive = this . _drivesList . filter (
630+ x =>
631+ x . name ===
632+ ( path . indexOf ( '/' ) !== - 1 ? path . substring ( 0 , path . indexOf ( '/' ) ) : path )
633+ ) [ 0 ] ;
634+
635+ // eliminate drive name from path
636+ const relativePath =
637+ path . indexOf ( '/' ) !== - 1 ? path . substring ( path . indexOf ( '/' ) + 1 ) : '' ;
638+ const toRelativePath =
639+ toDir . indexOf ( '/' ) !== - 1 ? toDir . substring ( toDir . indexOf ( '/' ) + 1 ) : '' ;
640+
641+ // construct new file or directory name for the copy
642+ const newFileName = await this . incrementCopyName (
643+ relativePath ,
644+ toRelativePath ,
645+ currentDrive . name
646+ ) ;
647+
648+ data = await copyObjects ( currentDrive . name , {
649+ path : relativePath ,
650+ toPath : toRelativePath ,
651+ newFileName : newFileName ,
652+ registeredFileTypes : this . _registeredFileTypes
653+ } ) ;
658654
659655 this . _fileChanged . emit ( {
660656 type : 'new' ,
0 commit comments