-
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.
Splitted the 'examples' folder into 'v1' and 'v2'
- Loading branch information
1 parent
7a23779
commit 7518929
Showing
35 changed files
with
2,097 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,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 | ||
``` | ||
|
||
|
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,45 @@ | ||
const webFsManager = require('./js/fsManager.js'), | ||
webFile = require('./js/resource.js'), | ||
webdav = require('webdav-server'), | ||
zlib = require('zlib'), | ||
fs = require('fs'); | ||
|
||
const server = new webdav.WebDAVServer({ | ||
port: 1900, | ||
autoSave: { | ||
treeFilePath: './data.json', | ||
tempTreeFilePath: './data.tmp.json' | ||
}, | ||
autoLoad: { | ||
treeFilePath: './data.json', | ||
fsManagers: [ | ||
new webdav.RootFSManager(), | ||
new webFsManager.WebFSManager(), | ||
new webdav.VirtualFSManager() | ||
] | ||
} | ||
}); | ||
|
||
server.autoLoad((e) => { | ||
if(e) | ||
{ | ||
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; | ||
|
||
run(); | ||
}); | ||
} | ||
else | ||
run(); | ||
}) | ||
|
||
function run() | ||
{ | ||
server.start((s) => { | ||
console.log('Server started 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,36 @@ | ||
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; | ||
} | ||
|
||
return fsManager; | ||
} |
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,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; | ||
} |
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,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.2" | ||
} | ||
} |
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,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()); | ||
}); |
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,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; | ||
} | ||
} |
Oops, something went wrong.