Skip to content

Commit 4bbd05b

Browse files
committed
MOBILE-3411 h5p: Fix extracting H5P in browser
1 parent 3c68a04 commit 4bbd05b

File tree

2 files changed

+78
-87
lines changed

2 files changed

+78
-87
lines changed

src/core/emulator/providers/file.ts

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/**

src/providers/file.ts

Lines changed: 21 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -812,42 +812,17 @@ export class CoreFileProvider {
812812
}
813813
}).then(() => {
814814

815-
if (this.isHTMLAPI) {
816-
// In Cordova API we need to calculate the longest matching path to make it work.
817-
// The function this.file.moveFile('a/', 'b/c.ext', 'a/', 'b/d.ext') doesn't work.
818-
// The function this.file.moveFile('a/b/', 'c.ext', 'a/b/', 'd.ext') works.
819-
const dirsA = originalPath.split('/'),
820-
dirsB = newPath.split('/');
821-
let commonPath = this.basePath;
822-
823-
for (let i = 0; i < dirsA.length; i++) {
824-
let dir = dirsA[i];
825-
if (dirsB[i] === dir) {
826-
// Found a common folder, add it to common path and remove it from each specific path.
827-
dir = dir + '/';
828-
commonPath = this.textUtils.concatenatePaths(commonPath, dir);
829-
originalPath = originalPath.replace(dir, '');
830-
newPath = newPath.replace(dir, '');
831-
} else {
832-
// Folder doesn't match, stop searching.
833-
break;
834-
}
835-
}
815+
return moveFn(this.basePath, originalPath, this.basePath, newPath).catch((error) => {
816+
// The move can fail if the path has encoded characters. Try again if that's the case.
817+
const decodedOriginal = decodeURI(originalPath),
818+
decodedNew = decodeURI(newPath);
836819

837-
return moveFn(commonPath, originalPath, commonPath, newPath);
838-
} else {
839-
return moveFn(this.basePath, originalPath, this.basePath, newPath).catch((error) => {
840-
// The move can fail if the path has encoded characters. Try again if that's the case.
841-
const decodedOriginal = decodeURI(originalPath),
842-
decodedNew = decodeURI(newPath);
843-
844-
if (decodedOriginal != originalPath || decodedNew != newPath) {
845-
return moveFn(this.basePath, decodedOriginal, this.basePath, decodedNew);
846-
} else {
847-
return Promise.reject(error);
848-
}
849-
});
850-
}
820+
if (decodedOriginal != originalPath || decodedNew != newPath) {
821+
return moveFn(this.basePath, decodedOriginal, this.basePath, decodedNew);
822+
} else {
823+
return Promise.reject(error);
824+
}
825+
});
851826
});
852827
}
853828

@@ -888,42 +863,31 @@ export class CoreFileProvider {
888863
* @return Promise resolved when the entry is copied.
889864
*/
890865
protected copyFileOrDir(from: string, to: string, isDir?: boolean, destDirExists?: boolean): Promise<any> {
891-
let fromFileAndDir,
892-
toFileAndDir;
893866
const copyFn = isDir ? this.file.copyDir.bind(this.file) : this.file.copyFile.bind(this.file);
894867

895868
return this.init().then(() => {
896869
// Paths cannot start with "/". Remove basePath if present.
897870
from = this.removeStartingSlash(from.replace(this.basePath, ''));
898871
to = this.removeStartingSlash(to.replace(this.basePath, ''));
899872

900-
fromFileAndDir = this.getFileAndDirectoryFromPath(from);
901-
toFileAndDir = this.getFileAndDirectoryFromPath(to);
873+
const toFileAndDir = this.getFileAndDirectoryFromPath(to);
902874

903875
if (toFileAndDir.directory && !destDirExists) {
904876
// Create the target directory if it doesn't exist.
905877
return this.createDir(toFileAndDir.directory);
906878
}
907879
}).then(() => {
908-
if (this.isHTMLAPI) {
909-
// In HTML API, the file name cannot include a directory, otherwise it fails.
910-
const fromDir = this.textUtils.concatenatePaths(this.basePath, fromFileAndDir.directory),
911-
toDir = this.textUtils.concatenatePaths(this.basePath, toFileAndDir.directory);
912-
913-
return copyFn(fromDir, fromFileAndDir.name, toDir, toFileAndDir.name);
914-
} else {
915-
return copyFn(this.basePath, from, this.basePath, to).catch((error) => {
916-
// The copy can fail if the path has encoded characters. Try again if that's the case.
917-
const decodedFrom = decodeURI(from),
918-
decodedTo = decodeURI(to);
880+
return copyFn(this.basePath, from, this.basePath, to).catch((error) => {
881+
// The copy can fail if the path has encoded characters. Try again if that's the case.
882+
const decodedFrom = decodeURI(from),
883+
decodedTo = decodeURI(to);
919884

920-
if (from != decodedFrom || to != decodedTo) {
921-
return copyFn(this.basePath, decodedFrom, this.basePath, decodedTo);
922-
} else {
923-
return Promise.reject(error);
924-
}
925-
});
926-
}
885+
if (from != decodedFrom || to != decodedTo) {
886+
return copyFn(this.basePath, decodedFrom, this.basePath, decodedTo);
887+
} else {
888+
return Promise.reject(error);
889+
}
890+
});
927891
});
928892
}
929893

0 commit comments

Comments
 (0)