From bb5fc2ba25761d44af21e2098e0bd8bc975c868f Mon Sep 17 00:00:00 2001 From: Adrien Castex Date: Sat, 13 May 2017 16:43:09 +0200 Subject: [PATCH] Added support for the DELETE method + Fixed delete bugs --- lib/resource/physical/PhysicalFolder.js | 11 ++++++++++- lib/resource/std/StandardResource.js | 5 +++++ lib/resource/virtual/VirtualFile.d.ts | 2 -- lib/resource/virtual/VirtualFile.js | 6 ------ lib/server/commands/Commands.js | 2 ++ lib/server/commands/Delete.d.ts | 2 ++ lib/server/commands/Delete.js | 20 ++++++++++++++++++++ src/resource/physical/PhysicalFolder.ts | 13 ++++++++++++- src/resource/std/StandardResource.ts | 7 +++++++ src/resource/virtual/VirtualFile.ts | 10 ---------- src/server/commands/Commands.ts | 2 ++ src/server/commands/Delete.ts | 22 ++++++++++++++++++++++ 12 files changed, 82 insertions(+), 20 deletions(-) create mode 100644 lib/server/commands/Delete.d.ts create mode 100644 lib/server/commands/Delete.js create mode 100644 src/server/commands/Delete.ts diff --git a/lib/resource/physical/PhysicalFolder.js b/lib/resource/physical/PhysicalFolder.js index 6aa9a9ae..3ed3a48f 100644 --- a/lib/resource/physical/PhysicalFolder.js +++ b/lib/resource/physical/PhysicalFolder.js @@ -35,10 +35,19 @@ var PhysicalFolder = (function (_super) { callback(e); return; } + if (children.length === 0) { + fs.rmdir(_this.realPath, function (e) { + if (e) + callback(e); + else + _this.removeFromParent(callback); + }); + return; + } ResourceChildren_1.forAll(children, function (child, cb) { child.delete(cb); }, function () { - fs.unlink(_this.realPath, function (e) { + fs.rmdir(_this.realPath, function (e) { if (e) callback(e); else diff --git a/lib/resource/std/StandardResource.js b/lib/resource/std/StandardResource.js index f2abb348..c27fc8a7 100644 --- a/lib/resource/std/StandardResource.js +++ b/lib/resource/std/StandardResource.js @@ -120,8 +120,13 @@ var StandardResource = (function () { var parent = this.parent; if (parent) parent.removeChild(this, function (e) { + if (e) { + callback(e); + return; + } if (_this.parent === parent) _this.parent = null; + callback(null); }); else callback(null); diff --git a/lib/resource/virtual/VirtualFile.d.ts b/lib/resource/virtual/VirtualFile.d.ts index f56e2ec2..2eb0c160 100644 --- a/lib/resource/virtual/VirtualFile.d.ts +++ b/lib/resource/virtual/VirtualFile.d.ts @@ -5,8 +5,6 @@ export declare class VirtualFile extends VirtualResource { content: Int8Array; constructor(name: string, parent?: IResource, fsManager?: FSManager); type(callback: ReturnCallback): void; - create(callback: SimpleCallback): void; - delete(callback: SimpleCallback): void; append(data: Int8Array, callback: SimpleCallback): void; write(data: Int8Array, callback: SimpleCallback): void; read(callback: ReturnCallback): void; diff --git a/lib/resource/virtual/VirtualFile.js b/lib/resource/virtual/VirtualFile.js index 6505f62f..30763325 100644 --- a/lib/resource/virtual/VirtualFile.js +++ b/lib/resource/virtual/VirtualFile.js @@ -23,12 +23,6 @@ var VirtualFile = (function (_super) { VirtualFile.prototype.type = function (callback) { callback(null, IResource_1.ResourceType.File); }; - VirtualFile.prototype.create = function (callback) { - callback(null); - }; - VirtualFile.prototype.delete = function (callback) { - this.removeFromParent(callback); - }; VirtualFile.prototype.append = function (data, callback) { var newContent = new Int8Array(this.content.length + data.length); for (var i = 0; i < this.content.length; ++i) diff --git a/lib/server/commands/Commands.js b/lib/server/commands/Commands.js index a3ce44a7..74616201 100644 --- a/lib/server/commands/Commands.js +++ b/lib/server/commands/Commands.js @@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); var NotImplemented_1 = require("./NotImplemented"); var Propfind_1 = require("./Propfind"); var Options_1 = require("./Options"); +var Delete_1 = require("./Delete"); var Mkcol_1 = require("./Mkcol"); var Head_1 = require("./Head"); var Post_1 = require("./Post"); @@ -12,6 +13,7 @@ exports.default = { NotImplemented: NotImplemented_1.default, Propfind: Propfind_1.default, Options: Options_1.default, + Delete: Delete_1.default, Mkcol: Mkcol_1.default, Head: Head_1.default, Post: Post_1.default, diff --git a/lib/server/commands/Delete.d.ts b/lib/server/commands/Delete.d.ts new file mode 100644 index 00000000..f8724433 --- /dev/null +++ b/lib/server/commands/Delete.d.ts @@ -0,0 +1,2 @@ +import { MethodCallArgs } from '../WebDAVRequest'; +export default function (arg: MethodCallArgs, callback: any): void; diff --git a/lib/server/commands/Delete.js b/lib/server/commands/Delete.js new file mode 100644 index 00000000..d01a9c8e --- /dev/null +++ b/lib/server/commands/Delete.js @@ -0,0 +1,20 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +var WebDAVRequest_1 = require("../WebDAVRequest"); +function default_1(arg, callback) { + arg.getResource(function (e, r) { + if (e) { + arg.setCode(WebDAVRequest_1.HTTPCodes.NotFound); + callback(); + return; + } + r.delete(function (e) { + if (e) + arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError); + else + arg.setCode(WebDAVRequest_1.HTTPCodes.OK); + callback(); + }); + }); +} +exports.default = default_1; diff --git a/src/resource/physical/PhysicalFolder.ts b/src/resource/physical/PhysicalFolder.ts index 1acd7a56..aa142ab1 100644 --- a/src/resource/physical/PhysicalFolder.ts +++ b/src/resource/physical/PhysicalFolder.ts @@ -36,10 +36,21 @@ export class PhysicalFolder extends PhysicalResource return; } + if(children.length === 0) + { + fs.rmdir(this.realPath, (e) => { + if(e) + callback(e); + else + this.removeFromParent(callback); + }); + return; + } + forAll(children, (child, cb) => { child.delete(cb); }, () => { - fs.unlink(this.realPath, (e) => { + fs.rmdir(this.realPath, (e) => { if(e) callback(e); else diff --git a/src/resource/std/StandardResource.ts b/src/resource/std/StandardResource.ts index 223267a1..7cb3bfb1 100644 --- a/src/resource/std/StandardResource.ts +++ b/src/resource/std/StandardResource.ts @@ -188,8 +188,15 @@ export abstract class StandardResource implements IResource const parent = this.parent; if(parent) parent.removeChild(this, (e) => { + if(e) + { + callback(e) + return; + } + if(this.parent === parent) // this.parent didn't change this.parent = null; + callback(null); }); else callback(null); diff --git a/src/resource/virtual/VirtualFile.ts b/src/resource/virtual/VirtualFile.ts index 1228a411..efe79499 100644 --- a/src/resource/virtual/VirtualFile.ts +++ b/src/resource/virtual/VirtualFile.ts @@ -19,16 +19,6 @@ export class VirtualFile extends VirtualResource { callback(null, ResourceType.File) } - - // ****************************** Actions ****************************** // - create(callback : SimpleCallback) - { - callback(null); - } - delete(callback : SimpleCallback) - { - this.removeFromParent(callback); - } // ****************************** Content ****************************** // append(data : Int8Array, callback : SimpleCallback) diff --git a/src/server/commands/Commands.ts b/src/server/commands/Commands.ts index f9bead89..73bad7d2 100644 --- a/src/server/commands/Commands.ts +++ b/src/server/commands/Commands.ts @@ -1,6 +1,7 @@ import NotImplemented from './NotImplemented' import Propfind from './Propfind' import Options from './Options' +import Delete from './Delete' import Mkcol from './Mkcol' import Head from './Head' import Post from './Post' @@ -11,6 +12,7 @@ export default { NotImplemented, Propfind, Options, + Delete, Mkcol, Head, Post, diff --git a/src/server/commands/Delete.ts b/src/server/commands/Delete.ts new file mode 100644 index 00000000..37c2c219 --- /dev/null +++ b/src/server/commands/Delete.ts @@ -0,0 +1,22 @@ +import { HTTPCodes, MethodCallArgs, WebDAVRequest } from '../WebDAVRequest' +import { IResource } from '../../resource/Resource' + +export default function(arg : MethodCallArgs, callback) +{ + arg.getResource((e, r) => { + if(e) + { + arg.setCode(HTTPCodes.NotFound) + callback() + return; + } + + r.delete((e) => { + if(e) + arg.setCode(HTTPCodes.InternalServerError); + else + arg.setCode(HTTPCodes.OK); + callback(); + }) + }) +}