From 14ec31012974e2935936cb624671526446ecc252 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Wed, 20 Jun 2018 13:18:44 +0200 Subject: [PATCH] MOBILE-2431 file: Fix copy and move files with special characters --- src/providers/file.ts | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/providers/file.ts b/src/providers/file.ts index 9677ab62f..15ac2513a 100644 --- a/src/providers/file.ts +++ b/src/providers/file.ts @@ -609,7 +609,17 @@ export class CoreFileProvider { return this.file.moveFile(commonPath, originalPath, commonPath, newPath); } else { - return this.file.moveFile(this.basePath, originalPath, this.basePath, newPath); + return this.file.moveFile(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 this.file.moveFile(this.basePath, decodedOriginal, this.basePath, decodedNew); + } else { + return Promise.reject(error); + } + }); } }); } @@ -645,7 +655,17 @@ export class CoreFileProvider { return this.file.copyFile(fromDir, fromFileAndDir.name, toDir, toFileAndDir.name); } else { - return this.file.copyFile(this.basePath, from, this.basePath, to); + return this.file.copyFile(this.basePath, from, this.basePath, to).catch((error) => { + // The copy can fail if the path has encoded characters. Try again if that's the case. + const decodedFrom = decodeURI(from), + decodedTo = decodeURI(to); + + if (from != decodedFrom || to != decodedTo) { + return this.file.copyFile(this.basePath, decodedFrom, this.basePath, decodedTo); + } else { + return Promise.reject(error); + } + }); } }); }