Skip to content

Commit

Permalink
Added 'before-*' file system events
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed Aug 19, 2017
1 parent 48d842f commit 8763396
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
17 changes: 13 additions & 4 deletions lib/manager/v2/fileSystem/FileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ var FileSystem = (function () {
};
if (!this._create)
return callback(Errors_1.Errors.InvalidOperation);
this.emit('before-create', ctx, path, { type: type, createIntermediates: createIntermediates });
issuePrivilegeCheck(this, ctx, path, 'canWrite', callback, function () {
var go = function () {
_this._create(path, {
Expand Down Expand Up @@ -229,6 +230,7 @@ var FileSystem = (function () {
};
if (!this._delete)
return callback(Errors_1.Errors.InvalidOperation);
this.emit('before-delete', ctx, path, { depth: depth });
issuePrivilegeCheck(this, ctx, path, 'canWrite', callback, function () {
_this.isLocked(ctx, path, function (e, isLocked) {
if (e || isLocked)
Expand Down Expand Up @@ -272,6 +274,7 @@ var FileSystem = (function () {
};
if (!this._openWriteStream)
return callback(Errors_1.Errors.InvalidOperation);
this.emit('before-openWriteStream', ctx, path, { targetSource: targetSource, mode: mode, estimatedSize: estimatedSize, created: created });
issuePrivilegeCheck(this, ctx, path, targetSource ? 'canWriteContentSource' : 'canWriteContentTranslated', callback, function () {
_this.isLocked(ctx, path, function (e, isLocked) {
if (e || isLocked)
Expand Down Expand Up @@ -327,6 +330,7 @@ var FileSystem = (function () {
_this.emit('openReadStream', ctx, path, { targetSource: targetSource, estimatedSize: estimatedSize });
callbackFinal(e, stream);
};
this.emit('before-openReadStream', ctx, path, { targetSource: targetSource, estimatedSize: estimatedSize });
issuePrivilegeCheck(this, ctx, path, targetSource ? 'canReadContentSource' : 'canReadContentTranslated', callback, function () {
_this.fastExistCheckEx(ctx, path, callback, function () {
if (!_this._openReadStream)
Expand All @@ -350,6 +354,7 @@ var FileSystem = (function () {
_this.emit('move', ctx, pathFrom, { pathFrom: pathFrom, pathTo: pathTo, overwrite: overwrite, overrided: overrided });
callbackFinal(e, overrided);
};
this.emit('before-move', ctx, pathFrom, { pathFrom: pathFrom, pathTo: pathTo, overwrite: overwrite });
issuePrivilegeCheck(this, ctx, pathFrom, 'canRead', callback, function () {
issuePrivilegeCheck(_this, ctx, pathTo, 'canWrite', callback, function () {
_this.isLocked(ctx, pathFrom, function (e, isLocked) {
Expand Down Expand Up @@ -391,6 +396,7 @@ var FileSystem = (function () {
_this.emit('copy', ctx, pathFrom, { pathTo: pathTo, overwrite: overwrite, overrided: overrided, depth: depth });
callbackFinal(e, overrided);
};
this.emit('before-copy', ctx, pathFrom, { pathTo: pathTo, overwrite: overwrite, depth: depth });
issuePrivilegeCheck(this, ctx, pathFrom, 'canRead', callback, function () {
issuePrivilegeCheck(_this, ctx, pathTo, 'canWrite', callback, function () {
_this.isLocked(ctx, pathTo, function (e, isLocked) {
Expand Down Expand Up @@ -424,12 +430,10 @@ var FileSystem = (function () {
var pathFrom = new Path_1.Path(_pathFrom);
var callback = function (e, overrided) {
if (!e)
_this.emit('rename', ctx, pathFrom, {
newName: newName,
overrided: overrided
});
_this.emit('rename', ctx, pathFrom, { newName: newName, overrided: overrided });
callbackFinal(e, overrided);
};
this.emit('before-rename', ctx, pathFrom, { newName: newName });
issuePrivilegeCheck(this, ctx, pathFrom, ['canRead', 'canWrite'], callback, function () {
_this.isLocked(ctx, pathFrom, function (e, isLocked) {
if (e || isLocked)
Expand Down Expand Up @@ -564,6 +568,7 @@ var FileSystem = (function () {
});
},
setLock: function (lock, callback) {
fs.emit('before-lock-set', ctx, pPath, { lock: lock });
issuePrivilegeCheck(fs, ctx, pPath, 'canWriteLocks', callback, function () {
buffIsLocked.isLocked(function (e, isLocked) {
if (e || isLocked)
Expand All @@ -577,6 +582,7 @@ var FileSystem = (function () {
});
},
removeLock: function (uuid, callback) {
fs.emit('before-lock-remove', ctx, pPath, { uuid: uuid });
issuePrivilegeCheck(fs, ctx, pPath, 'canWriteLocks', callback, function () {
buffIsLocked.isLocked(function (e, isLocked) {
if (e || isLocked)
Expand All @@ -595,6 +601,7 @@ var FileSystem = (function () {
});
},
refresh: function (uuid, timeout, callback) {
fs.emit('before-lock-refresh', ctx, pPath, { uuid: uuid, timeout: timeout });
issuePrivilegeCheck(fs, ctx, pPath, 'canWriteLocks', callback, function () {
buffIsLocked.isLocked(function (e, isLocked) {
if (e || isLocked)
Expand Down Expand Up @@ -631,6 +638,7 @@ var FileSystem = (function () {
var fs = _this;
callback(null, {
setProperty: function (name, value, attributes, callback) {
fs.emit('before-property-set', ctx, pPath, { name: name, value: value, attributes: attributes });
issuePrivilegeCheck(fs, ctx, pPath, 'canWriteProperties', callback, function () {
buffIsLocked.isLocked(function (e, isLocked) {
if (e || isLocked)
Expand All @@ -649,6 +657,7 @@ var FileSystem = (function () {
});
},
removeProperty: function (name, callback) {
fs.emit('before-property-remove', ctx, pPath, { name: name });
issuePrivilegeCheck(fs, ctx, pPath, 'canWriteProperties', callback, function () {
buffIsLocked.isLocked(function (e, isLocked) {
if (e || isLocked)
Expand Down
2 changes: 1 addition & 1 deletion lib/server/v2/webDAVServer/WebDAVServer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import * as startStop from './StartStop';
import * as https from 'https';
import * as http from 'http';
export declare type WebDAVServerStartCallback = (server?: http.Server) => void;
export declare type FileSystemEvent = 'create' | 'delete' | 'openReadStream' | 'openWriteStream' | 'move' | 'copy' | 'rename';
export declare type FileSystemEvent = 'create' | 'delete' | 'openReadStream' | 'openWriteStream' | 'move' | 'copy' | 'rename' | 'before-create' | 'before-delete' | 'before-openReadStream' | 'before-openWriteStream' | 'before-move' | 'before-copy' | 'before-rename';
export declare type ServerEvent = FileSystemEvent;
export declare type EventCallback = (ctx: RequestContext, fs: FileSystem, path: Path, data?: any) => void;
export declare class WebDAVServer {
Expand Down
24 changes: 20 additions & 4 deletions src/manager/v2/fileSystem/FileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ export abstract class FileSystem implements ISerializableFileSystem

if(!this._create)
return callback(Errors.InvalidOperation);

this.emit('before-create', ctx, path, { type, createIntermediates })

issuePrivilegeCheck(this, ctx, path, 'canWrite', callback, () => {
const go = () => {
Expand Down Expand Up @@ -324,6 +326,8 @@ export abstract class FileSystem implements ISerializableFileSystem
if(!this._delete)
return callback(Errors.InvalidOperation);

this.emit('before-delete', ctx, path, { depth })

issuePrivilegeCheck(this, ctx, path, 'canWrite', callback, () => {
this.isLocked(ctx, path, (e, isLocked) => {
if(e || isLocked)
Expand Down Expand Up @@ -445,6 +449,8 @@ export abstract class FileSystem implements ISerializableFileSystem

if(!this._openWriteStream)
return callback(Errors.InvalidOperation);

this.emit('before-openWriteStream', ctx, path, { targetSource, mode, estimatedSize, created })

issuePrivilegeCheck(this, ctx, path, targetSource ? 'canWriteContentSource' : 'canWriteContentTranslated', callback, () => {
this.isLocked(ctx, path, (e, isLocked) => {
Expand Down Expand Up @@ -550,6 +556,8 @@ export abstract class FileSystem implements ISerializableFileSystem
this.emit('openReadStream', ctx, path, { targetSource, estimatedSize })
callbackFinal(e, stream);
}

this.emit('before-openReadStream', ctx, path, { targetSource, estimatedSize })

issuePrivilegeCheck(this, ctx, path, targetSource ? 'canReadContentSource' : 'canReadContentTranslated', callback, () => {
this.fastExistCheckEx(ctx, path, callback, () => {
Expand Down Expand Up @@ -598,6 +606,8 @@ export abstract class FileSystem implements ISerializableFileSystem
callbackFinal(e, overrided);
}

this.emit('before-move', ctx, pathFrom, { pathFrom, pathTo, overwrite })

issuePrivilegeCheck(this, ctx, pathFrom, 'canRead', callback, () => {
issuePrivilegeCheck(this, ctx, pathTo, 'canWrite', callback, () => {
this.isLocked(ctx, pathFrom, (e, isLocked) => {
Expand Down Expand Up @@ -688,6 +698,8 @@ export abstract class FileSystem implements ISerializableFileSystem
this.emit('copy', ctx, pathFrom, { pathTo, overwrite, overrided, depth })
callbackFinal(e, overrided);
}

this.emit('before-copy', ctx, pathFrom, { pathTo, overwrite, depth })

issuePrivilegeCheck(this, ctx, pathFrom, 'canRead', callback, () => {
issuePrivilegeCheck(this, ctx, pathTo, 'canWrite', callback, () => {
Expand Down Expand Up @@ -751,12 +763,11 @@ export abstract class FileSystem implements ISerializableFileSystem

const callback : ReturnCallback<boolean> = (e, overrided) => {
if(!e)
this.emit('rename', ctx, pathFrom, {
newName,
overrided
})
this.emit('rename', ctx, pathFrom, { newName, overrided })
callbackFinal(e, overrided);
}

this.emit('before-rename', ctx, pathFrom, { newName })

issuePrivilegeCheck(this, ctx, pathFrom, [ 'canRead', 'canWrite' ], callback, () => {
this.isLocked(ctx, pathFrom, (e, isLocked) => {
Expand Down Expand Up @@ -962,6 +973,7 @@ export abstract class FileSystem implements ISerializableFileSystem
},
setLock(lock : Lock, callback : SimpleCallback) : void
{
fs.emit('before-lock-set', ctx, pPath, { lock });
issuePrivilegeCheck(fs, ctx, pPath, 'canWriteLocks', callback, () => {
buffIsLocked.isLocked((e, isLocked) => {
if(e || isLocked)
Expand All @@ -977,6 +989,7 @@ export abstract class FileSystem implements ISerializableFileSystem
},
removeLock(uuid : string, callback : ReturnCallback<boolean>) : void
{
fs.emit('before-lock-remove', ctx, pPath, { uuid });
issuePrivilegeCheck(fs, ctx, pPath, 'canWriteLocks', callback, () => {
buffIsLocked.isLocked((e, isLocked) => {
if(e || isLocked)
Expand All @@ -998,6 +1011,7 @@ export abstract class FileSystem implements ISerializableFileSystem
},
refresh(uuid : string, timeout : number, callback : ReturnCallback<Lock>) : void
{
fs.emit('before-lock-refresh', ctx, pPath, { uuid, timeout });
issuePrivilegeCheck(fs, ctx, pPath, 'canWriteLocks', callback, () => {
buffIsLocked.isLocked((e, isLocked) => {
if(e || isLocked)
Expand Down Expand Up @@ -1041,6 +1055,7 @@ export abstract class FileSystem implements ISerializableFileSystem
callback(null, {
setProperty(name : string, value : ResourcePropertyValue, attributes : PropertyAttributes, callback : SimpleCallback) : void
{
fs.emit('before-property-set', ctx, pPath, { name, value, attributes });
issuePrivilegeCheck(fs, ctx, pPath, 'canWriteProperties', callback, () => {
buffIsLocked.isLocked((e, isLocked) => {
if(e || isLocked)
Expand All @@ -1062,6 +1077,7 @@ export abstract class FileSystem implements ISerializableFileSystem
},
removeProperty(name : string, callback : SimpleCallback) : void
{
fs.emit('before-property-remove', ctx, pPath, { name });
issuePrivilegeCheck(fs, ctx, pPath, 'canWriteProperties', callback, () => {
buffIsLocked.isLocked((e, isLocked) => {
if(e || isLocked)
Expand Down
3 changes: 2 additions & 1 deletion src/server/v2/webDAVServer/WebDAVServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import * as http from 'http'

export type WebDAVServerStartCallback = (server ?: http.Server) => void;

export type FileSystemEvent = 'create' | 'delete' | 'openReadStream' | 'openWriteStream' | 'move' | 'copy' | 'rename';
export type FileSystemEvent = 'create' | 'delete' | 'openReadStream' | 'openWriteStream' | 'move' | 'copy' | 'rename' |
'before-create' | 'before-delete' | 'before-openReadStream' | 'before-openWriteStream' | 'before-move' | 'before-copy' | 'before-rename';
export type ServerEvent = FileSystemEvent;
export type EventCallback = (ctx : RequestContext, fs : FileSystem, path : Path, data ?: any) => void;

Expand Down

0 comments on commit 8763396

Please sign in to comment.