Skip to content

Commit

Permalink
Implemented an 'autoLoad' method in the server to help loading files …
Browse files Browse the repository at this point in the history
…saved by the 'autoSave'
  • Loading branch information
AdrienCastex committed Jun 16, 2017
1 parent f0365f2 commit 5acdf13
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 6 deletions.
9 changes: 8 additions & 1 deletion lib/server/WebDAVServerOptions.d.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
/// <reference types="node" />
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;
tempTreeFilePath: string;
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;
Expand All @@ -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;
1 change: 1 addition & 0 deletions lib/server/WebDAVServerOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var WebDAVServerOptions = (function () {
this.serverName = 'webdav-server';
this.version = '1.8.0';
this.autoSave = null;
this.autoLoad = null;
}
return WebDAVServerOptions;
}());
Expand Down
2 changes: 2 additions & 0 deletions lib/server/webDAVServer/Persistence.d.ts
Original file line number Diff line number Diff line change
@@ -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;
37 changes: 36 additions & 1 deletion lib/server/webDAVServer/Persistence.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/server/webDAVServer/WebDAVServer.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/// <reference types="node" />
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';
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions lib/server/webDAVServer/WebDAVServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 10 additions & 1 deletion src/server/WebDAVServerOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;

Expand Down
47 changes: 46 additions & 1 deletion src/server/webDAVServer/Persistence.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/server/webDAVServer/WebDAVServer.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -82,6 +83,7 @@ export class WebDAVServer
stop = startStop.stop

// Persistence
autoLoad = persistence.autoLoad
load = persistence.load
save = persistence.save

Expand Down

0 comments on commit 5acdf13

Please sign in to comment.