From 656d0f67aef92e8659dfc73cfb146d2565f378fd Mon Sep 17 00:00:00 2001 From: Adrien Castex Date: Sun, 1 Oct 2017 14:23:40 +0200 Subject: [PATCH] Fixed the '_move' method in the 'PhysicalFileSystem' class --- .../v2/instances/PhysicalFileSystem.js | 47 +++++++++++++++---- .../v2/instances/PhysicalFileSystem.ts | 36 +++++++++++++- 2 files changed, 73 insertions(+), 10 deletions(-) diff --git a/lib/manager/v2/instances/PhysicalFileSystem.js b/lib/manager/v2/instances/PhysicalFileSystem.js index c0465bbe..1cddb383 100644 --- a/lib/manager/v2/instances/PhysicalFileSystem.js +++ b/lib/manager/v2/instances/PhysicalFileSystem.js @@ -156,22 +156,49 @@ var PhysicalFileSystem = (function (_super) { var _this = this; var realPathFrom = this.getRealPath(pathFrom).realPath; var realPathTo = this.getRealPath(pathTo).realPath; - fs.rename(realPathFrom, realPathTo, function (e) { - if (!e) { + var rename = function (overwritten) { + fs.rename(realPathFrom, realPathTo, function (e) { + if (e) + return callback(e); _this.resources[realPathTo] = _this.resources[realPathFrom]; delete _this.resources[realPathFrom]; - callback(null, true); + callback(null, overwritten); + }); + }; + fs.access(realPathTo, function (e) { + if (e) { + rename(false); } else { - fs.stat(realPathTo, function (er) { - if (!er) - e = Errors_1.Errors.ResourceAlreadyExists; - else - e = Errors_1.Errors.ResourceNotFound; - callback(e, false); + if (!ctx.overwrite) + return callback(Errors_1.Errors.ResourceAlreadyExists); + _this.delete(ctx.context, pathTo, function (e) { + if (e) + return callback(e); + rename(true); }); } }); + /* + + fs.rename(realPathFrom, realPathTo, (e) => { + if(!e) + { + this.resources[realPathTo] = this.resources[realPathFrom]; + delete this.resources[realPathFrom]; + callback(null, true); + } + else + { + fs.stat(realPathTo, (er) => { + if(!er) + e = Errors.ResourceAlreadyExists; + else + e = Errors.ResourceNotFound; + callback(e, false); + }) + } + })*/ }; PhysicalFileSystem.prototype._size = function (path, ctx, callback) { var realPath = this.getRealPath(path).realPath; @@ -221,6 +248,8 @@ var PhysicalFileSystem = (function (_super) { }; PhysicalFileSystem.prototype._type = function (path, ctx, callback) { var realPath = this.getRealPath(path).realPath; + if (realPath.indexOf('.url') !== -1) + return callback(Errors_1.Errors.ResourceNotFound); fs.stat(realPath, function (e, stat) { if (e) return callback(Errors_1.Errors.ResourceNotFound); diff --git a/src/manager/v2/instances/PhysicalFileSystem.ts b/src/manager/v2/instances/PhysicalFileSystem.ts index ad2319d4..a77aad03 100644 --- a/src/manager/v2/instances/PhysicalFileSystem.ts +++ b/src/manager/v2/instances/PhysicalFileSystem.ts @@ -212,6 +212,37 @@ export class PhysicalFileSystem extends FileSystem { const { realPath: realPathFrom } = this.getRealPath(pathFrom); const { realPath: realPathTo } = this.getRealPath(pathTo); + + const rename = (overwritten) => { + fs.rename(realPathFrom, realPathTo, (e) => { + if(e) + return callback(e); + + this.resources[realPathTo] = this.resources[realPathFrom]; + delete this.resources[realPathFrom]; + callback(null, overwritten); + }); + }; + + fs.access(realPathTo, (e) => { + if(e) + { // destination doesn't exist + rename(false); + } + else + { // destination exists + if(!ctx.overwrite) + return callback(Errors.ResourceAlreadyExists); + + this.delete(ctx.context, pathTo, (e) => { + if(e) + return callback(e); + rename(true); + }); + } + }) + + /* fs.rename(realPathFrom, realPathTo, (e) => { if(!e) @@ -230,7 +261,7 @@ export class PhysicalFileSystem extends FileSystem callback(e, false); }) } - }) + })*/ } protected _size(path : Path, ctx : SizeInfo, callback : ReturnCallback) : void @@ -306,6 +337,9 @@ export class PhysicalFileSystem extends FileSystem { const { realPath } = this.getRealPath(path); + if(realPath.indexOf('.url') !== -1) + return callback(Errors.ResourceNotFound); + fs.stat(realPath, (e, stat) => { if(e) return callback(Errors.ResourceNotFound);