normalize path api

pull/5370/head
yann300 3 years ago committed by davidzagi93@gmail.com
parent f25d45e071
commit f4038c7eb7
  1. 17
      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)

Loading…
Cancel
Save