Skip to content

Commit

Permalink
Implemented the MOVE method
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed May 15, 2017
1 parent b591429 commit 2cb0430
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 22 deletions.
4 changes: 2 additions & 2 deletions lib/resource/IResource.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FSManager, FSPath } from '../manager/FSManager';
import { FSManager } from '../manager/FSManager';
import { XMLElement } from '../helper/XML';
import { LockKind } from './lock/LockKind';
import { Lock } from './lock/Lock';
Expand All @@ -23,7 +23,7 @@ export interface IResource {
fsManager: FSManager;
create(callback: SimpleCallback): any;
delete(callback: SimpleCallback): any;
moveTo(to: FSPath, callback: Return2Callback<FSPath, FSPath>): any;
moveTo(parent: IResource, newName: string, override: boolean, callback: SimpleCallback): any;
rename(newName: string, callback: Return2Callback<string, string>): any;
isSame(resource: IResource, callback: ReturnCallback<boolean>): any;
isOnTheSameFSWith(resource: IResource, callback: ReturnCallback<boolean>): any;
Expand Down
5 changes: 3 additions & 2 deletions lib/resource/physical/PhysicalResource.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { IResource, SimpleCallback, ReturnCallback, Return2Callback, ResourceType } from '../IResource';
import { FSManager, FSPath } from '../../manager/FSManager';
import { FSManager } from '../../manager/FSManager';
import { StandardResource } from '../std/StandardResource';
export declare abstract class PhysicalResource extends StandardResource {
realPath: string;
name: string;
constructor(realPath: string, parent?: IResource, fsManager?: FSManager);
abstract create(callback: SimpleCallback): any;
abstract delete(callback: SimpleCallback): any;
moveTo(to: FSPath, callback: Return2Callback<FSPath, FSPath>): void;
moveTo(parent: IResource, newName: string, override: boolean, callback: SimpleCallback): void;
rename(newName: string, callback: Return2Callback<string, string>): void;
webName(callback: ReturnCallback<string>): void;
abstract type(callback: ReturnCallback<ResourceType>): any;
Expand Down
28 changes: 24 additions & 4 deletions lib/resource/physical/PhysicalResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,29 @@ var PhysicalResource = (function (_super) {
fsManager = PhysicalFSManager_1.PhysicalFSManager.instance();
_this = _super.call(this, parent, fsManager) || this;
_this.realPath = path.resolve(realPath);
_this.name = path.basename(_this.realPath);
return _this;
}
PhysicalResource.prototype.moveTo = function (to, callback) {
callback(new Error('Not implemented yet.'), null, null);
PhysicalResource.prototype.moveTo = function (parent, newName, override, callback) {
var _this = this;
if (parent === this.parent) {
this.rename(newName, function (e, oldName, newName) {
callback(e);
});
return;
}
var oldName = this.name;
this.name = newName;
this.removeFromParent(function (e) {
if (e) {
_this.name = oldName;
callback(e);
}
else
parent.addChild(_this, function (e) {
callback(e);
});
});
};
PhysicalResource.prototype.rename = function (newName, callback) {
var _this = this;
Expand All @@ -40,12 +59,13 @@ var PhysicalResource = (function (_super) {
}
var oldName = path.basename(_this.realPath);
_this.realPath = newPath;
_this.name = newName;
_this.updateLastModified();
callback(e, oldName, newName);
callback(null, oldName, newName);
});
};
PhysicalResource.prototype.webName = function (callback) {
callback(null, path.basename(this.realPath));
callback(null, path.basename(this.name));
};
return PhysicalResource;
}(StandardResource_1.StandardResource));
Expand Down
4 changes: 2 additions & 2 deletions lib/resource/std/StandardResource.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IResource, ReturnCallback, SimpleCallback, Return2Callback, ResourceType, ResourcePropertyValue } from '../IResource';
import { FSManager, FSPath } from '../../manager/FSManager';
import { FSManager } from '../../manager/FSManager';
import { LockKind } from '../lock/LockKind';
import { LockBag } from '../lock/LockBag';
import { Lock } from '../lock/Lock';
Expand All @@ -26,7 +26,7 @@ export declare abstract class StandardResource implements IResource {
getProperties(callback: ReturnCallback<object>): void;
abstract create(callback: SimpleCallback): any;
abstract delete(callback: SimpleCallback): any;
abstract moveTo(to: FSPath, callback: Return2Callback<FSPath, FSPath>): any;
abstract moveTo(parent: IResource, newName: string, override: boolean, callback: SimpleCallback): any;
abstract rename(newName: string, callback: Return2Callback<string, string>): any;
abstract append(data: Int8Array, callback: SimpleCallback): any;
abstract write(data: Int8Array, callback: SimpleCallback): any;
Expand Down
4 changes: 2 additions & 2 deletions lib/resource/virtual/VirtualResource.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { IResource, SimpleCallback, ReturnCallback, Return2Callback, ResourceType } from '../IResource';
import { FSManager, FSPath } from '../../manager/FSManager';
import { FSManager } from '../../manager/FSManager';
import { StandardResource } from '../std/StandardResource';
export declare abstract class VirtualResource extends StandardResource {
name: string;
constructor(name: string, parent?: IResource, fsManager?: FSManager);
create(callback: SimpleCallback): void;
delete(callback: SimpleCallback): void;
moveTo(to: FSPath, callback: Return2Callback<FSPath, FSPath>): void;
moveTo(parent: IResource, newName: string, override: boolean, callback: SimpleCallback): void;
rename(newName: string, callback: Return2Callback<string, string>): void;
webName(callback: ReturnCallback<string>): void;
abstract type(callback: ReturnCallback<ResourceType>): any;
Expand Down
22 changes: 20 additions & 2 deletions lib/resource/virtual/VirtualResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,26 @@ var VirtualResource = (function (_super) {
VirtualResource.prototype.delete = function (callback) {
this.removeFromParent(callback);
};
VirtualResource.prototype.moveTo = function (to, callback) {
callback(new Error('Not implemented yet.'), null, null);
VirtualResource.prototype.moveTo = function (parent, newName, override, callback) {
var _this = this;
if (parent === this.parent) {
this.rename(newName, function (e, oldName, newName) {
callback(e);
});
return;
}
var oldName = this.name;
this.name = newName;
this.removeFromParent(function (e) {
if (e) {
_this.name = oldName;
callback(e);
}
else
parent.addChild(_this, function (e) {
callback(e);
});
});
};
VirtualResource.prototype.rename = function (newName, callback) {
var oldName = this.name;
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 @@ -6,6 +6,7 @@ var Propfind_1 = require("./Propfind");
var Options_1 = require("./Options");
var Delete_1 = require("./Delete");
var Mkcol_1 = require("./Mkcol");
var Move_1 = require("./Move");
var Head_1 = require("./Head");
var Post_1 = require("./Post");
var Put_1 = require("./Put");
Expand All @@ -17,6 +18,7 @@ exports.default = {
Options: Options_1.default,
Delete: Delete_1.default,
Mkcol: Mkcol_1.default,
Move: Move_1.default,
Head: Head_1.default,
Post: Post_1.default,
Put: Put_1.default,
Expand Down
2 changes: 2 additions & 0 deletions lib/server/commands/Move.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;
33 changes: 33 additions & 0 deletions lib/server/commands/Move.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var WebDAVRequest_1 = require("../WebDAVRequest");
var FSManager_1 = require("../../manager/FSManager");
function default_1(arg, callback) {
arg.getResource(function (e, r) {
if (e) {
arg.setCode(WebDAVRequest_1.HTTPCodes.NotFound);
callback();
return;
}
var override = arg.findHeader('overwrite') === 'T';
var destination = arg.findHeader('destination');
if (!destination) {
arg.setCode(WebDAVRequest_1.HTTPCodes.BadRequest);
callback();
return;
}
destination = destination.substring(destination.indexOf('://') + '://'.length);
destination = destination.substring(destination.indexOf('/'));
destination = new FSManager_1.FSPath(destination);
arg.server.getResourceFromPath(destination.getParent(), function (e, rDest) {
r.moveTo(rDest, destination.fileName(), override, function (e) {
if (e)
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
else
arg.setCode(WebDAVRequest_1.HTTPCodes.Created);
callback();
});
});
});
}
exports.default = default_1;
2 changes: 1 addition & 1 deletion src/resource/IResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface IResource
// ****************************** Actions ****************************** //
create(callback : SimpleCallback)
delete(callback : SimpleCallback)
moveTo(to : FSPath, callback : Return2Callback<FSPath, FSPath>)
moveTo(parent : IResource, newName : string, override : boolean, callback : SimpleCallback)
rename(newName : string, callback : Return2Callback<string, string>)

// ****************************** Tests ****************************** //
Expand Down
31 changes: 27 additions & 4 deletions src/resource/physical/PhysicalResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as fs from 'fs'
export abstract class PhysicalResource extends StandardResource
{
realPath : string
name : string

constructor(realPath : string, parent ?: IResource, fsManager ?: FSManager)
{
Expand All @@ -20,14 +21,35 @@ export abstract class PhysicalResource extends StandardResource
super(parent, fsManager);

this.realPath = path.resolve(realPath);
this.name = path.basename(this.realPath);
}

// ****************************** Actions ****************************** //
abstract create(callback : SimpleCallback)
abstract delete(callback : SimpleCallback)
moveTo(to : FSPath, callback : Return2Callback<FSPath, FSPath>)
moveTo(parent : IResource, newName : string, override : boolean, callback : SimpleCallback)
{
callback(new Error('Not implemented yet.'), null, null);
if(parent === this.parent)
{
this.rename(newName, (e, oldName, newName) => {
callback(e);
})
return;
}

const oldName = this.name;
this.name = newName;
this.removeFromParent((e) => {
if(e)
{
this.name = oldName;
callback(e);
}
else
parent.addChild(this, (e) => {
callback(e);
})
})
}
rename(newName : string, callback : Return2Callback<string, string>)
{
Expand All @@ -40,15 +62,16 @@ export abstract class PhysicalResource extends StandardResource
}
const oldName = path.basename(this.realPath);
this.realPath = newPath;
this.name = newName;
this.updateLastModified();
callback(e, oldName, newName);
callback(null, oldName, newName);
})
}

// ****************************** Std meta-data ****************************** //
webName(callback : ReturnCallback<string>)
{
callback(null, path.basename(this.realPath));
callback(null, path.basename(this.name));
}
abstract type(callback : ReturnCallback<ResourceType>)

Expand Down
2 changes: 1 addition & 1 deletion src/resource/std/StandardResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export abstract class StandardResource implements IResource
// ****************************** Actions ****************************** //
abstract create(callback : SimpleCallback)
abstract delete(callback : SimpleCallback)
abstract moveTo(to : FSPath, callback : Return2Callback<FSPath, FSPath>)
abstract moveTo(parent : IResource, newName : string, override : boolean, callback : SimpleCallback)
abstract rename(newName : string, callback : Return2Callback<string, string>)

// ****************************** Content ****************************** //
Expand Down
24 changes: 22 additions & 2 deletions src/resource/virtual/VirtualResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,29 @@ export abstract class VirtualResource extends StandardResource
{
this.removeFromParent(callback);
}
moveTo(to : FSPath, callback : Return2Callback<FSPath, FSPath>)
moveTo(parent : IResource, newName : string, override : boolean, callback : SimpleCallback)
{
callback(new Error('Not implemented yet.'), null, null);
if(parent === this.parent)
{
this.rename(newName, (e, oldName, newName) => {
callback(e);
})
return;
}

const oldName = this.name;
this.name = newName;
this.removeFromParent((e) => {
if(e)
{
this.name = oldName;
callback(e);
}
else
parent.addChild(this, (e) => {
callback(e);
})
})
}
rename(newName : string, callback : Return2Callback<string, string>)
{
Expand Down
2 changes: 2 additions & 0 deletions src/server/commands/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Propfind from './Propfind'
import Options from './Options'
import Delete from './Delete'
import Mkcol from './Mkcol'
import Move from './Move'
import Head from './Head'
import Post from './Post'
import Put from './Put'
Expand All @@ -16,6 +17,7 @@ export default {
Options,
Delete,
Mkcol,
Move,
Head,
Post,
Put,
Expand Down
39 changes: 39 additions & 0 deletions src/server/commands/Move.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { HTTPCodes, MethodCallArgs, WebDAVRequest } from '../WebDAVRequest'
import { IResource, ResourceType } from '../../resource/Resource'
import { FSPath } from '../../manager/FSManager'

export default function(arg : MethodCallArgs, callback)
{
arg.getResource((e, r) => {
if(e)
{
arg.setCode(HTTPCodes.NotFound)
callback();
return;
}

const override = arg.findHeader('overwrite') === 'T';

let destination : any = arg.findHeader('destination');
if(!destination)
{
arg.setCode(HTTPCodes.BadRequest);
callback();
return;
}

destination = destination.substring(destination.indexOf('://') + '://'.length)
destination = destination.substring(destination.indexOf('/'))
destination = new FSPath(destination)

arg.server.getResourceFromPath(destination.getParent(), (e, rDest) => {
r.moveTo(rDest, destination.fileName(), override, (e) => {
if(e)
arg.setCode(HTTPCodes.InternalServerError)
else
arg.setCode(HTTPCodes.Created)
callback()
})
})
})
}

0 comments on commit 2cb0430

Please sign in to comment.