Skip to content

Commit 9123bf6

Browse files
committed
Added an example of a physical gateway
1 parent d17ac14 commit 9123bf6

File tree

11 files changed

+502
-0
lines changed

11 files changed

+502
-0
lines changed

examples/gatewayPhysical/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Custom Physical gateway
2+
3+
This is an example to show how to use a gateway to manage the content of a physical folder.
4+
5+
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.
6+
7+
## Usage
8+
9+
### Execute
10+
11+
```bash
12+
node index.js
13+
```

examples/gatewayPhysical/data.json

476 Bytes
Binary file not shown.

examples/gatewayPhysical/index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const webdav = require('../../lib/index.js'),
2+
phyFsManager = require('./js/PhysicalGFSManager.js'),
3+
gateway = require('./js/PhysicalGateway.js');
4+
5+
const server = new webdav.WebDAVServer({
6+
port: 1900,
7+
autoSave: {
8+
treeFilePath: './data.json',
9+
tempTreeFilePath: './data.tmp.json'
10+
},
11+
autoLoad: {
12+
treeFilePath: './data.json',
13+
fsManagers: [
14+
new webdav.RootFSManager(),
15+
new phyFsManager.PhysicalGFSManager(),
16+
new webdav.VirtualFSManager()
17+
]
18+
}
19+
});
20+
21+
server.autoLoad((e) => {
22+
if(e)
23+
{
24+
server.addResourceTree(new gateway.PhysicalGateway('./testData', 'phyGateway'), (e) => {
25+
if(e) throw e;
26+
27+
run();
28+
});
29+
}
30+
else
31+
run();
32+
})
33+
34+
function run()
35+
{
36+
server.start((s) => {
37+
console.log('Server started on port ' + s.address().port + '.');
38+
});
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"use strict";
2+
const webdav = require('../../../lib/index.js'),
3+
FTPClient = require('ftp'),
4+
PhysicalGateway = require('./PhysicalGateway.js').PhysicalGateway;
5+
6+
module.exports.PhysicalGFSManager = function(config)
7+
{
8+
const fsManager = new webdav.PhysicalFSManager();
9+
fsManager.constructor = module.exports.PhysicalGFSManager;
10+
11+
fsManager.uid = "PhysicalGFSManager_1.0.0";
12+
13+
fsManager.serialize = function(resource, obj)
14+
{
15+
if(resource.constructor !== PhysicalGateway)
16+
return null;
17+
18+
return {
19+
realPath: resource.realPath,
20+
dateCreation: resource.dateCreation,
21+
dateLastModified: resource.dateLastModified,
22+
properties: resource.properties,
23+
customName: resource.customName
24+
};
25+
}
26+
27+
fsManager.unserialize = function(data, obj)
28+
{
29+
const rs = new PhysicalGateway(data.realPath, data.customName, null, this);
30+
31+
rs.dateCreation = data.dateCreation;
32+
rs.dateLastModified = data.dateLastModified;
33+
rs.properties = data.properties;
34+
35+
return rs;
36+
}
37+
38+
return fsManager;
39+
}
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
"use strict";
2+
const webdav = require('../../../lib/index.js'),
3+
PhysicalGFSManager = require('./PhysicalGFSManager.js').PhysicalGFSManager,
4+
path = require('path'),
5+
fs = require('fs');
6+
7+
module.exports.PhysicalGateway = function(rootPath, customName, parent, fsManager)
8+
{
9+
const gateway = new webdav.PhysicalFolder(rootPath, parent, fsManager ? fsManager : new PhysicalGFSManager());
10+
gateway.constructor = module.exports.PhysicalGateway;
11+
12+
gateway.customName = customName;
13+
gateway.rootPath = rootPath ? rootPath : '/';
14+
gateway.cache = {
15+
'/': gateway
16+
};
17+
18+
const super_webName = gateway.webName;
19+
gateway.webName = function(callback)
20+
{
21+
if(this.customName)
22+
callback(null, this.customName);
23+
else
24+
super_webName(callback);
25+
}
26+
27+
gateway.listChildren = function(parent, rpath, callback)
28+
{
29+
if(rpath.lastIndexOf('/') !== rpath.length - 1)
30+
rpath += '/';
31+
32+
fs.readdir(parent.realPath, (e, list) => {
33+
if(e)
34+
{
35+
callback(e);
36+
return;
37+
}
38+
39+
new webdav.Workflow()
40+
.each(list, (file, cb) => {
41+
const resourcePath = rpath + file;
42+
let resource = this.cache[resourcePath];
43+
const realPath = path.join(parent.realPath, file);
44+
45+
if(resource)
46+
{
47+
cb(null, resource);
48+
return;
49+
}
50+
51+
fs.stat(realPath, (e, stat) => {
52+
if(e)
53+
{
54+
cb(e);
55+
return;
56+
}
57+
58+
if(stat.isFile())
59+
resource = new webdav.PhysicalFile(realPath, parent, this.fsManager);
60+
else
61+
resource = new webdav.PhysicalFolder(realPath, parent, this.fsManager);
62+
resource.deleteOnMoved = true;
63+
this.cache[resourcePath] = resource;
64+
cb(null, resource);
65+
})
66+
})
67+
.error(callback)
68+
.done((resources) => callback(null, resources));
69+
})
70+
}
71+
72+
gateway.find = function(path, callback, forceRefresh = false)
73+
{
74+
const resource = this.cache[path.toString()];
75+
if(forceRefresh || !resource)
76+
{
77+
const parentPath = path.getParent();
78+
this.find(parentPath, (e, parent) => {
79+
if(e)
80+
{
81+
callback(e);
82+
return;
83+
}
84+
85+
parent.getChildren((e, actualChildren) => {
86+
if(e)
87+
{
88+
callback(e);
89+
return;
90+
}
91+
92+
this.listChildren(parent, parentPath.toString(), (e, children) => {
93+
if(e)
94+
{
95+
callback(e);
96+
return;
97+
}
98+
99+
actualChildren
100+
.filter((c) => c.constructor !== webdav.PhysicalResource && c.constructor !== webdav.PhysicalFile && c.constructor !== webdav.PhysicalFolder)
101+
.forEach((c) => children.push(c));
102+
103+
parent.children.children = children;
104+
105+
new webdav.Workflow()
106+
.each(children, (child, cb) => {
107+
child.webName((e, name) => {
108+
cb(e, !e && name === path.fileName() ? child : null);
109+
})
110+
})
111+
.error(callback)
112+
.done((matchingChildren) => {
113+
for(const child of matchingChildren)
114+
if(child)
115+
{
116+
callback(null, child);
117+
return;
118+
}
119+
120+
callback(webdav.Errors.ResourceNotFound);
121+
})
122+
})
123+
})
124+
})
125+
}
126+
else
127+
callback(null, resource);
128+
}
129+
130+
gateway.gateway = function(arg, path, callback)
131+
{
132+
const updateChildren = (r, cb) =>
133+
{
134+
this.listChildren(r, path.toString(), (e, children) => {
135+
if(!e)
136+
{
137+
r.children.children
138+
.filter((c) => c.constructor !== webdav.PhysicalResource && c.constructor !== webdav.PhysicalFile && c.constructor !== webdav.PhysicalFolder)
139+
.forEach((c) => children.push(c));
140+
141+
r.children.children = children;
142+
}
143+
cb(e);
144+
})
145+
}
146+
147+
if(path.isRoot())
148+
{
149+
updateChildren(this, (e) => {
150+
callback(e, this);
151+
})
152+
return;
153+
}
154+
155+
this.find(path, (e, r) => {
156+
if(e)
157+
{
158+
callback(e);
159+
return;
160+
}
161+
162+
r.type((e, type) => {
163+
if(e)
164+
{
165+
callback(e);
166+
return;
167+
}
168+
169+
if(type.isFile)
170+
{
171+
callback(e, r);
172+
return;
173+
}
174+
175+
updateChildren(r, (e) => {
176+
callback(e, r);
177+
})
178+
})
179+
});
180+
}
181+
182+
return gateway;
183+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "customphysicalgateway",
3+
"version": "1.0.0",
4+
"description": "Example to show how to use a gateway to manage the content of a physical folder.",
5+
"main": "index.js",
6+
"author": "Adrien Castex <[email protected]>",
7+
"license": "Unlicense",
8+
"dependencies": {
9+
"webdav-server": "^1.9.0"
10+
}
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is some data 2.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is some data 3.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is some data.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { SerializedObject, FSManager, Errors, IResource, ResourceType, PhysicalFSManager, PhysicalFile, PhysicalFolder, PhysicalResource } from '../../../lib/index.js'
2+
import { PhysicalGateway } from './PhysicalGateway'
3+
4+
export class PhysicalGFSManager extends PhysicalFSManager
5+
{
6+
uid : string = "PhysicalGFSManager_1.0.0";
7+
8+
serialize(resource : any, obj : SerializedObject) : object
9+
{
10+
if(resource.constructor !== PhysicalGateway)
11+
return null;
12+
13+
return {
14+
realPath: resource.realPath,
15+
dateCreation: resource.dateCreation,
16+
dateLastModified: resource.dateLastModified,
17+
properties: resource.properties,
18+
customName: resource.customName
19+
};
20+
}
21+
unserialize(data : any, obj : SerializedObject) : IResource
22+
{
23+
const rs = new PhysicalGateway(data.realPath, data.customName, null, this);
24+
25+
rs.dateCreation = data.dateCreation;
26+
rs.dateLastModified = data.dateLastModified;
27+
rs.properties = data.properties;
28+
29+
return rs;
30+
}
31+
}

0 commit comments

Comments
 (0)