From 90d63c05506df9ef05eae86eaa2c2d1d9417b892 Mon Sep 17 00:00:00 2001 From: yann300 Date: Mon, 13 Sep 2021 17:10:52 +0200 Subject: [PATCH] normalize path api --- apps/remix-ide/src/app/files/fileManager.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/apps/remix-ide/src/app/files/fileManager.js b/apps/remix-ide/src/app/files/fileManager.js index ca311a3d3f..6cd3b4a5aa 100644 --- a/apps/remix-ide/src/app/files/fileManager.js +++ b/apps/remix-ide/src/app/files/fileManager.js @@ -61,6 +61,10 @@ class FileManager extends Plugin { return path.replace(/^\/browser\//, '').replace(/^browser\//, '') // forbids plugin to access the root filesystem } + normalize (path) { + return path.replace(/^\/+/, '') + } + /** * Emit error if path doesn't exist * @param {string} path path of the file/directory @@ -119,6 +123,7 @@ class FileManager extends Plugin { */ exists (path) { try { + path = this.normalize(path) path = this.limitPluginScope(path) const provider = this.fileProviderOf(path) const result = provider.exists(path) @@ -168,6 +173,7 @@ class FileManager extends Plugin { * @returns {void} */ async open (path) { + path = this.normalize(path) path = this.limitPluginScope(path) path = this.getPathFromUrl(path).file await this._handleExists(path, `Cannot open file ${path}`) @@ -183,6 +189,7 @@ class FileManager extends Plugin { */ async writeFile (path, data) { try { + path = this.normalize(path) path = this.limitPluginScope(path) if (await this.exists(path)) { await this._handleIsFile(path, `Cannot write file ${path}`) @@ -204,6 +211,7 @@ class FileManager extends Plugin { */ async readFile (path) { try { + path = this.normalize(path) path = this.limitPluginScope(path) await this._handleExists(path, `Cannot read file ${path}`) await this._handleIsFile(path, `Cannot read file ${path}`) @@ -221,6 +229,8 @@ class FileManager extends Plugin { */ async copyFile (src, dest, customName) { try { + src = this.normalize(src) + dest = this.normalize(dest) src = this.limitPluginScope(src) dest = this.limitPluginScope(dest) await this._handleExists(src, `Cannot copy from ${src}. Path does not exist.`) @@ -245,6 +255,8 @@ class FileManager extends Plugin { */ async copyDir (src, dest) { try { + src = this.normalize(src) + dest = this.normalize(dest) src = this.limitPluginScope(src) dest = this.limitPluginScope(dest) await this._handleExists(src, `Cannot copy from ${src}. Path does not exist.`) @@ -281,6 +293,8 @@ class FileManager extends Plugin { */ async rename (oldPath, newPath) { try { + oldPath = this.normalize(oldPath) + newPath = this.normalize(newPath) oldPath = this.limitPluginScope(oldPath) newPath = this.limitPluginScope(newPath) await this._handleExists(oldPath, `Cannot rename ${oldPath}`) @@ -313,6 +327,7 @@ class FileManager extends Plugin { */ async mkdir (path) { try { + path = this.normalize(path) path = this.limitPluginScope(path) if (await this.exists(path)) { throw createError({ code: 'EEXIST', message: `Cannot create directory ${path}` }) @@ -332,6 +347,7 @@ class FileManager extends Plugin { */ async readdir (path) { try { + path = this.normalize(path) path = this.limitPluginScope(path) await this._handleExists(path) await this._handleIsDir(path) @@ -356,6 +372,7 @@ class FileManager extends Plugin { */ async remove (path) { try { + path = this.normalize(path) path = this.limitPluginScope(path) await this._handleExists(path, `Cannot remove file or directory ${path}`) const provider = this.fileProviderOf(path)