Skip to content

Commit

Permalink
Added support for the DELETE method + Fixed delete bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed May 13, 2017
1 parent feb22d6 commit bb5fc2b
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 20 deletions.
11 changes: 10 additions & 1 deletion lib/resource/physical/PhysicalFolder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/resource/std/StandardResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions lib/resource/virtual/VirtualFile.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ export declare class VirtualFile extends VirtualResource {
content: Int8Array;
constructor(name: string, parent?: IResource, fsManager?: FSManager);
type(callback: ReturnCallback<ResourceType>): void;
create(callback: SimpleCallback): void;
delete(callback: SimpleCallback): void;
append(data: Int8Array, callback: SimpleCallback): void;
write(data: Int8Array, callback: SimpleCallback): void;
read(callback: ReturnCallback<Int8Array>): void;
Expand Down
6 changes: 0 additions & 6 deletions lib/resource/virtual/VirtualFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lib/server/commands/Commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions lib/server/commands/Delete.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { MethodCallArgs } from '../WebDAVRequest';
export default function (arg: MethodCallArgs, callback: any): void;
20 changes: 20 additions & 0 deletions lib/server/commands/Delete.js
Original file line number Diff line number Diff line change
@@ -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;
13 changes: 12 additions & 1 deletion src/resource/physical/PhysicalFolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IResource>(children, (child, cb) => {
child.delete(cb);
}, () => {
fs.unlink(this.realPath, (e) => {
fs.rmdir(this.realPath, (e) => {
if(e)
callback(e);
else
Expand Down
7 changes: 7 additions & 0 deletions src/resource/std/StandardResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 0 additions & 10 deletions src/resource/virtual/VirtualFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/server/commands/Commands.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -11,6 +12,7 @@ export default {
NotImplemented,
Propfind,
Options,
Delete,
Mkcol,
Head,
Post,
Expand Down
22 changes: 22 additions & 0 deletions src/server/commands/Delete.ts
Original file line number Diff line number Diff line change
@@ -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();
})
})
}

0 comments on commit bb5fc2b

Please sign in to comment.