Retain path structure for loading paths (#845)

* update retain dir structure logic to use emscripten FS APIs

* remove unused import

* replace error message
This commit is contained in:
Madhur Tandon
2022-10-27 20:08:58 +05:30
committed by GitHub
parent bf4d835948
commit ab085c2d92
4 changed files with 38 additions and 7 deletions

View File

@@ -1,5 +1,4 @@
import { Runtime } from './runtime';
import { getLastPath } from './utils';
import { getLogger } from './logger';
import type { loadPyodide as loadPyodideDeclaration, PyodideInterface } from 'pyodide';
// eslint-disable-next-line
@@ -91,11 +90,25 @@ export class PyodideRuntime extends Runtime {
}
async loadFromFile(path: string): Promise<void> {
const filename = getLastPath(path);
const pathArr = path.split('/');
const filename = pathArr.pop();
for(let i=0; i<pathArr.length; i++)
{
const eachPath = pathArr.slice(0, i+1).join('/');
const {exists, parentExists} = this.interpreter.FS.analyzePath(eachPath);
if (!parentExists) {
throw new Error(`'INTERNAL ERROR! cannot create ${path}, this should never happen'`)
}
if (!exists)
{
this.interpreter.FS.mkdir(eachPath);
}
}
const response = await fetch(path);
const buffer = await response.arrayBuffer();
const data = new Uint8Array(buffer);
const stream = this.interpreter.FS.open(filename, 'w');
pathArr.push(filename);
const stream = this.interpreter.FS.open(pathArr.join('/'), 'w');
this.interpreter.FS.write(stream, data, 0, data.length, 0);
this.interpreter.FS.close(stream);
}

View File

@@ -10,10 +10,6 @@ export function removeClasses(element: HTMLElement, classes: string[]) {
}
}
export function getLastPath(str: string): string {
return str.split('\\').pop().split('/').pop();
}
export function escape(str: string): string {
return str.replace(/</g, "&lt;").replace(/>/g, "&gt;")
}