-
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 an example of a physical gateway
- Loading branch information
1 parent
d17ac14
commit 9123bf6
Showing
11 changed files
with
502 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,13 @@ | ||
# Custom Physical gateway | ||
|
||
This is an example to show how to use a gateway to manage the content of a physical folder. | ||
|
||
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 | ||
``` |
Binary file not shown.
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,39 @@ | ||
const webdav = require('../../lib/index.js'), | ||
phyFsManager = require('./js/PhysicalGFSManager.js'), | ||
gateway = require('./js/PhysicalGateway.js'); | ||
|
||
const server = new webdav.WebDAVServer({ | ||
port: 1900, | ||
autoSave: { | ||
treeFilePath: './data.json', | ||
tempTreeFilePath: './data.tmp.json' | ||
}, | ||
autoLoad: { | ||
treeFilePath: './data.json', | ||
fsManagers: [ | ||
new webdav.RootFSManager(), | ||
new phyFsManager.PhysicalGFSManager(), | ||
new webdav.VirtualFSManager() | ||
] | ||
} | ||
}); | ||
|
||
server.autoLoad((e) => { | ||
if(e) | ||
{ | ||
server.addResourceTree(new gateway.PhysicalGateway('./testData', 'phyGateway'), (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,39 @@ | ||
"use strict"; | ||
const webdav = require('../../../lib/index.js'), | ||
FTPClient = require('ftp'), | ||
PhysicalGateway = require('./PhysicalGateway.js').PhysicalGateway; | ||
|
||
module.exports.PhysicalGFSManager = function(config) | ||
{ | ||
const fsManager = new webdav.PhysicalFSManager(); | ||
fsManager.constructor = module.exports.PhysicalGFSManager; | ||
|
||
fsManager.uid = "PhysicalGFSManager_1.0.0"; | ||
|
||
fsManager.serialize = function(resource, obj) | ||
{ | ||
if(resource.constructor !== PhysicalGateway) | ||
return null; | ||
|
||
return { | ||
realPath: resource.realPath, | ||
dateCreation: resource.dateCreation, | ||
dateLastModified: resource.dateLastModified, | ||
properties: resource.properties, | ||
customName: resource.customName | ||
}; | ||
} | ||
|
||
fsManager.unserialize = function(data, obj) | ||
{ | ||
const rs = new PhysicalGateway(data.realPath, data.customName, null, this); | ||
|
||
rs.dateCreation = data.dateCreation; | ||
rs.dateLastModified = data.dateLastModified; | ||
rs.properties = data.properties; | ||
|
||
return rs; | ||
} | ||
|
||
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,183 @@ | ||
"use strict"; | ||
const webdav = require('../../../lib/index.js'), | ||
PhysicalGFSManager = require('./PhysicalGFSManager.js').PhysicalGFSManager, | ||
path = require('path'), | ||
fs = require('fs'); | ||
|
||
module.exports.PhysicalGateway = function(rootPath, customName, parent, fsManager) | ||
{ | ||
const gateway = new webdav.PhysicalFolder(rootPath, parent, fsManager ? fsManager : new PhysicalGFSManager()); | ||
gateway.constructor = module.exports.PhysicalGateway; | ||
|
||
gateway.customName = customName; | ||
gateway.rootPath = rootPath ? rootPath : '/'; | ||
gateway.cache = { | ||
'/': gateway | ||
}; | ||
|
||
const super_webName = gateway.webName; | ||
gateway.webName = function(callback) | ||
{ | ||
if(this.customName) | ||
callback(null, this.customName); | ||
else | ||
super_webName(callback); | ||
} | ||
|
||
gateway.listChildren = function(parent, rpath, callback) | ||
{ | ||
if(rpath.lastIndexOf('/') !== rpath.length - 1) | ||
rpath += '/'; | ||
|
||
fs.readdir(parent.realPath, (e, list) => { | ||
if(e) | ||
{ | ||
callback(e); | ||
return; | ||
} | ||
|
||
new webdav.Workflow() | ||
.each(list, (file, cb) => { | ||
const resourcePath = rpath + file; | ||
let resource = this.cache[resourcePath]; | ||
const realPath = path.join(parent.realPath, file); | ||
|
||
if(resource) | ||
{ | ||
cb(null, resource); | ||
return; | ||
} | ||
|
||
fs.stat(realPath, (e, stat) => { | ||
if(e) | ||
{ | ||
cb(e); | ||
return; | ||
} | ||
|
||
if(stat.isFile()) | ||
resource = new webdav.PhysicalFile(realPath, parent, this.fsManager); | ||
else | ||
resource = new webdav.PhysicalFolder(realPath, parent, this.fsManager); | ||
resource.deleteOnMoved = true; | ||
this.cache[resourcePath] = resource; | ||
cb(null, resource); | ||
}) | ||
}) | ||
.error(callback) | ||
.done((resources) => callback(null, resources)); | ||
}) | ||
} | ||
|
||
gateway.find = function(path, callback, forceRefresh = false) | ||
{ | ||
const resource = this.cache[path.toString()]; | ||
if(forceRefresh || !resource) | ||
{ | ||
const parentPath = path.getParent(); | ||
this.find(parentPath, (e, parent) => { | ||
if(e) | ||
{ | ||
callback(e); | ||
return; | ||
} | ||
|
||
parent.getChildren((e, actualChildren) => { | ||
if(e) | ||
{ | ||
callback(e); | ||
return; | ||
} | ||
|
||
this.listChildren(parent, parentPath.toString(), (e, children) => { | ||
if(e) | ||
{ | ||
callback(e); | ||
return; | ||
} | ||
|
||
actualChildren | ||
.filter((c) => c.constructor !== webdav.PhysicalResource && c.constructor !== webdav.PhysicalFile && c.constructor !== webdav.PhysicalFolder) | ||
.forEach((c) => children.push(c)); | ||
|
||
parent.children.children = children; | ||
|
||
new webdav.Workflow() | ||
.each(children, (child, cb) => { | ||
child.webName((e, name) => { | ||
cb(e, !e && name === path.fileName() ? child : null); | ||
}) | ||
}) | ||
.error(callback) | ||
.done((matchingChildren) => { | ||
for(const child of matchingChildren) | ||
if(child) | ||
{ | ||
callback(null, child); | ||
return; | ||
} | ||
|
||
callback(webdav.Errors.ResourceNotFound); | ||
}) | ||
}) | ||
}) | ||
}) | ||
} | ||
else | ||
callback(null, resource); | ||
} | ||
|
||
gateway.gateway = function(arg, path, callback) | ||
{ | ||
const updateChildren = (r, cb) => | ||
{ | ||
this.listChildren(r, path.toString(), (e, children) => { | ||
if(!e) | ||
{ | ||
r.children.children | ||
.filter((c) => c.constructor !== webdav.PhysicalResource && c.constructor !== webdav.PhysicalFile && c.constructor !== webdav.PhysicalFolder) | ||
.forEach((c) => children.push(c)); | ||
|
||
r.children.children = children; | ||
} | ||
cb(e); | ||
}) | ||
} | ||
|
||
if(path.isRoot()) | ||
{ | ||
updateChildren(this, (e) => { | ||
callback(e, this); | ||
}) | ||
return; | ||
} | ||
|
||
this.find(path, (e, r) => { | ||
if(e) | ||
{ | ||
callback(e); | ||
return; | ||
} | ||
|
||
r.type((e, type) => { | ||
if(e) | ||
{ | ||
callback(e); | ||
return; | ||
} | ||
|
||
if(type.isFile) | ||
{ | ||
callback(e, r); | ||
return; | ||
} | ||
|
||
updateChildren(r, (e) => { | ||
callback(e, r); | ||
}) | ||
}) | ||
}); | ||
} | ||
|
||
return gateway; | ||
} |
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": "customphysicalgateway", | ||
"version": "1.0.0", | ||
"description": "Example to show how to use a gateway to manage the content of a physical folder.", | ||
"main": "index.js", | ||
"author": "Adrien Castex <[email protected]>", | ||
"license": "Unlicense", | ||
"dependencies": { | ||
"webdav-server": "^1.9.0" | ||
} | ||
} |
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 @@ | ||
This is some data 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 @@ | ||
This is some data 3. |
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 @@ | ||
This is some data. |
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,31 @@ | ||
import { SerializedObject, FSManager, Errors, IResource, ResourceType, PhysicalFSManager, PhysicalFile, PhysicalFolder, PhysicalResource } from '../../../lib/index.js' | ||
import { PhysicalGateway } from './PhysicalGateway' | ||
|
||
export class PhysicalGFSManager extends PhysicalFSManager | ||
{ | ||
uid : string = "PhysicalGFSManager_1.0.0"; | ||
|
||
serialize(resource : any, obj : SerializedObject) : object | ||
{ | ||
if(resource.constructor !== PhysicalGateway) | ||
return null; | ||
|
||
return { | ||
realPath: resource.realPath, | ||
dateCreation: resource.dateCreation, | ||
dateLastModified: resource.dateLastModified, | ||
properties: resource.properties, | ||
customName: resource.customName | ||
}; | ||
} | ||
unserialize(data : any, obj : SerializedObject) : IResource | ||
{ | ||
const rs = new PhysicalGateway(data.realPath, data.customName, null, this); | ||
|
||
rs.dateCreation = data.dateCreation; | ||
rs.dateLastModified = data.dateLastModified; | ||
rs.properties = data.properties; | ||
|
||
return rs; | ||
} | ||
} |
Oops, something went wrong.