MOBILE-3411 h5p: Fix extracting H5P in browser

main
Dani Palou 2020-05-26 15:08:47 +02:00
parent 3c68a0425e
commit 4bbd05bd55
2 changed files with 77 additions and 86 deletions

View File

@ -98,13 +98,7 @@ export class FileMock extends File {
* @return Returns a Promise that resolves to the new Entry object or rejects with an error. * @return Returns a Promise that resolves to the new Entry object or rejects with an error.
*/ */
copyDir(path: string, dirName: string, newPath: string, newDirName: string): Promise<Entry> { copyDir(path: string, dirName: string, newPath: string, newDirName: string): Promise<Entry> {
return this.resolveDirectoryUrl(path).then((fse) => { return this.copyFileOrDir(path, dirName, newPath, newDirName);
return this.getDirectory(fse, dirName, { create: false });
}).then((srcde) => {
return this.resolveDirectoryUrl(newPath).then((deste) => {
return this.copyMock(srcde, deste, newDirName);
});
});
} }
/** /**
@ -117,15 +111,26 @@ export class FileMock extends File {
* @return Returns a Promise that resolves to an Entry or rejects with an error. * @return Returns a Promise that resolves to an Entry or rejects with an error.
*/ */
copyFile(path: string, fileName: string, newPath: string, newFileName: string): Promise<Entry> { copyFile(path: string, fileName: string, newPath: string, newFileName: string): Promise<Entry> {
newFileName = newFileName || fileName; return this.copyFileOrDir(path, fileName, newPath, newFileName || fileName);
}
return this.resolveDirectoryUrl(path).then((fse) => { /**
return this.getFile(fse, fileName, { create: false }); * Copy a file or dir to a given path.
}).then((srcfe) => { *
return this.resolveDirectoryUrl(newPath).then((deste) => { * @param sourcePath Path of the file/dir to copy.
return this.copyMock(srcfe, deste, newFileName); * @param sourceName Name of file/dir to copy
}); * @param destPath Path where to copy.
}); * @param destName New name of file/dir.
* @return Returns a Promise that resolves to the new Entry or rejects with an error.
*/
async copyFileOrDir(sourcePath: string, sourceName: string, destPath: string, destName: string): Promise<Entry> {
const destFixed = this.fixPathAndName(destPath, destName);
const source = await this.resolveLocalFilesystemUrl(this.textUtils.concatenatePaths(sourcePath, sourceName));
const destParentDir = await this.resolveDirectoryUrl(destFixed.path);
return this.copyMock(source, destParentDir, destFixed.name);
} }
/** /**
@ -431,13 +436,7 @@ export class FileMock extends File {
* an error. * an error.
*/ */
moveDir(path: string, dirName: string, newPath: string, newDirName: string): Promise<DirectoryEntry | Entry> { moveDir(path: string, dirName: string, newPath: string, newDirName: string): Promise<DirectoryEntry | Entry> {
return this.resolveDirectoryUrl(path).then((fse) => { return this.moveFileOrDir(path, dirName, newPath, newDirName);
return this.getDirectory(fse, dirName, { create: false });
}).then((srcde) => {
return this.resolveDirectoryUrl(newPath).then((deste) => {
return this.moveMock(srcde, deste, newDirName);
});
});
} }
/** /**
@ -450,15 +449,43 @@ export class FileMock extends File {
* @return Returns a Promise that resolves to the new Entry or rejects with an error. * @return Returns a Promise that resolves to the new Entry or rejects with an error.
*/ */
moveFile(path: string, fileName: string, newPath: string, newFileName: string): Promise<Entry> { moveFile(path: string, fileName: string, newPath: string, newFileName: string): Promise<Entry> {
newFileName = newFileName || fileName; return this.moveFileOrDir(path, fileName, newPath, newFileName || fileName);
}
return this.resolveDirectoryUrl(path).then((fse) => { /**
return this.getFile(fse, fileName, { create: false }); * Move a file or dir to a given path.
}).then((srcfe) => { *
return this.resolveDirectoryUrl(newPath).then((deste) => { * @param sourcePath Path of the file/dir to copy.
return this.moveMock(srcfe, deste, newFileName); * @param sourceName Name of file/dir to copy
}); * @param destPath Path where to copy.
}); * @param destName New name of file/dir.
* @return Returns a Promise that resolves to the new Entry or rejects with an error.
*/
async moveFileOrDir(sourcePath: string, sourceName: string, destPath: string, destName: string): Promise<Entry> {
const destFixed = this.fixPathAndName(destPath, destName);
const source = await this.resolveLocalFilesystemUrl(this.textUtils.concatenatePaths(sourcePath, sourceName));
const destParentDir = await this.resolveDirectoryUrl(destFixed.path);
return this.moveMock(source, destParentDir, destFixed.name);
}
/**
* 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.
*
* @param path Path to fix.
* @param name Name to fix.
* @return Fixed values.
*/
protected fixPathAndName(path: string, name: string): {path: string, name: string} {
const fullPath = this.textUtils.concatenatePaths(path, name);
return {
path: fullPath.substring(0, fullPath.lastIndexOf('/')),
name: fullPath.substr(fullPath.lastIndexOf('/') + 1),
};
} }
/** /**

View File

@ -812,42 +812,17 @@ export class CoreFileProvider {
} }
}).then(() => { }).then(() => {
if (this.isHTMLAPI) { return moveFn(this.basePath, originalPath, this.basePath, newPath).catch((error) => {
// In Cordova API we need to calculate the longest matching path to make it work. // The move can fail if the path has encoded characters. Try again if that's the case.
// The function this.file.moveFile('a/', 'b/c.ext', 'a/', 'b/d.ext') doesn't work. const decodedOriginal = decodeURI(originalPath),
// The function this.file.moveFile('a/b/', 'c.ext', 'a/b/', 'd.ext') works. decodedNew = decodeURI(newPath);
const dirsA = originalPath.split('/'),
dirsB = newPath.split('/');
let commonPath = this.basePath;
for (let i = 0; i < dirsA.length; i++) { if (decodedOriginal != originalPath || decodedNew != newPath) {
let dir = dirsA[i]; return moveFn(this.basePath, decodedOriginal, this.basePath, decodedNew);
if (dirsB[i] === dir) { } else {
// Found a common folder, add it to common path and remove it from each specific path. return Promise.reject(error);
dir = dir + '/';
commonPath = this.textUtils.concatenatePaths(commonPath, dir);
originalPath = originalPath.replace(dir, '');
newPath = newPath.replace(dir, '');
} else {
// Folder doesn't match, stop searching.
break;
}
} }
});
return moveFn(commonPath, originalPath, commonPath, newPath);
} else {
return moveFn(this.basePath, originalPath, this.basePath, newPath).catch((error) => {
// The move can fail if the path has encoded characters. Try again if that's the case.
const decodedOriginal = decodeURI(originalPath),
decodedNew = decodeURI(newPath);
if (decodedOriginal != originalPath || decodedNew != newPath) {
return moveFn(this.basePath, decodedOriginal, this.basePath, decodedNew);
} else {
return Promise.reject(error);
}
});
}
}); });
} }
@ -888,8 +863,6 @@ export class CoreFileProvider {
* @return Promise resolved when the entry is copied. * @return Promise resolved when the entry is copied.
*/ */
protected copyFileOrDir(from: string, to: string, isDir?: boolean, destDirExists?: boolean): Promise<any> { protected copyFileOrDir(from: string, to: string, isDir?: boolean, destDirExists?: boolean): Promise<any> {
let fromFileAndDir,
toFileAndDir;
const copyFn = isDir ? this.file.copyDir.bind(this.file) : this.file.copyFile.bind(this.file); const copyFn = isDir ? this.file.copyDir.bind(this.file) : this.file.copyFile.bind(this.file);
return this.init().then(() => { return this.init().then(() => {
@ -897,33 +870,24 @@ export class CoreFileProvider {
from = this.removeStartingSlash(from.replace(this.basePath, '')); from = this.removeStartingSlash(from.replace(this.basePath, ''));
to = this.removeStartingSlash(to.replace(this.basePath, '')); to = this.removeStartingSlash(to.replace(this.basePath, ''));
fromFileAndDir = this.getFileAndDirectoryFromPath(from); const toFileAndDir = this.getFileAndDirectoryFromPath(to);
toFileAndDir = this.getFileAndDirectoryFromPath(to);
if (toFileAndDir.directory && !destDirExists) { if (toFileAndDir.directory && !destDirExists) {
// Create the target directory if it doesn't exist. // Create the target directory if it doesn't exist.
return this.createDir(toFileAndDir.directory); return this.createDir(toFileAndDir.directory);
} }
}).then(() => { }).then(() => {
if (this.isHTMLAPI) { return copyFn(this.basePath, from, this.basePath, to).catch((error) => {
// In HTML API, the file name cannot include a directory, otherwise it fails. // The copy can fail if the path has encoded characters. Try again if that's the case.
const fromDir = this.textUtils.concatenatePaths(this.basePath, fromFileAndDir.directory), const decodedFrom = decodeURI(from),
toDir = this.textUtils.concatenatePaths(this.basePath, toFileAndDir.directory); decodedTo = decodeURI(to);
return copyFn(fromDir, fromFileAndDir.name, toDir, toFileAndDir.name); if (from != decodedFrom || to != decodedTo) {
} else { return copyFn(this.basePath, decodedFrom, this.basePath, decodedTo);
return copyFn(this.basePath, from, this.basePath, to).catch((error) => { } else {
// The copy can fail if the path has encoded characters. Try again if that's the case. return Promise.reject(error);
const decodedFrom = decodeURI(from), }
decodedTo = decodeURI(to); });
if (from != decodedFrom || to != decodedTo) {
return copyFn(this.basePath, decodedFrom, this.basePath, decodedTo);
} else {
return Promise.reject(error);
}
});
}
}); });
} }