-
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 'webResource' example for the v2
- Loading branch information
1 parent
7518929
commit d406d81
Showing
4 changed files
with
165 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# 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 `ts` file and the `js` file are the same thing. The `js` file displays the example in JavaScript while the `ts` file displays the example in TypeScript. |
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,73 @@ | ||
const webdav = require('webdav-server').v2, | ||
request = require('request'); | ||
|
||
// Serializer | ||
function WebFileSystemSerializer() | ||
{ | ||
return { | ||
uid() | ||
{ | ||
return "WebFileSystemSerializer_1.0.0"; | ||
}, | ||
serialize(fs, callback) | ||
{ | ||
callback(null, { | ||
url: fs.url, | ||
props: fs.props | ||
}); | ||
}, | ||
unserialize(serializedData, callback) | ||
{ | ||
const fs = new WebFileSystem(serializedData.url); | ||
fs.props = serializedData.props; | ||
callback(null, fs); | ||
}, | ||
constructor: WebFileSystemSerializer | ||
} | ||
} | ||
|
||
// File system | ||
function WebFileSystem(url) | ||
{ | ||
const r = new webdav.FileSystem(new WebFileSystemSerializer()); | ||
r.constructor = WebFileSystem; | ||
r.props = new webdav.LocalPropertyManager(); | ||
r.locks = new webdav.LocalLockManager(); | ||
r.url = url; | ||
|
||
r._fastExistCheck = function(ctx, path, callback) | ||
{ | ||
callback(path.isRoot()); | ||
} | ||
|
||
r._openReadStream = function(path, info, callback) | ||
{ | ||
const stream = request.get(this.url); | ||
stream.end(); | ||
callback(null, stream); | ||
} | ||
|
||
r._propertyManager = function(path, info, callback) | ||
{ | ||
callback(null, this.props); | ||
} | ||
|
||
r._lockManager = function(path, info, callback) | ||
{ | ||
callback(null, this.locks); | ||
} | ||
|
||
r._type = function(path, info, callback) | ||
{ | ||
callback(null, webdav.ResourceType.File); | ||
} | ||
|
||
return r; | ||
} | ||
|
||
// Server instantiation | ||
const server = new webdav.WebDAVServer(); | ||
server.setFileSystemSync('/chocolate.jpg', new WebFileSystem('http://www.stuffedcupcakes.com/wp-content/uploads/2013/05/Chocolate-Overload.jpg')); | ||
server.setFileSystemSync('/webdav-server-github.html', new WebFileSystem('https://github.com/OpenMarshal/npm-WebDAV-Server')); | ||
server.setFileSystemSync('/license.txt', new WebFileSystem('http://unlicense.org/UNLICENSE')); | ||
server.start((s) => console.log('Ready on port', s.address().port)); |
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,76 @@ | ||
import { v2 } from 'webdav-server' | ||
import * as request from 'request' | ||
import { Readable } from 'stream' | ||
|
||
// Serializer | ||
class WebFileSystemSerializer implements v2.FileSystemSerializer | ||
{ | ||
uid() : string | ||
{ | ||
return "WebFileSystemSerializer_1.0.0"; | ||
} | ||
|
||
serialize(fs : WebFileSystem, callback : v2.ReturnCallback<any>) : void | ||
{ | ||
callback(null, { | ||
url: fs.url, | ||
props: fs.props | ||
}); | ||
} | ||
|
||
unserialize(serializedData : any, callback : v2.ReturnCallback<WebFileSystem>) : void | ||
{ | ||
const fs = new WebFileSystem(serializedData.url); | ||
fs.props = serializedData.props; | ||
callback(null, fs); | ||
} | ||
} | ||
|
||
// File system | ||
class WebFileSystem extends v2.FileSystem | ||
{ | ||
props : v2.IPropertyManager; | ||
locks : v2.ILockManager; | ||
|
||
constructor(public url : string) | ||
{ | ||
super(new WebFileSystemSerializer()); | ||
|
||
this.props = new v2.LocalPropertyManager(); | ||
this.locks = new v2.LocalLockManager(); | ||
} | ||
|
||
_fastExistCheck(ctx : v2.RequestContext, path : v2.Path, callback : (exists : boolean) => void) : void | ||
{ | ||
callback(path.isRoot()); | ||
} | ||
|
||
_openReadStream(path : v2.Path, info : v2.OpenReadStreamInfo, callback : v2.ReturnCallback<Readable>) : void | ||
{ | ||
const stream = request.get(this.url); | ||
stream.end(); | ||
callback(null, (stream as any) as Readable); | ||
} | ||
|
||
_propertyManager(path : v2.Path, info : v2.PropertyManagerInfo, callback : v2.ReturnCallback<v2.IPropertyManager>) : void | ||
{ | ||
callback(null, this.props); | ||
} | ||
|
||
_lockManager(path : v2.Path, info : v2.LockManagerInfo, callback : v2.ReturnCallback<v2.ILockManager>) : void | ||
{ | ||
callback(null, this.locks); | ||
} | ||
|
||
_type(path : v2.Path, info : v2.TypeInfo, callback : v2.ReturnCallback<v2.ResourceType>) : void | ||
{ | ||
callback(null, v2.ResourceType.File); | ||
} | ||
} | ||
|
||
// Server instantiation | ||
const server = new v2.WebDAVServer(); | ||
server.setFileSystemSync('/chocolate.jpg', new WebFileSystem('http://www.stuffedcupcakes.com/wp-content/uploads/2013/05/Chocolate-Overload.jpg')); | ||
server.setFileSystemSync('/webdav-server-github.html', new WebFileSystem('https://github.com/OpenMarshal/npm-WebDAV-Server')); | ||
server.setFileSystemSync('/license.txt', new WebFileSystem('http://unlicense.org/UNLICENSE')); | ||
server.start((s) => console.log('Ready on port', s.address().port)); |
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,11 @@ | ||
{ | ||
"name": "web-resource", | ||
"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", | ||
"author": "Adrien Castex <[email protected]>", | ||
"license": "Unlicense", | ||
"dependencies": { | ||
"webdav-server": "^2.0.0" | ||
} | ||
} |