Skip to content

Commit 9a5688c

Browse files
committed
Added file system wide events
1 parent f8d8969 commit 9a5688c

File tree

3 files changed

+264
-26
lines changed

3 files changed

+264
-26
lines changed

lib/manager/v2/fileSystem/FileSystem.d.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { PrivilegeManagerInfo, AvailableLocksInfo, CopyInfo, CreateInfo, CreationDateInfo, DeleteInfo, DisplayNameInfo, ETagInfo, LastModifiedDateInfo, LockManagerInfo, MimeTypeInfo, MoveInfo, OpenReadStreamInfo, OpenWriteStreamInfo, PropertyManagerInfo, ReadDirInfo, RenameInfo, SizeInfo, TypeInfo } from './ContextInfo';
33
import { Readable, Writable } from 'stream';
44
import { RequestContext } from '../../../server/v2/RequestContext';
5+
import { FileSystemEvent, WebDAVServer } from '../../../server/v2/webDAVServer/WebDAVServer';
56
import { BasicPrivilege, PrivilegeManager } from '../../../user/v2/privilege/PrivilegeManager';
67
import { LockKind } from '../../../resource/lock/LockKind';
78
import { Lock } from '../../../resource/lock/Lock';
@@ -598,4 +599,44 @@ export declare abstract class FileSystem implements ISerializableFileSystem {
598599
* @param callback Returns the serialized data or an error.
599600
*/
600601
serialize(callback: ReturnCallback<any>): void;
602+
/**
603+
* Attach a listener to an event.
604+
*
605+
* @param server Server in which the event can happen.
606+
* @param event Name of the event.
607+
* @param listener Listener of the event.
608+
*/
609+
on(server: WebDAVServer, event: FileSystemEvent, listener: (ctx: RequestContext, path: Path, data?: any) => void): this;
610+
/**
611+
* Attach a listener to an event.
612+
*
613+
* @param server Server in which the event can happen.
614+
* @param event Name of the event.
615+
* @param listener Listener of the event.
616+
*/
617+
on(server: WebDAVServer, event: string, listener: (ctx: RequestContext, path: Path, data?: any) => void): this;
618+
/**
619+
* Attach a listener to an event.
620+
*
621+
* @param ctx Context containing the server in which the event can happen.
622+
* @param event Name of the event.
623+
* @param listener Listener of the event.
624+
*/
625+
on(ctx: RequestContext, event: FileSystemEvent, listener: (ctx: RequestContext, path: Path, data?: any) => void): this;
626+
/**
627+
* Attach a listener to an event.
628+
*
629+
* @param ctx Context containing the server in which the event can happen.
630+
* @param event Name of the event.
631+
* @param listener Listener of the event.
632+
*/
633+
on(ctx: RequestContext, event: string, listener: (ctx: RequestContext, path: Path, data?: any) => void): this;
634+
/**
635+
* Trigger an event.
636+
*
637+
* @param event Name of the event.
638+
* @param ctx Context of the event.
639+
* @param path Path of the resource on which the event happened.
640+
*/
641+
emit(event: string, ctx: RequestContext, path: Path | string, data?: any): void;
601642
}

lib/manager/v2/fileSystem/FileSystem.js

Lines changed: 90 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,13 @@ var FileSystem = (function () {
142142
FileSystem.prototype.create = function (ctx, _path, type, _createIntermediates, _callback) {
143143
var _this = this;
144144
var createIntermediates = _callback ? _createIntermediates : false;
145-
var callback = _callback ? _callback : _createIntermediates;
145+
var callbackFinal = _callback ? _callback : _createIntermediates;
146146
var path = new Path_1.Path(_path);
147+
var callback = function (e) {
148+
if (!e)
149+
_this.emit('create', ctx, path, { type: type, createIntermediates: createIntermediates });
150+
callbackFinal(e);
151+
};
147152
if (!this._create)
148153
return callback(Errors_1.Errors.InvalidOperation);
149154
issuePrivilegeCheck(this, ctx, path, 'canWrite', callback, function () {
@@ -215,8 +220,13 @@ var FileSystem = (function () {
215220
FileSystem.prototype.delete = function (ctx, _path, _depth, _callback) {
216221
var _this = this;
217222
var depth = _callback ? _depth : -1;
218-
var callback = _callback ? _callback : _depth;
223+
var callbackFinal = _callback ? _callback : _depth;
219224
var path = new Path_1.Path(_path);
225+
var callback = function (e) {
226+
if (!e)
227+
_this.emit('delete', ctx, path, { depth: depth });
228+
callbackFinal(e);
229+
};
220230
if (!this._delete)
221231
return callback(Errors_1.Errors.InvalidOperation);
222232
issuePrivilegeCheck(this, ctx, path, 'canWrite', callback, function () {
@@ -246,15 +256,20 @@ var FileSystem = (function () {
246256
if (obj && obj.constructor === Number)
247257
estimatedSize = obj;
248258
}
249-
var callback;
259+
var callbackFinal;
250260
for (var _d = 0, _e = [_mode, _targetSource, _estimatedSize, _callback]; _d < _e.length; _d++) {
251261
var obj = _e[_d];
252262
if (obj && obj.constructor === Function)
253-
callback = obj;
263+
callbackFinal = obj;
254264
}
255265
var mode = _mode && _mode.constructor === String ? _mode : 'mustExist';
256266
var path = new Path_1.Path(_path);
257267
var created = false;
268+
var callback = function (e, stream, created) {
269+
if (!e)
270+
_this.emit('openWriteStream', ctx, path, { targetSource: targetSource, mode: mode, estimatedSize: estimatedSize, created: created });
271+
callbackFinal(e, stream, created);
272+
};
258273
if (!this._openWriteStream)
259274
return callback(Errors_1.Errors.InvalidOperation);
260275
issuePrivilegeCheck(this, ctx, path, targetSource ? 'canWriteContentSource' : 'canWriteContentTranslated', callback, function () {
@@ -305,8 +320,13 @@ var FileSystem = (function () {
305320
var _this = this;
306321
var targetSource = _targetSource.constructor === Boolean ? _targetSource : false;
307322
var estimatedSize = _callback ? _estimatedSize : _estimatedSize ? _targetSource : -1;
308-
var callback = _callback ? _callback : _estimatedSize ? _estimatedSize : _targetSource;
323+
var callbackFinal = _callback ? _callback : _estimatedSize ? _estimatedSize : _targetSource;
309324
var path = new Path_1.Path(_path);
325+
var callback = function (e, stream) {
326+
if (!e)
327+
_this.emit('openReadStream', ctx, path, { targetSource: targetSource, estimatedSize: estimatedSize });
328+
callbackFinal(e, stream);
329+
};
310330
issuePrivilegeCheck(this, ctx, path, targetSource ? 'canReadContentSource' : 'canReadContentTranslated', callback, function () {
311331
_this.fastExistCheckEx(ctx, path, callback, function () {
312332
if (!_this._openReadStream)
@@ -321,10 +341,15 @@ var FileSystem = (function () {
321341
};
322342
FileSystem.prototype.move = function (ctx, _pathFrom, _pathTo, _overwrite, _callback) {
323343
var _this = this;
324-
var callback = _callback ? _callback : _overwrite;
344+
var callbackFinal = _callback ? _callback : _overwrite;
325345
var overwrite = _callback ? _overwrite : false;
326346
var pathFrom = new Path_1.Path(_pathFrom);
327347
var pathTo = new Path_1.Path(_pathTo);
348+
var callback = function (e, overrided) {
349+
if (!e)
350+
_this.emit('move', ctx, pathFrom, { pathFrom: pathFrom, pathTo: pathTo, overwrite: overwrite, overrided: overrided });
351+
callbackFinal(e, overrided);
352+
};
328353
issuePrivilegeCheck(this, ctx, pathFrom, 'canRead', callback, function () {
329354
issuePrivilegeCheck(_this, ctx, pathTo, 'canWrite', callback, function () {
330355
_this.isLocked(ctx, pathFrom, function (e, isLocked) {
@@ -358,9 +383,14 @@ var FileSystem = (function () {
358383
var _this = this;
359384
var overwrite = _overwrite.constructor === Boolean ? _overwrite : false;
360385
var depth = _callback ? _depth : !_depth ? -1 : _overwrite.constructor === Number ? _overwrite : -1;
361-
var callback = _callback ? _callback : _depth ? _depth : _overwrite;
386+
var callbackFinal = _callback ? _callback : _depth ? _depth : _overwrite;
362387
var pathFrom = new Path_1.Path(_pathFrom);
363388
var pathTo = new Path_1.Path(_pathTo);
389+
var callback = function (e, overrided) {
390+
if (!e)
391+
_this.emit('copy', ctx, pathFrom, { pathTo: pathTo, overwrite: overwrite, overrided: overrided, depth: depth });
392+
callbackFinal(e, overrided);
393+
};
364394
issuePrivilegeCheck(this, ctx, pathFrom, 'canRead', callback, function () {
365395
issuePrivilegeCheck(_this, ctx, pathTo, 'canWrite', callback, function () {
366396
_this.isLocked(ctx, pathTo, function (e, isLocked) {
@@ -390,8 +420,16 @@ var FileSystem = (function () {
390420
FileSystem.prototype.rename = function (ctx, _pathFrom, newName, _overwrite, _callback) {
391421
var _this = this;
392422
var overwrite = _callback ? _overwrite : false;
393-
var callback = _callback ? _callback : _overwrite;
423+
var callbackFinal = _callback ? _callback : _overwrite;
394424
var pathFrom = new Path_1.Path(_pathFrom);
425+
var callback = function (e, overrided) {
426+
if (!e)
427+
_this.emit('rename', ctx, pathFrom, {
428+
newName: newName,
429+
overrided: overrided
430+
});
431+
callbackFinal(e, overrided);
432+
};
395433
issuePrivilegeCheck(this, ctx, pathFrom, ['canRead', 'canWrite'], callback, function () {
396434
_this.isLocked(ctx, pathFrom, function (e, isLocked) {
397435
if (e || isLocked)
@@ -530,7 +568,11 @@ var FileSystem = (function () {
530568
buffIsLocked.isLocked(function (e, isLocked) {
531569
if (e || isLocked)
532570
return callback(e ? e : Errors_1.Errors.Locked);
533-
lm.setLock(lock, callback);
571+
lm.setLock(lock, function (e) {
572+
if (!e)
573+
fs.emit('lock-set', ctx, pPath, { lock: lock });
574+
callback(e);
575+
});
534576
});
535577
});
536578
},
@@ -539,7 +581,11 @@ var FileSystem = (function () {
539581
buffIsLocked.isLocked(function (e, isLocked) {
540582
if (e || isLocked)
541583
return callback(e ? e : Errors_1.Errors.Locked);
542-
lm.removeLock(uuid, callback);
584+
lm.removeLock(uuid, function (e, removed) {
585+
if (!e)
586+
fs.emit('lock-remove', ctx, pPath, { uuid: uuid, removed: removed });
587+
callback(e, removed);
588+
});
543589
});
544590
});
545591
},
@@ -553,7 +599,11 @@ var FileSystem = (function () {
553599
buffIsLocked.isLocked(function (e, isLocked) {
554600
if (e || isLocked)
555601
return callback(e ? e : Errors_1.Errors.Locked);
556-
lm.refresh(uuid, timeout, callback);
602+
lm.refresh(uuid, timeout, function (e, lock) {
603+
if (!e)
604+
fs.emit('lock-refresh', ctx, pPath, { uuid: uuid, timeout: timeout, lock: lock });
605+
callback(e);
606+
});
557607
});
558608
});
559609
}
@@ -585,7 +635,11 @@ var FileSystem = (function () {
585635
buffIsLocked.isLocked(function (e, isLocked) {
586636
if (e || isLocked)
587637
return callback(e ? e : Errors_1.Errors.Locked);
588-
pm.setProperty(name, value, attributes, callback);
638+
pm.setProperty(name, value, attributes, function (e) {
639+
if (!e)
640+
fs.emit('property-set', ctx, pPath, { name: name, value: value, attributes: attributes });
641+
callback(e);
642+
});
589643
});
590644
});
591645
},
@@ -599,7 +653,11 @@ var FileSystem = (function () {
599653
buffIsLocked.isLocked(function (e, isLocked) {
600654
if (e || isLocked)
601655
return callback(e ? e : Errors_1.Errors.Locked);
602-
pm.removeProperty(name, callback);
656+
pm.removeProperty(name, function (e) {
657+
if (!e)
658+
fs.emit('property-remove', ctx, pPath, { name: name });
659+
callback(e);
660+
});
603661
});
604662
});
605663
},
@@ -964,6 +1022,25 @@ var FileSystem = (function () {
9641022
return callback();
9651023
serializer.serialize(this, callback);
9661024
};
1025+
FileSystem.prototype.on = function (ctx, event, listener) {
1026+
var _this = this;
1027+
var server = ctx.events ? ctx : ctx.server;
1028+
server.on(event, function (ctx, fs, path) {
1029+
if (fs === _this)
1030+
listener(ctx, path);
1031+
});
1032+
return this;
1033+
};
1034+
/**
1035+
* Trigger an event.
1036+
*
1037+
* @param event Name of the event.
1038+
* @param ctx Context of the event.
1039+
* @param path Path of the resource on which the event happened.
1040+
*/
1041+
FileSystem.prototype.emit = function (event, ctx, path, data) {
1042+
ctx.server.emit(event, ctx, this, path, data);
1043+
};
9671044
return FileSystem;
9681045
}());
9691046
exports.FileSystem = FileSystem;

0 commit comments

Comments
 (0)