Skip to content

Commit

Permalink
Implemented the static methods 'standardMoveWithoutCopy', 'standardMo…
Browse files Browse the repository at this point in the history
…veByCopy', 'standardFindChildByName', 'standardFindChildren' and 'standardAddToParent' in the 'StandardResource' class + Use the 'standardMoveByCopy' in 'standardMoveTo' static method + Implemented a default method for 'moveTo' in the 'StandardResource' class
  • Loading branch information
AdrienCastex committed Jun 20, 2017
1 parent 1a38d52 commit 2bb7a4b
Show file tree
Hide file tree
Showing 3 changed files with 483 additions and 109 deletions.
16 changes: 13 additions & 3 deletions lib/resource/std/StandardResource.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import { LockBag } from '../lock/LockBag';
import { Lock } from '../lock/Lock';
export declare abstract class StandardResource implements IResource {
static sizeOfSubFiles(resource: IResource, targetSource: boolean, callback: ReturnCallback<number>): void;
dateLastModified: number;
deleteOnMoved: boolean;
dateCreation: number;
properties: object;
fsManager: FSManager;
lockBag: LockBag;
parent: IResource;
dateCreation: number;
dateLastModified: number;
constructor(parent: IResource, fsManager: FSManager);
getAvailableLocks(callback: ReturnCallback<LockKind[]>): void;
getLocks(callback: ReturnCallback<Lock[]>): void;
Expand All @@ -26,8 +27,8 @@ export declare abstract class StandardResource implements IResource {
getProperties(callback: ReturnCallback<object>): void;
abstract create(callback: SimpleCallback): any;
abstract delete(callback: SimpleCallback): any;
abstract moveTo(parent: IResource, newName: string, overwrite: boolean, callback: SimpleCallback): any;
abstract rename(newName: string, callback: Return2Callback<string, string>): any;
moveTo(parent: IResource, newName: string, overwrite: boolean, callback: SimpleCallback): void;
abstract write(targetSource: boolean, callback: ReturnCallback<Writable>): any;
abstract read(targetSource: boolean, callback: ReturnCallback<Readable>): any;
abstract mimeType(targetSource: boolean, callback: ReturnCallback<string>): any;
Expand All @@ -42,7 +43,16 @@ export declare abstract class StandardResource implements IResource {
gateway?(arg: MethodCallArgs, path: FSPath, callback: (error: Error, resource?: IResource) => void): any;
protected updateLastModified(): void;
protected removeFromParent(callback: SimpleCallback): void;
protected addToParent(parent: IResource, callback: SimpleCallback): void;
static standardRemoveFromParent(resource: IResource, callback: SimpleCallback): void;
static standardAddToParent(resource: IResource, parent: IResource, callback: SimpleCallback): void;
static standardFindChildren(parent: IResource, predicate: (resource: IResource, callback: (error: Error, isMatching?: boolean) => void) => void, callback: ReturnCallback<IResource[]>): void;
static standardFindChildByName(parent: IResource, name: string, callback: ReturnCallback<IResource>): void;
static standardMoveByCopy(resource: IResource, parent: IResource, newName: string, overwrite: boolean, deleteSource: boolean, callback: ReturnCallback<IResource>): void;
static standardMoveTo(resource: IResource, parent: IResource, newName: string, overwrite: boolean, callback: SimpleCallback): void;
/**
* @deprecated Prefer calling 'standardMoveByCopy(...)' instead to avoid compatibility issue between file systems.
*/
static standardMoveWithoutCopy(resource: IResource, parent: IResource, newName: string, overwrite: boolean, callback: SimpleCallback): void;
static standardMimeType(resource: IResource, targetSource: boolean, callback: ReturnCallback<string>): void;
}
250 changes: 207 additions & 43 deletions lib/resource/std/StandardResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var Errors_1 = require("../../Errors");
var mimeTypes = require("mime-types");
var StandardResource = (function () {
function StandardResource(parent, fsManager) {
this.deleteOnMoved = true;
this.dateCreation = Date.now();
this.properties = {};
this.fsManager = fsManager;
Expand All @@ -30,6 +31,7 @@ var StandardResource = (function () {
.done(function (sizes) { return callback(null, sizes.reduce(function (o, s) { return o + s; }, 0)); });
});
};
// ****************************** Locks ****************************** //
StandardResource.prototype.getAvailableLocks = function (callback) {
callback(null, [
new LockKind_1.LockKind(LockScope_1.LockScope.Exclusive, LockType_1.LockType.Write),
Expand All @@ -52,6 +54,7 @@ var StandardResource = (function () {
StandardResource.prototype.getLock = function (uuid, callback) {
callback(null, this.lockBag.getLock(uuid));
};
// ****************************** Properties ****************************** //
StandardResource.prototype.setProperty = function (name, value, callback) {
this.properties[name] = value;
this.updateLastModified();
Expand All @@ -72,6 +75,10 @@ var StandardResource = (function () {
StandardResource.prototype.getProperties = function (callback) {
callback(null, this.properties);
};
StandardResource.prototype.moveTo = function (parent, newName, overwrite, callback) {
StandardResource.standardMoveByCopy(this, parent, newName, overwrite, this.deleteOnMoved, callback);
};
// ****************************** Std meta-data ****************************** //
StandardResource.prototype.creationDate = function (callback) {
callback(null, this.dateCreation);
};
Expand All @@ -84,72 +91,229 @@ var StandardResource = (function () {
StandardResource.prototype.removeFromParent = function (callback) {
StandardResource.standardRemoveFromParent(this, callback);
};
StandardResource.prototype.addToParent = function (parent, callback) {
StandardResource.standardAddToParent(this, parent, callback);
};
StandardResource.standardRemoveFromParent = function (resource, callback) {
var parent = resource.parent;
if (parent)
parent.removeChild(resource, function (e) {
if (e) {
callback(e);
return;
}
if (resource.parent === parent)
if (!e && resource.parent === parent)
resource.parent = null;
callback(null);
callback(e);
});
else
callback(null);
};
StandardResource.standardMoveTo = function (resource, parent, newName, overwrite, callback) {
StandardResource.standardAddToParent = function (resource, parent, callback) {
parent.addChild(resource, function (e) {
if (!e)
resource.parent = parent;
callback(e);
});
};
StandardResource.standardFindChildren = function (parent, predicate, callback) {
parent.getChildren(function (e, children) {
if (e) {
callback(e, null);
return;
}
new Workflow_1.Workflow()
.each(children, function (child, cb) { return child.webName(function (e, name) {
.each(children, function (child, cb) {
predicate(child, function (e, isMatching) { return cb(e, isMatching ? child : null); });
})
.error(function (e) { return callback(e, null); })
.done(function (conflictingChildren) { return callback(null, conflictingChildren.filter(function (c) { return !!c; })); });
});
};
StandardResource.standardFindChildByName = function (parent, name, callback) {
this.standardFindChildren(parent, function (r, cb) { return r.webName(function (e, rname) {
if (e)
cb(e);
else if (name === rname)
cb(null, true);
else
cb(null, false);
}); }, function (e, rs) {
if (e)
callback(e, null);
else if (rs.length > 0)
callback(null, rs[0]);
else
callback(Errors_1.Errors.ResourceNotFound, null);
});
};
StandardResource.standardMoveByCopy = function (resource, parent, newName, overwrite, deleteSource, callback) {
StandardResource.standardFindChildByName(parent, newName, function (e, r) {
if (e === Errors_1.Errors.ResourceNotFound)
copy();
else if (e)
callback(e, null);
else if (!overwrite)
callback(Errors_1.Errors.ResourceAlreadyExists, null);
else
r.delete(function (e) {
if (e)
callback(e, null);
else
copy();
});
});
function copy() {
resource.type(function (e, type) {
if (e) {
callback(e, null);
return;
}
var destination = parent.fsManager.newResource(null, newName, type, parent);
destination.create(function (e) {
if (e) {
callback(e, null);
return;
}
parent.addChild(destination, function (e) {
if (e) {
callback(e, null);
return;
}
if (type.isDirectory)
copyDir(destination);
else
copyFile(destination);
});
});
});
}
function copyProperties(destination, callback) {
resource.getProperties(function (e, props) {
if (e) {
callback(e);
return;
}
new Workflow_1.Workflow()
.each(Object.keys(props), function (key, cb) { return destination.setProperty(key, props[key], cb); })
.error(callback)
.done(function () { return callback(null); });
});
}
function copyLocks(destination, callback) {
resource.getLocks(function (e, locks) {
if (e === Errors_1.Errors.MustIgnore) {
callback(null);
return;
}
if (e) {
callback(e);
return;
}
new Workflow_1.Workflow()
.each(locks, function (lock, cb) { return destination.setLock(lock, cb); })
.error(callback)
.done(function () { return callback(null); });
});
}
function finalizeCopy(destination) {
copyProperties(destination, function (e) {
if (e)
cb(e);
else if (name === newName && !overwrite)
cb(Errors_1.Errors.ResourceAlreadyExists);
else if (name === newName && overwrite)
cb(null, child);
callback(e, null);
else
cb();
}); })
.error(callback)
.done(function (conflictingChildren) {
conflictingChildren = conflictingChildren.filter(function (c) { return !!c; });
copyLocks(destination, function (e) {
if (e)
callback(e, null);
else if (deleteSource)
resource.delete(function (e) { return callback(e, destination); });
else
resource.parent.removeChild(resource, function (e) { return callback(e, destination); });
});
});
}
function copyDir(destination) {
resource.getChildren(function (e, children) {
if (e) {
callback(e, null);
return;
}
new Workflow_1.Workflow()
.each(conflictingChildren, function (child, cb) { return child.delete(cb); })
.error(callback)
.done(function () {
if (parent === resource.parent) {
resource.rename(newName, function (e, oldName, newName) {
.each(children, function (child, cb) { return child.webName(function (e, name) {
if (e)
cb(e);
else
child.moveTo(destination, name, overwrite, cb);
}); })
.error(function (e) { return callback(e, null); })
.done(function () { return finalizeCopy(destination); });
});
}
function copyFile(destination) {
resource.read(true, function (e, rStream) {
if (e) {
callback(e, null);
return;
}
destination.write(true, function (e, wStream) {
if (e) {
callback(e, null);
return;
}
rStream.pipe(wStream);
wStream.on('error', callback);
wStream.on('finish', function () { return finalizeCopy(destination); });
});
});
}
};
StandardResource.standardMoveTo = function (resource, parent, newName, overwrite, callback) {
StandardResource.standardMoveByCopy(resource, parent, newName, overwrite, true, callback);
};
/**
* @deprecated Prefer calling 'standardMoveByCopy(...)' instead to avoid compatibility issue between file systems.
*/
StandardResource.standardMoveWithoutCopy = function (resource, parent, newName, overwrite, callback) {
StandardResource.standardFindChildByName(parent, newName, function (e, r) {
if (e === Errors_1.Errors.ResourceNotFound)
move();
else if (e)
callback(e);
else if (!overwrite)
callback(Errors_1.Errors.ResourceAlreadyExists);
else
r.delete(function (e) {
if (e)
callback(e);
else
move();
});
});
function move() {
if (parent === resource.parent) {
resource.rename(newName, function (e, oldName, newName) {
callback(e);
});
return;
}
StandardResource.standardRemoveFromParent(resource, function (e) {
if (e) {
callback(e);
return;
}
resource.webName(function (e, name) {
if (e || name === newName) {
parent.addChild(resource, function (e) {
callback(e);
});
return;
}
StandardResource.standardRemoveFromParent(resource, function (e) {
if (e) {
resource.rename(newName, function (e, oldName, newName) {
if (e)
callback(e);
return;
}
resource.webName(function (e, name) {
if (e || name === newName) {
parent.addChild(resource, function (e) {
callback(e);
});
return;
}
resource.rename(newName, function (e, oldName, newName) {
if (e)
callback(e);
else
parent.addChild(resource, function (e) {
callback(e);
});
else
parent.addChild(resource, function (e) {
callback(e);
});
});
});
});
});
});
}
};
StandardResource.standardMimeType = function (resource, targetSource, callback) {
resource.type(function (e, type) {
Expand Down

0 comments on commit 2bb7a4b

Please sign in to comment.