-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the storage manager to allow to limit the storage
- Loading branch information
1 parent
6eb0cd0
commit 74094e3
Showing
13 changed files
with
439 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { RequestContext } from '../../../server/v2/RequestContext'; | ||
import { Path } from '../Path'; | ||
import { ResourceType, ResourcePropertyValue, PropertyAttributes } from './CommonTypes'; | ||
import { FileSystem } from './FileSystem'; | ||
export declare type IStorageManagerEvaluateCallback = (size: number) => void; | ||
export interface IStorageManager { | ||
reserve(ctx: RequestContext, fs: FileSystem, size: number, callback: (reserved: boolean) => void): void; | ||
evaluateCreate(ctx: RequestContext, fs: FileSystem, path: Path, type: ResourceType, callback: IStorageManagerEvaluateCallback): void; | ||
evaluateContent(ctx: RequestContext, fs: FileSystem, expectedSize: number, callback: IStorageManagerEvaluateCallback): void; | ||
evaluateProperty(ctx: RequestContext, fs: FileSystem, name: string, value: ResourcePropertyValue, attributes: PropertyAttributes, callback: IStorageManagerEvaluateCallback): void; | ||
available(ctx: RequestContext, fs: FileSystem, callback: (available: number) => void): void; | ||
reserved(ctx: RequestContext, fs: FileSystem, callback: (reserved: number) => void): void; | ||
} | ||
export declare class NoStorageManager implements IStorageManager { | ||
reserve(ctx: RequestContext, fs: FileSystem, size: number, callback: (reserved: boolean) => void): void; | ||
evaluateCreate(ctx: RequestContext, fs: FileSystem, path: Path, type: ResourceType, callback: IStorageManagerEvaluateCallback): void; | ||
evaluateContent(ctx: RequestContext, fs: FileSystem, expectedSize: number, callback: IStorageManagerEvaluateCallback): void; | ||
evaluateProperty(ctx: RequestContext, fs: FileSystem, name: string, value: ResourcePropertyValue, attributes: PropertyAttributes, callback: IStorageManagerEvaluateCallback): void; | ||
available(ctx: RequestContext, fs: FileSystem, callback: (available: number) => void): void; | ||
reserved(ctx: RequestContext, fs: FileSystem, callback: (reserved: number) => void): void; | ||
} | ||
export declare class PerUserStorageManager implements IStorageManager { | ||
limitPerUser: number; | ||
storage: { | ||
[UUID: string]: number; | ||
}; | ||
constructor(limitPerUser: number); | ||
reserve(ctx: RequestContext, fs: FileSystem, size: number, callback: (reserved: boolean) => void): void; | ||
evaluateCreate(ctx: RequestContext, fs: FileSystem, path: Path, type: ResourceType, callback: IStorageManagerEvaluateCallback): void; | ||
evaluateContent(ctx: RequestContext, fs: FileSystem, expectedSize: number, callback: IStorageManagerEvaluateCallback): void; | ||
evalPropValue(value: ResourcePropertyValue): number; | ||
evaluateProperty(ctx: RequestContext, fs: FileSystem, name: string, value: ResourcePropertyValue, attributes: PropertyAttributes, callback: IStorageManagerEvaluateCallback): void; | ||
available(ctx: RequestContext, fs: FileSystem, callback: (available: number) => void): void; | ||
reserved(ctx: RequestContext, fs: FileSystem, callback: (reserved: number) => void): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var NoStorageManager = (function () { | ||
function NoStorageManager() { | ||
} | ||
NoStorageManager.prototype.reserve = function (ctx, fs, size, callback) { | ||
callback(true); | ||
}; | ||
NoStorageManager.prototype.evaluateCreate = function (ctx, fs, path, type, callback) { | ||
callback(0); | ||
}; | ||
NoStorageManager.prototype.evaluateContent = function (ctx, fs, expectedSize, callback) { | ||
callback(0); | ||
}; | ||
NoStorageManager.prototype.evaluateProperty = function (ctx, fs, name, value, attributes, callback) { | ||
callback(0); | ||
}; | ||
NoStorageManager.prototype.available = function (ctx, fs, callback) { | ||
callback(-1); | ||
}; | ||
NoStorageManager.prototype.reserved = function (ctx, fs, callback) { | ||
callback(0); | ||
}; | ||
return NoStorageManager; | ||
}()); | ||
exports.NoStorageManager = NoStorageManager; | ||
var PerUserStorageManager = (function () { | ||
function PerUserStorageManager(limitPerUser) { | ||
this.limitPerUser = limitPerUser; | ||
this.storage = {}; | ||
} | ||
PerUserStorageManager.prototype.reserve = function (ctx, fs, size, callback) { | ||
var nb = this.storage[ctx.user.uid]; | ||
if (nb === undefined) | ||
nb = 0; | ||
nb += size; | ||
if (nb > this.limitPerUser) | ||
return callback(false); | ||
this.storage[ctx.user.uid] = Math.max(0, nb); | ||
callback(true); | ||
}; | ||
PerUserStorageManager.prototype.evaluateCreate = function (ctx, fs, path, type, callback) { | ||
fs.getFullPath(ctx, path, function (e, fullPath) { | ||
callback(fullPath.toString().length); | ||
}); | ||
}; | ||
PerUserStorageManager.prototype.evaluateContent = function (ctx, fs, expectedSize, callback) { | ||
callback(expectedSize); | ||
}; | ||
PerUserStorageManager.prototype.evalPropValue = function (value) { | ||
var _this = this; | ||
if (!value) | ||
return 0; | ||
if (value.constructor === String) | ||
return value.length; | ||
if (Array.isArray(value)) | ||
return value.map(function (el) { return _this.evalPropValue(el); }).reduce(function (p, n) { return p + n; }, 0); | ||
var xml = value; | ||
var attributesLength = Object.keys(xml.attributes).map(function (at) { return at.length + xml.attributes[at].length; }).reduce(function (p, n) { return p + n; }, 0); | ||
return xml.name.length + attributesLength + (xml.elements && xml.elements.length > 0 ? this.evalPropValue(xml.elements) : 0); | ||
}; | ||
PerUserStorageManager.prototype.evaluateProperty = function (ctx, fs, name, value, attributes, callback) { | ||
callback(name.length + Object.keys(attributes).map(function (ak) { return attributes[ak].length + ak.length; }).reduce(function (p, n) { return p + n; }, 0) + this.evalPropValue(value)); | ||
}; | ||
PerUserStorageManager.prototype.available = function (ctx, fs, callback) { | ||
var nb = this.storage[ctx.user.uid]; | ||
callback(nb === undefined ? this.limitPerUser : this.limitPerUser - nb); | ||
}; | ||
PerUserStorageManager.prototype.reserved = function (ctx, fs, callback) { | ||
var nb = this.storage[ctx.user.uid]; | ||
callback(nb === undefined ? 0 : nb); | ||
}; | ||
return PerUserStorageManager; | ||
}()); | ||
exports.PerUserStorageManager = PerUserStorageManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.