Skip to content

Commit

Permalink
Added resource events in the system
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed Jun 11, 2017
1 parent b254f51 commit 83bb59e
Show file tree
Hide file tree
Showing 25 changed files with 403 additions and 31 deletions.
3 changes: 3 additions & 0 deletions lib/server/MethodCallArgs.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// <reference types="node" />
import { BasicPrivilege } from '../user/privilege/IPrivilegeManager';
import { IResource, ReturnCallback } from '../resource/IResource';
import { EventsName, DetailsType } from './webDAVServer/Events';
import { XMLElement } from '../helper/XML';
import { WebDAVServer } from './webDAVServer/WebDAVServer';
import { FSPath } from '../manager/FSManager';
Expand Down Expand Up @@ -33,6 +34,8 @@ export declare class MethodCallArgs {
findHeader(name: string, defaultValue?: string): string;
getResource(callback: ReturnCallback<IResource>): void;
dateISO8601(ticks: number): string;
invokeEvent(event: EventsName, subjectResource?: IResource, details?: DetailsType): void;
wrapEvent(event: EventsName, subjectResource?: IResource, details?: DetailsType): () => void;
fullUri(uri?: string): string;
prefixUri(): string;
getResourcePath(resource: IResource, callback: ReturnCallback<string>): void;
Expand Down
13 changes: 13 additions & 0 deletions lib/server/MethodCallArgs.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@ var MethodCallArgs = (function () {
result += h + ':' + m;
return result;
};
MethodCallArgs.prototype.invokeEvent = function (event, subjectResource, details) {
this.server.invoke(event, this, subjectResource, details);
};
MethodCallArgs.prototype.wrapEvent = function (event, subjectResource, details) {
var _this = this;
var oldExit = this.exit;
this.exit = function () {
if (Math.floor(_this.response.statusCode / 100) === 2)
_this.invokeEvent(event, subjectResource, details);
oldExit();
};
return this.exit;
};
MethodCallArgs.prototype.fullUri = function (uri) {
if (uri === void 0) { uri = null; }
if (!uri)
Expand Down
24 changes: 20 additions & 4 deletions lib/server/commands/Copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,29 @@ function copy(arg, source, rDest, destination, callback) {
var dest = rDest.fsManager.newResource(destination.toString(), destination.fileName(), type, rDest);
arg.requirePrivilege(['canCreate', 'canSetProperty', 'canWrite'], dest, function () {
dest.create(function (e) { return _(e, function () {
arg.invokeEvent('create', dest);
rDest.addChild(dest, function (e) { return _(e, function () {
arg.invokeEvent('addChild', rDest, dest);
copyAllProperties(source, dest, function (e) { return _(e, function () {
if (!type.isFile) {
next();
return;
}
source.read(true, function (e, rstream) { return _(e, function () {
dest.write(true, function (e, wstream) { return _(e, function () {
rstream.on('end', next);
rstream.on('end', function () {
arg.invokeEvent('read', source);
next();
});
wstream.on('finish', function () {
arg.invokeEvent('write', dest);
});
rstream.pipe(wstream);
}); });
}); });
function next() {
if (!type.isDirectory) {
arg.invokeEvent('copy', source, dest);
callback(null);
return;
}
Expand All @@ -73,11 +82,14 @@ function copy(arg, source, rDest, destination, callback) {
return;
}
--nb;
if (nb === 0)
if (nb === 0) {
arg.invokeEvent('copy', source, dest);
callback(null);
}
}
if (nb === 0) {
callback(null);
arg.invokeEvent('copy', source, dest);
return;
}
children.forEach(function (child) {
Expand Down Expand Up @@ -114,8 +126,11 @@ function default_1(arg, callback) {
callback();
return;
}
destination = destination.substring(destination.indexOf('://') + '://'.length);
destination = destination.substring(destination.indexOf('/'));
var startIndex = destination.indexOf('://');
if (startIndex !== -1) {
destination = destination.substring(startIndex + '://'.length);
destination = destination.substring(destination.indexOf('/'));
}
destination = new FSManager_1.FSPath(destination);
arg.server.getResourceFromPath(destination.getParent(), function (e, rDest) {
if (e) {
Expand Down Expand Up @@ -174,6 +189,7 @@ function default_1(arg, callback) {
callback(e);
return;
}
arg.invokeEvent('delete', destCollision);
done(true);
}); });
}); });
Expand Down
4 changes: 3 additions & 1 deletion lib/server/commands/Delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ function default_1(arg, callback) {
r.delete(function (e) { return process.nextTick(function () {
if (e)
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
else
else {
arg.setCode(WebDAVRequest_1.HTTPCodes.OK);
arg.invokeEvent('delete', r);
}
callback();
}); });
});
Expand Down
1 change: 1 addition & 0 deletions lib/server/commands/Get.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ function default_1(arg, callback) {
callback();
}
else {
arg.invokeEvent('read', r);
var range = arg.findHeader('Range');
if (range) {
var rex = /([0-9]+)/g;
Expand Down
31 changes: 23 additions & 8 deletions lib/server/commands/Lock.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,20 @@ function createLock(arg, callback) {
callback();
return;
}
arg.invokeEvent('create', resource);
r.addChild(resource, function (e) {
if (e)
if (e) {
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
else
arg.setCode(WebDAVRequest_1.HTTPCodes.Created);
callback();
callback();
}
else {
arg.invokeEvent('addChild', r, resource);
writeLock(resource, function () {
arg.setCode(WebDAVRequest_1.HTTPCodes.Created);
arg.writeXML(createResponse(arg, lock_1));
callback();
});
}
});
}); });
});
Expand All @@ -75,20 +83,26 @@ function createLock(arg, callback) {
callback();
return;
}
arg.checkIfHeader(r, function () {
function writeLock(r, cb) {
arg.requirePrivilege(['canSetLock'], r, function () {
r.setLock(lock_1, function (e) { return process.nextTick(function () {
if (e) {
arg.setCode(WebDAVRequest_1.HTTPCodes.Locked);
callback();
return;
}
arg.invokeEvent('lock', r, lock_1);
arg.response.setHeader('Lock-Token', lock_1.uuid);
arg.setCode(WebDAVRequest_1.HTTPCodes.OK);
arg.writeXML(createResponse(arg, lock_1));
callback();
cb();
}); });
});
}
arg.checkIfHeader(r, function () {
writeLock(r, function () {
arg.setCode(WebDAVRequest_1.HTTPCodes.OK);
arg.writeXML(createResponse(arg, lock_1));
callback();
});
});
});
}
Expand All @@ -113,6 +127,7 @@ function refreshLock(arg, lockUUID, callback) {
return;
}
lock.refresh();
arg.invokeEvent('refreshLock', r, lock);
arg.setCode(WebDAVRequest_1.HTTPCodes.OK);
arg.writeXML(createResponse(arg, lock));
callback();
Expand Down
5 changes: 4 additions & 1 deletion lib/server/commands/Mkcol.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ function default_1(arg, callback) {
callback();
return;
}
arg.invokeEvent('create', resource);
r.addChild(resource, function (e) { return process.nextTick(function () {
if (e)
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
else
else {
arg.invokeEvent('addChild', r, resource);
arg.setCode(WebDAVRequest_1.HTTPCodes.Created);
}
callback();
}); });
}); });
Expand Down
4 changes: 3 additions & 1 deletion lib/server/commands/Move.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ function default_1(arg, callback) {
r.moveTo(rDest, destination.fileName(), overwrite, function (e) { return process.nextTick(function () {
if (e)
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
else
else {
arg.invokeEvent('move', r, destination);
arg.setCode(WebDAVRequest_1.HTTPCodes.Created);
}
callback();
}); });
});
Expand Down
23 changes: 18 additions & 5 deletions lib/server/commands/Put.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ function createResource(arg, callback, validCallback) {
callback();
return;
}
arg.invokeEvent('create', resource);
r.addChild(resource, function (e) { return process.nextTick(function () {
if (e) {
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
callback();
}
else
else {
arg.invokeEvent('addChild', r, resource);
validCallback(resource);
}
}); });
}); });
});
Expand All @@ -55,8 +58,10 @@ function unchunkedMethod(arg, callback) {
stream.end();
if (e)
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
else
else {
arg.invokeEvent('write', r);
arg.setCode(WebDAVRequest_1.HTTPCodes.OK);
}
callback();
}); });
});
Expand All @@ -79,8 +84,10 @@ function unchunkedMethod(arg, callback) {
stream.end(arg.data, function (e) {
if (e)
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
else
else {
arg.invokeEvent('write', r);
arg.setCode(WebDAVRequest_1.HTTPCodes.Created);
}
callback();
});
}); });
Expand Down Expand Up @@ -108,8 +115,10 @@ function unchunkedMethod(arg, callback) {
stream.end(arg.data, function (e) {
if (e)
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
else
else {
arg.invokeEvent('write', r);
arg.setCode(WebDAVRequest_1.HTTPCodes.OK);
}
callback();
});
}); });
Expand Down Expand Up @@ -137,8 +146,10 @@ unchunkedMethod.chunked = function (arg, callback) {
stream.end();
if (e)
arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError);
else
else {
arg.invokeEvent('write', r);
arg.setCode(WebDAVRequest_1.HTTPCodes.OK);
}
callback();
}); });
});
Expand All @@ -161,6 +172,7 @@ unchunkedMethod.chunked = function (arg, callback) {
arg.request.pipe(stream);
stream.on('finish', function (e) {
arg.setCode(WebDAVRequest_1.HTTPCodes.Created);
arg.invokeEvent('write', r);
callback();
});
stream.on('error', function (e) {
Expand Down Expand Up @@ -192,6 +204,7 @@ unchunkedMethod.chunked = function (arg, callback) {
arg.request.pipe(stream);
stream.on('finish', function (e) {
arg.setCode(WebDAVRequest_1.HTTPCodes.OK);
arg.invokeEvent('write', r);
callback();
});
stream.on('error', function (e) {
Expand Down
4 changes: 3 additions & 1 deletion lib/server/commands/Unlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ function default_1(arg, callback) {
r.removeLock(lock.uuid, function (e, done) {
if (e || !done)
arg.setCode(WebDAVRequest_1.HTTPCodes.Forbidden);
else
else {
arg.invokeEvent('unlock', r, lock);
arg.setCode(WebDAVRequest_1.HTTPCodes.NoContent);
}
callback();
});
});
Expand Down
15 changes: 15 additions & 0 deletions lib/server/webDAVServer/Events.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MethodCallArgs } from '../WebDAVRequest';
import { XMLElement } from '../../helper/XML';
import { IResource } from '../../resource/IResource';
import { FSPath } from '../../manager/FSPath';
import { Lock } from '../../resource/lock/Lock';
export declare type EventsName = 'create' | 'delete' | 'copy' | 'move' | 'lock' | 'refreshLock' | 'unlock' | 'setProperty' | 'removeProperty' | 'write' | 'read' | 'addChild';
export declare type DetailsType = IResource | FSPath | Lock | XMLElement;
export declare type Listener = (arg: MethodCallArgs, subjectResource: IResource, details?: DetailsType) => void;
export declare function invoke(event: EventsName, arg: MethodCallArgs, subjectResource?: IResource, details?: DetailsType): void;
export declare function register(event: EventsName, listener: Listener): void;
export declare function registerWithName(event: EventsName, name: string, listener: Listener): void;
export declare function clear(event: EventsName): void;
export declare function clearAll(event: EventsName): void;
export declare function remove(event: EventsName, listener: Listener): void;
export declare function removeByName(event: EventsName, name: string): void;
54 changes: 54 additions & 0 deletions lib/server/webDAVServer/Events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function getEventBag(_this, event) {
if (!_this.__events)
_this.__events = {};
if (event && !_this.__events[event]) {
_this.__events[event] = {
all: [],
named: {}
};
return _this.__events[event];
}
return event ? _this.__events[event] : _this.__events;
}
function invoke(event, arg, subjectResource, details) {
var events = getEventBag(this, event);
events.all.forEach(function (e) { return process.nextTick(function () { return e(arg, subjectResource, details); }); });
}
exports.invoke = invoke;
function register(event, listener) {
var events = getEventBag(this, event);
events.all.push(listener);
}
exports.register = register;
function registerWithName(event, name, listener) {
var events = getEventBag(this, event);
events.all.push(listener);
events.named[name] = listener;
}
exports.registerWithName = registerWithName;
function clear(event) {
var events = getEventBag(this, event);
events.all = [];
events.named = {};
}
exports.clear = clear;
function clearAll(event) {
this.__events = {};
}
exports.clearAll = clearAll;
function remove(event, listener) {
var events = getEventBag(this, event);
events.all.indexOf(listener);
}
exports.remove = remove;
function removeByName(event, name) {
var events = getEventBag(this, event);
var listener = events.named[name];
if (listener) {
delete events.named[name];
events.all.splice(events.all.indexOf(listener), 1);
}
}
exports.removeByName = removeByName;
Loading

0 comments on commit 83bb59e

Please sign in to comment.