@@ -98,13 +98,7 @@ export class FileMock extends File {
9898 * @return Returns a Promise that resolves to the new Entry object or rejects with an error.
9999 */
100100 copyDir ( path : string , dirName : string , newPath : string , newDirName : string ) : Promise < Entry > {
101- return this . resolveDirectoryUrl ( path ) . then ( ( fse ) => {
102- return this . getDirectory ( fse , dirName , { create : false } ) ;
103- } ) . then ( ( srcde ) => {
104- return this . resolveDirectoryUrl ( newPath ) . then ( ( deste ) => {
105- return this . copyMock ( srcde , deste , newDirName ) ;
106- } ) ;
107- } ) ;
101+ return this . copyFileOrDir ( path , dirName , newPath , newDirName ) ;
108102 }
109103
110104 /**
@@ -117,15 +111,26 @@ export class FileMock extends File {
117111 * @return Returns a Promise that resolves to an Entry or rejects with an error.
118112 */
119113 copyFile ( path : string , fileName : string , newPath : string , newFileName : string ) : Promise < Entry > {
120- newFileName = newFileName || fileName ;
114+ return this . copyFileOrDir ( path , fileName , newPath , newFileName || fileName ) ;
115+ }
121116
122- return this . resolveDirectoryUrl ( path ) . then ( ( fse ) => {
123- return this . getFile ( fse , fileName , { create : false } ) ;
124- } ) . then ( ( srcfe ) => {
125- return this . resolveDirectoryUrl ( newPath ) . then ( ( deste ) => {
126- return this . copyMock ( srcfe , deste , newFileName ) ;
127- } ) ;
128- } ) ;
117+ /**
118+ * Copy a file or dir to a given path.
119+ *
120+ * @param sourcePath Path of the file/dir to copy.
121+ * @param sourceName Name of file/dir to copy
122+ * @param destPath Path where to copy.
123+ * @param destName New name of file/dir.
124+ * @return Returns a Promise that resolves to the new Entry or rejects with an error.
125+ */
126+ async copyFileOrDir ( sourcePath : string , sourceName : string , destPath : string , destName : string ) : Promise < Entry > {
127+ const destFixed = this . fixPathAndName ( destPath , destName ) ;
128+
129+ const source = await this . resolveLocalFilesystemUrl ( this . textUtils . concatenatePaths ( sourcePath , sourceName ) ) ;
130+
131+ const destParentDir = await this . resolveDirectoryUrl ( destFixed . path ) ;
132+
133+ return this . copyMock ( source , destParentDir , destFixed . name ) ;
129134 }
130135
131136 /**
@@ -431,13 +436,7 @@ export class FileMock extends File {
431436 * an error.
432437 */
433438 moveDir ( path : string , dirName : string , newPath : string , newDirName : string ) : Promise < DirectoryEntry | Entry > {
434- return this . resolveDirectoryUrl ( path ) . then ( ( fse ) => {
435- return this . getDirectory ( fse , dirName , { create : false } ) ;
436- } ) . then ( ( srcde ) => {
437- return this . resolveDirectoryUrl ( newPath ) . then ( ( deste ) => {
438- return this . moveMock ( srcde , deste , newDirName ) ;
439- } ) ;
440- } ) ;
439+ return this . moveFileOrDir ( path , dirName , newPath , newDirName ) ;
441440 }
442441
443442 /**
@@ -450,15 +449,43 @@ export class FileMock extends File {
450449 * @return Returns a Promise that resolves to the new Entry or rejects with an error.
451450 */
452451 moveFile ( path : string , fileName : string , newPath : string , newFileName : string ) : Promise < Entry > {
453- newFileName = newFileName || fileName ;
452+ return this . moveFileOrDir ( path , fileName , newPath , newFileName || fileName ) ;
453+ }
454454
455- return this . resolveDirectoryUrl ( path ) . then ( ( fse ) => {
456- return this . getFile ( fse , fileName , { create : false } ) ;
457- } ) . then ( ( srcfe ) => {
458- return this . resolveDirectoryUrl ( newPath ) . then ( ( deste ) => {
459- return this . moveMock ( srcfe , deste , newFileName ) ;
460- } ) ;
461- } ) ;
455+ /**
456+ * Move a file or dir to a given path.
457+ *
458+ * @param sourcePath Path of the file/dir to copy.
459+ * @param sourceName Name of file/dir to copy
460+ * @param destPath Path where to copy.
461+ * @param destName New name of file/dir.
462+ * @return Returns a Promise that resolves to the new Entry or rejects with an error.
463+ */
464+ async moveFileOrDir ( sourcePath : string , sourceName : string , destPath : string , destName : string ) : Promise < Entry > {
465+ const destFixed = this . fixPathAndName ( destPath , destName ) ;
466+
467+ const source = await this . resolveLocalFilesystemUrl ( this . textUtils . concatenatePaths ( sourcePath , sourceName ) ) ;
468+
469+ const destParentDir = await this . resolveDirectoryUrl ( destFixed . path ) ;
470+
471+ return this . moveMock ( source , destParentDir , destFixed . name ) ;
472+ }
473+
474+ /**
475+ * Fix a path and name, making sure the name doesn't contain any folder. If it does, the folder will be moved to the path.
476+ *
477+ * @param path Path to fix.
478+ * @param name Name to fix.
479+ * @return Fixed values.
480+ */
481+ protected fixPathAndName ( path : string , name : string ) : { path : string , name : string } {
482+
483+ const fullPath = this . textUtils . concatenatePaths ( path , name ) ;
484+
485+ return {
486+ path : fullPath . substring ( 0 , fullPath . lastIndexOf ( '/' ) ) ,
487+ name : fullPath . substr ( fullPath . lastIndexOf ( '/' ) + 1 ) ,
488+ } ;
462489 }
463490
464491 /**
0 commit comments