Skip to content

Commit

Permalink
Added a 2nd example of creating a custom method for the server
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed Jul 10, 2017
1 parent 5efdead commit d191897
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
9 changes: 9 additions & 0 deletions examples/v2/customMethod2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Custom method

This is an example to show how to make a custom method.

This example focus on the `TRACE` method, to send to a remote user all modified/created resources since the last request.

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.

Note : The methods which can be added are limited to [the supported methods](https://nodejs.org/api/http.html#http_http_methods) in the `http` module.
56 changes: 56 additions & 0 deletions examples/v2/customMethod2/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const webdav = require('webdav-server').v2;

// Server instantiation
const server = new webdav.WebDAVServer();

let folderNotify = { };
server.method('TRACE', {
unchunked(ctx, data, cb)
{
if(!ctx.user)
{
ctx.setCode(webdav.HTTPCodes.Unauthorized);
return cb();
}

const path = ctx.requested.path.toString(true);
const name = ctx.user.username;
let lastCheck = 0;
if(!folderNotify[name])
folderNotify[name] = {};
if(folderNotify[name][path])
lastCheck = folderNotify[name][path];

const list = [];

ctx.getResource((e, r) => {
r.readDir((e, files) => {
let nb = files.length + 1;
const go = () => {
if(--nb === 0)
{
ctx.setCode(webdav.HTTPCodes.OK);
ctx.response.write(JSON.stringify(list));
folderNotify[name][path] = Date.now();
cb();
}
}
go();

files.forEach((file) => {
r.fs.lastModifiedDate(ctx, r.path.toString(true) + file, (e, date) => {
if(date >= lastCheck)
list.push({
path: path + file,
name: file
});

go();
})
})
})
})
}
});

server.start((s) => console.log('Ready on port', s.address().port));
57 changes: 57 additions & 0 deletions examples/v2/customMethod2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { v2 as webdav } from 'webdav-server'
import * as request from 'request'

// Server instantiation
const server = new webdav.WebDAVServer();

let folderNotify = { };
server.method('TRACE', {
unchunked(ctx : webdav.HTTPRequestContext, data : Buffer, cb : () => void)
{
if(!ctx.user)
{
ctx.setCode(webdav.HTTPCodes.Unauthorized);
return cb();
}

const path = ctx.requested.path.toString(true);
const name = ctx.user.username;
let lastCheck = 0;
if(!folderNotify[name])
folderNotify[name] = {};
if(folderNotify[name][path])
lastCheck = folderNotify[name][path];

const list = [];

ctx.getResource((e, r) => {
r.readDir((e, files) => {
let nb = files.length + 1;
const go = () => {
if(--nb === 0)
{
ctx.setCode(webdav.HTTPCodes.OK);
ctx.response.write(JSON.stringify(list));
folderNotify[name][path] = Date.now();
cb();
}
}
go();

files.forEach((file) => {
r.fs.lastModifiedDate(ctx, r.path.toString(true) + file, (e, date) => {
if(date >= lastCheck)
list.push({
path: path + file,
name: file
});

go();
})
})
})
})
}
});

server.start((s) => console.log('Ready on port', s.address().port));
11 changes: 11 additions & 0 deletions examples/v2/customMethod2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "custom-method",
"version": "1.0.0",
"description": "Example to show how to make a custom method.",
"main": "index.js",
"author": "Adrien Castex <adrien.castex@protonmail.com>",
"license": "Unlicense",
"dependencies": {
"webdav-server": "^2.0.0"
}
}

0 comments on commit d191897

Please sign in to comment.