diff --git a/lib/server/WebDAVServerOptions.d.ts b/lib/server/WebDAVServerOptions.d.ts index 469dee65..4c807f2c 100644 --- a/lib/server/WebDAVServerOptions.d.ts +++ b/lib/server/WebDAVServerOptions.d.ts @@ -1,9 +1,10 @@ /// import { HTTPAuthentication } from '../user/authentication/HTTPAuthentication'; +import { Writable, Readable } from 'stream'; import { IPrivilegeManager } from '../user/privilege/IPrivilegeManager'; import { IUserManager } from '../user/IUserManager'; import { IResource } from '../resource/IResource'; -import { Writable } from 'stream'; +import { FSManager } from '../manager/FSManager'; import * as https from 'https'; export interface IAutoSave { treeFilePath: string; @@ -11,6 +12,11 @@ export interface IAutoSave { onSaveError?: (error: Error) => void; streamProvider?: (inputStream: Writable, callback: (outputStream?: Writable) => void) => void; } +export interface IAutoLoad { + treeFilePath: string; + fsManagers: FSManager[]; + streamProvider?: (inputStream: Readable, callback: (outputStream?: Readable) => void) => void; +} export declare class WebDAVServerOptions { requireAuthentification?: boolean; httpAuthentication?: HTTPAuthentication; @@ -26,6 +32,7 @@ export declare class WebDAVServerOptions { serverName?: string; version?: string; autoSave?: IAutoSave; + autoLoad?: IAutoLoad; } export default WebDAVServerOptions; export declare function setDefaultServerOptions(options: WebDAVServerOptions): WebDAVServerOptions; diff --git a/lib/server/WebDAVServerOptions.js b/lib/server/WebDAVServerOptions.js index 485d4545..b1fa13dd 100644 --- a/lib/server/WebDAVServerOptions.js +++ b/lib/server/WebDAVServerOptions.js @@ -20,6 +20,7 @@ var WebDAVServerOptions = (function () { this.serverName = 'webdav-server'; this.version = '1.8.0'; this.autoSave = null; + this.autoLoad = null; } return WebDAVServerOptions; }()); diff --git a/lib/server/webDAVServer/Persistence.d.ts b/lib/server/webDAVServer/Persistence.d.ts index b99a8c19..2eeb1e4b 100644 --- a/lib/server/webDAVServer/Persistence.d.ts +++ b/lib/server/webDAVServer/Persistence.d.ts @@ -1,4 +1,6 @@ import { SerializedObject } from '../../manager/ISerializer'; +import { SimpleCallback } from '../../resource/IResource'; import { FSManager } from '../../manager/FSManager'; export declare function load(obj: SerializedObject, managers: FSManager[], callback: (error: Error) => void): void; +export declare function autoLoad(callback: SimpleCallback): void; export declare function save(callback: (error: Error, obj: any) => void): void; diff --git a/lib/server/webDAVServer/Persistence.js b/lib/server/webDAVServer/Persistence.js index b57ede63..2da93183 100644 --- a/lib/server/webDAVServer/Persistence.js +++ b/lib/server/webDAVServer/Persistence.js @@ -1,9 +1,19 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); +var export_1 = require("../../manager/export"); var ISerializer_1 = require("../../manager/ISerializer"); +var zlib = require("zlib"); +var fs = require("fs"); +function defaultFSManagers() { + return [ + new export_1.RootFSManager(), + new export_1.VirtualFSManager(), + new export_1.PhysicalFSManager() + ]; +} function load(obj, managers, callback) { var _this = this; - ISerializer_1.unserialize(obj, managers, function (e, r) { + ISerializer_1.unserialize(obj, managers ? managers : defaultFSManagers(), function (e, r) { if (!e) { _this.rootResource = r; callback(null); @@ -13,6 +23,31 @@ function load(obj, managers, callback) { }); } exports.load = load; +function autoLoad(callback) { + var _this = this; + var oStream = fs.createReadStream(this.options.autoLoad.treeFilePath); + var stream = oStream.pipe(zlib.createGunzip()); + oStream.on('error', callback); + stream.on('error', callback); + var streamProvider = this.options.autoLoad.streamProvider; + if (!streamProvider) + streamProvider = function (s, cb) { return cb(s); }; + streamProvider(stream, function (s) { + if (!s) + s = stream; + var data = ''; + s.on('data', function (chunk) { + data += chunk.toString(); + }); + s.on('error', callback); + s.on('end', function () { + var obj = JSON.parse(data.toString()); + var fsManagers = _this.options.autoLoad.fsManagers; + _this.load(obj, fsManagers ? fsManagers : defaultFSManagers(), callback); + }); + }); +} +exports.autoLoad = autoLoad; function save(callback) { ISerializer_1.serialize(this.rootResource, callback); } diff --git a/lib/server/webDAVServer/WebDAVServer.d.ts b/lib/server/webDAVServer/WebDAVServer.d.ts index 1a8a2c33..09ef64b9 100644 --- a/lib/server/webDAVServer/WebDAVServer.d.ts +++ b/lib/server/webDAVServer/WebDAVServer.d.ts @@ -1,8 +1,8 @@ /// import { WebDAVServerOptions } from '../WebDAVServerOptions'; import { ResourceTreeNode, WebDAVServerStartCallback } from './Types'; -import { MethodCallArgs, WebDAVRequest } from '../WebDAVRequest'; import { IResource, ReturnCallback } from '../../resource/IResource'; +import { MethodCallArgs, WebDAVRequest } from '../WebDAVRequest'; import { HTTPAuthentication } from '../../user/authentication/HTTPAuthentication'; import { IPrivilegeManager } from '../../user/privilege/IPrivilegeManager'; import { FSPath } from '../../manager/FSManager'; @@ -37,6 +37,7 @@ export declare class WebDAVServer { start(callback: WebDAVServerStartCallback): any; start(port: number, callback: WebDAVServerStartCallback): any; stop: typeof startStop.stop; + autoLoad: typeof persistence.autoLoad; load: typeof persistence.load; save: typeof persistence.save; method(name: string, manager: WebDAVRequest): void; diff --git a/lib/server/webDAVServer/WebDAVServer.js b/lib/server/webDAVServer/WebDAVServer.js index 199de540..2afbfa25 100644 --- a/lib/server/webDAVServer/WebDAVServer.js +++ b/lib/server/webDAVServer/WebDAVServer.js @@ -12,6 +12,7 @@ exports.WebDAVServerOptions = WebDAVServerOptions_2.WebDAVServerOptions; var WebDAVServer = (function () { function WebDAVServer(options) { this.stop = startStop.stop; + this.autoLoad = persistence.autoLoad; this.load = persistence.load; this.save = persistence.save; this.beforeRequest = beforeAfter.beforeRequest; diff --git a/src/server/WebDAVServerOptions.ts b/src/server/WebDAVServerOptions.ts index 2371b1da..9e18af9c 100644 --- a/src/server/WebDAVServerOptions.ts +++ b/src/server/WebDAVServerOptions.ts @@ -2,12 +2,13 @@ import { HTTPBasicAuthentication } from '../user/authentication/HTTPBasicAuthent import { HTTPDigestAuthentication } from '../user/authentication/HTTPDigestAuthentication' import { FakePrivilegeManager } from '../user/privilege/FakePrivilegeManager' import { HTTPAuthentication } from '../user/authentication/HTTPAuthentication' +import { Writable, Readable } from 'stream' import { IPrivilegeManager } from '../user/privilege/IPrivilegeManager' import { SimpleUserManager } from '../user/simple/SimpleUserManager' import { RootResource } from '../resource/std/RootResource' import { IUserManager } from '../user/IUserManager' import { IResource } from '../resource/IResource' -import { Writable } from 'stream' +import { FSManager } from '../manager/FSManager' import * as https from 'https' export interface IAutoSave @@ -18,6 +19,13 @@ export interface IAutoSave streamProvider ?: (inputStream : Writable, callback : (outputStream ?: Writable) => void) => void } +export interface IAutoLoad +{ + treeFilePath : string, + fsManagers : FSManager[], + streamProvider ?: (inputStream : Readable, callback : (outputStream ?: Readable) => void) => void +} + export class WebDAVServerOptions { requireAuthentification ?: boolean = false @@ -34,6 +42,7 @@ export class WebDAVServerOptions serverName ?: string = 'webdav-server' version ?: string = '1.8.0' autoSave ?: IAutoSave = null + autoLoad ?: IAutoLoad = null } export default WebDAVServerOptions; diff --git a/src/server/webDAVServer/Persistence.ts b/src/server/webDAVServer/Persistence.ts index f9ce50e8..41277866 100644 --- a/src/server/webDAVServer/Persistence.ts +++ b/src/server/webDAVServer/Persistence.ts @@ -1,9 +1,23 @@ +import { RootFSManager, VirtualFSManager, PhysicalFSManager } from '../../manager/export' import { SerializedObject, unserialize, serialize } from '../../manager/ISerializer' +import { SimpleCallback } from '../../resource/IResource' import { FSManager } from '../../manager/FSManager' +import { Readable } from 'stream' +import * as zlib from 'zlib' +import * as fs from 'fs' + +function defaultFSManagers() +{ + return [ + new RootFSManager(), + new VirtualFSManager(), + new PhysicalFSManager() + ]; +} export function load(obj : SerializedObject, managers : FSManager[], callback: (error : Error) => void) { - unserialize(obj, managers, (e, r) => { + unserialize(obj, managers ? managers : defaultFSManagers(), (e, r) => { if(!e) { this.rootResource = r; @@ -14,6 +28,37 @@ export function load(obj : SerializedObject, managers : FSManager[], callback: ( }) } +export function autoLoad(callback : SimpleCallback) +{ + const oStream = fs.createReadStream(this.options.autoLoad.treeFilePath); + const stream = oStream.pipe(zlib.createGunzip()); + + oStream.on('error', callback) + stream.on('error', callback) + + let streamProvider = this.options.autoLoad.streamProvider; + + if(!streamProvider) + streamProvider = (s, cb) => cb(s); + + streamProvider(stream, (s : Readable) => { + if(!s) + s = stream; + + let data = ''; + s.on('data', (chunk) => { + data += chunk.toString(); + }) + s.on('error', callback) + s.on('end', () => { + const obj = JSON.parse(data.toString()); + + const fsManagers = this.options.autoLoad.fsManagers; + this.load(obj, fsManagers ? fsManagers : defaultFSManagers(), callback); + }) + }) +} + export function save(callback : (error : Error, obj : any) => void) { serialize(this.rootResource, callback); diff --git a/src/server/webDAVServer/WebDAVServer.ts b/src/server/webDAVServer/WebDAVServer.ts index 194bf391..ec779a77 100644 --- a/src/server/webDAVServer/WebDAVServer.ts +++ b/src/server/webDAVServer/WebDAVServer.ts @@ -1,11 +1,12 @@ import { WebDAVServerOptions, setDefaultServerOptions } from '../WebDAVServerOptions' import { ResourceTreeNode, WebDAVServerStartCallback } from './Types' +import { IResource, ReturnCallback, SimpleCallback } from '../../resource/IResource' import { HTTPCodes, MethodCallArgs, WebDAVRequest } from '../WebDAVRequest' -import { IResource, ReturnCallback } from '../../resource/IResource' import { HTTPAuthentication } from '../../user/authentication/HTTPAuthentication' import { IPrivilegeManager } from '../../user/privilege/IPrivilegeManager' import { FSManager, FSPath } from '../../manager/FSManager' import { IUserManager } from '../../user/IUserManager' +import { Readable } from 'stream' import Commands from '../commands/Commands' import * as persistence from './Persistence' @@ -82,6 +83,7 @@ export class WebDAVServer stop = startStop.stop // Persistence + autoLoad = persistence.autoLoad load = persistence.load save = persistence.save