Skip to content

Commit

Permalink
Added an example folder to provide real/full examples + Added the exa…
Browse files Browse the repository at this point in the history
…mple : Custom Web Resource
  • Loading branch information
AdrienCastex committed Jun 16, 2017
1 parent cb90c9f commit 14cdf33
Show file tree
Hide file tree
Showing 9 changed files with 367 additions and 0 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ test
.*
tslint.json
*.log
examples
21 changes: 21 additions & 0 deletions examples/customWebResource/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Custom Web Resource

This is an example to show how to make a simple custom resource base on the web. It gets a web content as its own content.

The folder `ts` and the folder `js` are the same thing. The `js` folder display the example in JavaScript while the `ts` display the example in TypeScript.

## Usage

### Execute

```bash
node index.js
```

### Tests

```bash
npm tst
```


18 changes: 18 additions & 0 deletions examples/customWebResource/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const webFile = require('./js/resource.js'),
webdav = require('webdav-server');

const server = new webdav.WebDAVServer({
port: 1900
});

server.addResourceTree([
new webFile.WebFile('http://unlicense.org/UNLICENSE', 'license.txt'),
new webFile.WebFile('https://github.com/OpenMarshal/npm-WebDAV-Server', 'webdav-server-github.html'),
new webFile.WebFile('http://www.stuffedcupcakes.com/wp-content/uploads/2013/05/Chocolate-Overload.jpg', 'chocolate.jpg')
], (e) => {
if(e) throw e;

server.start((s) => {
console.log('Server started on port ' + s.address().port + '.');
});
});
34 changes: 34 additions & 0 deletions examples/customWebResource/js/fsManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const webdav = require('webdav-server'),
webFile = require('./resource.js');

module.exports.WebFSManager = function()
{
const fsManager = { };
fsManager.uid = 'WebFSManager_1.0.0';

fsManager.serialize = function(resource, obj)
{
return {
dateCreation: resource.dateCreation,
dateLastModified: resource.dateLastModified,
properties: resource.properties,
webUrl: resource.webUrl,
fileName: resource.fileName,
refreshTimeoutMS: resource.refreshTimeoutMS
};
}

fsManager.unserialize = function(data, obj)
{
const rs = new webFile.WebFile(data.webUrl, data.fileName, data.refreshTimeoutMS);
rs.dateCreation = data.dateCreation;
rs.dateLastModified = data.dateLastModified;
rs.properties = data.properties;
return rs;
}

fsManager.newResource = function(fullPath, name, type, parent)
{
throw webdav.Errors.InvalidOperation;
}
}
105 changes: 105 additions & 0 deletions examples/customWebResource/js/resource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
"use strict";
const webdav = require('webdav-server'),
request = require('request'),
webFSManager = require('./fsManager.js');

module.exports.WebFile = function(webUrl, fileName, refreshTimeoutMs = 10000)
{
const stdRes = new webdav.StandardResource(null, new webFSManager.WebFSManager());

stdRes.refreshTimeoutMs = refreshTimeoutMs;
stdRes.lenUpdateTime = 0;
stdRes.fileName = fileName;
stdRes.webUrl = webUrl;
stdRes.len = -1;

stdRes.openReadStream = function()
{
let size = 0;
const stream = request.get(this.webUrl);
stream.on('data', (chunk) => {
size = chunk.length;
})
stream.on('end', () => {
this.len = size;
this.lenUpdateTime = Date.now();
})
stream.end();
return stream;
};

stdRes.create = function(callback)
{
callback();
};

stdRes.delete = function(callback)
{
webdav.StandardResource.standardRemoveFromParent(this, callback);
}

stdRes.moveTo = function(parent, newName, overwrite, callback)
{
webdav.StandardResource.standardMoveTo(this, parent, newName, overwrite, callback);
}

stdRes.rename = function(newName, callback)
{
const oldname = this.fileName;
this.fileName = newName;
callback(null, oldname, newName);
}

stdRes.write = function(targetSource, callback)
{
callback(webdav.Errors.InvalidOperation);
}

stdRes.read = function(targetSource, callback)
{
callback(null, this.openReadStream());
}

stdRes.mimeType = function(targetSource, callback)
{
webdav.StandardResource.standardMimeType(this, targetSource, callback);
}

stdRes.size = function(targetSource, callback)
{
if(this.len >= 0 && Date.now() - this.lenUpdateTime < this.refreshTimeoutMs)
{
callback(null, this.len);
return;
}

const stream = this.openReadStream();
stream.on('end', () => callback(null, this.len));
}

stdRes.webName = function(callback)
{
callback(null, this.fileName);
}

stdRes.type = function(callback)
{
callback(null, webdav.ResourceType.File);
}

stdRes.addChild = function(resource, callback)
{
callback(webdav.Errors.InvalidOperation);
}
stdRes.removeChild = function(resource, callback)
{
callback(webdav.Errors.InvalidOperation);
}
stdRes.getChildren = function(callback)
{
callback(webdav.Errors.InvalidOperation);
}


return stdRes;
}
14 changes: 14 additions & 0 deletions examples/customWebResource/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "customwebresource",
"version": "1.0.0",
"description": "Example to show how to make a simple custom resource base on the web. It gets a web content as its own content.",
"main": "index.js",
"scripts": {
"test": "node test.js"
},
"author": "Adrien Castex <[email protected]>",
"license": "Unlicense",
"dependencies": {
"webdav-server": "^1.8.0"
}
}
32 changes: 32 additions & 0 deletions examples/customWebResource/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const webFile = require('./js/resource.js'),
webdav = require('webdav-server');

new webdav.ResourceTester({
canHaveVirtualFolderChildren: false,
canHaveVirtualFileChildren: false,
canGetLastModifiedDate: true,
canGetCreationDate: true,
canRemoveChildren: false,
canHaveChildren: false,
canGetChildren: false,
canGetMimeType: true,
canBeCreated: true,
canBeDeleted: true,
canBeRenamed: true,
canGetSize: true,
canBeMoved: true,
canWrite: false,
canRead: true,
canLock: true
},
// For each battery of tests, create the resource to test
// willCreate : A value of true means you must not call the '.create(...)' method because it will be tested
(willCreate, cb) => cb(new webFile.WebFile('http://unlicense.org/UNLICENSE', 'test.txt'))
).run((results) => {

// Display the results of the tests
console.log(results.all.isValid ? 'Tests passed!' : 'Tests failed!');
if(results.all.errors)
for(const value of results.all.errors)
console.log(value.toString());
});
33 changes: 33 additions & 0 deletions examples/customWebResource/ts/FSManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { SerializedObject, FSManager, Errors, IResource, ResourceType } from 'webdav-server'
import { WebFile } from './Resource'

export class WebFSManager implements FSManager
{
uid : string = 'WebFSManager_1.0.0';

serialize(resource : any, obj : SerializedObject) : object
{
return {
dateCreation: resource.dateCreation,
dateLastModified: resource.dateLastModified,
properties: resource.properties,
webUrl: resource.webUrl,
fileName: resource.fileName,
refreshTimeoutMS: resource.refreshTimeoutMS
};
}

unserialize(data : any, obj : SerializedObject) : IResource
{
const rs = new WebFile(data.webUrl, data.fileName, data.refreshTimeoutMS);
rs.dateCreation = data.dateCreation;
rs.dateLastModified = data.dateLastModified;
rs.properties = data.properties;
return rs;
}

newResource(fullPath : string, name : string, type : ResourceType, parent : IResource) : IResource
{
throw Errors.InvalidOperation;
}
}
109 changes: 109 additions & 0 deletions examples/customWebResource/ts/Resource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import * as webdav from 'webdav-server'
import * as request from 'request'

export class WebFile extends webdav.StandardResource
{
fileName : string
webUrl : string
len : number
lenUpdateTime : number
refreshTimeoutMs : number

constructor(webUrl : string, fileName : string, refreshTimeoutMs : number = 10000)
{
super(null, null);

this.refreshTimeoutMs = refreshTimeoutMs;
this.lenUpdateTime = 0;
this.fileName = fileName;
this.webUrl = webUrl;
this.len = -1;
}

openReadStream()
{
let size = 0;
const stream = request.get(this.webUrl);
stream.on('data', (chunk) => {
size = chunk.length;
})
stream.on('end', () => {
this.len = size;
this.lenUpdateTime = Date.now();
})
stream.end();
return stream;
}

create(callback)
{
callback();
}

delete(callback)
{
webdav.StandardResource.standardRemoveFromParent(this, callback);
}

moveTo(parent, newName, overwrite, callback)
{
webdav.StandardResource.standardMoveTo(this, parent, newName, overwrite, callback);
}

rename(newName, callback)
{
const oldname = this.fileName;
this.fileName = newName;
callback(null, oldname, newName);
}

write(targetSource, callback)
{
callback(webdav.Errors.InvalidOperation);
}

read(targetSource, callback)
{
callback(null, this.openReadStream());
}

mimeType(targetSource, callback)
{
webdav.StandardResource.standardMimeType(this, targetSource, callback);
}

size(targetSource, callback)
{
if(this.len >= 0 && Date.now() - this.lenUpdateTime < this.refreshTimeoutMs)
{
callback(null, this.len);
return;
}

const stream = this.openReadStream();
stream.on('end', () => callback(null, this.len));
}

webName(callback)
{
callback(null, this.fileName);
}

type(callback)
{
callback(null, webdav.ResourceType.File);
}

addChild(resource, callback)
{
callback(webdav.Errors.InvalidOperation);
}
removeChild(resource, callback)
{
callback(webdav.Errors.InvalidOperation);
}
getChildren(callback)
{
callback(webdav.Errors.InvalidOperation);
}
}

0 comments on commit 14cdf33

Please sign in to comment.