|
|
|
@@ -0,0 +1,85 @@ |
|
|
|
import * as fs from 'fs'; |
|
|
|
import { v2 as webdav } from 'webdav-server'; |
|
|
|
|
|
|
|
function getConfig(name: string) : any { |
|
|
|
let config = {}; |
|
|
|
let base = "./config/" + name; |
|
|
|
let debug = base + '.debug.json'; |
|
|
|
let prod = base + '.json'; |
|
|
|
let path = fs.existsSync(debug) ? debug : prod; |
|
|
|
if (fs.existsSync(path)) { |
|
|
|
try { |
|
|
|
config = JSON.parse(fs.readFileSync(path, 'utf8')); |
|
|
|
} |
|
|
|
catch (err) { |
|
|
|
} |
|
|
|
} |
|
|
|
return config; |
|
|
|
} |
|
|
|
|
|
|
|
const config = getConfig('srvconfig'); |
|
|
|
const users = getConfig('users'); |
|
|
|
|
|
|
|
if (typeof config.port === undefined) |
|
|
|
config.port = 8080; |
|
|
|
if (typeof config.realm === undefined) |
|
|
|
config.realm = "realm"; |
|
|
|
|
|
|
|
let mountNames : string[] = []; |
|
|
|
for (let mount in config['mounts']) { |
|
|
|
if (!!config['mounts'][mount] && fs.existsSync(config['mounts'][mount])) |
|
|
|
mountNames.push(mount); |
|
|
|
} |
|
|
|
|
|
|
|
// User manager (tells who are the users) |
|
|
|
const userManager: webdav.SimpleUserManager = new webdav.SimpleUserManager(); |
|
|
|
const privilegeManager: webdav.SimplePathPrivilegeManager = new webdav.SimplePathPrivilegeManager(); |
|
|
|
for (let nuser = 0 ; nuser < users.length; nuser++) { |
|
|
|
let isAdmin = !!users[nuser]['isAdmin']; |
|
|
|
const user = userManager.addUser(users[nuser]['username'], users[nuser]['password'], isAdmin); |
|
|
|
if (isAdmin) { |
|
|
|
privilegeManager.setRights(user, '/', [ 'all' ]); |
|
|
|
} |
|
|
|
else { |
|
|
|
if (!!users[nuser]['access']) { |
|
|
|
for (let userMount in users[nuser]['access']) { |
|
|
|
if (mountNames.includes(userMount) && !!users[nuser]['access'][userMount]) { |
|
|
|
privilegeManager.setRights(user, '/' + userMount, users[nuser]['access'][userMount]); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
console.log(user); |
|
|
|
} |
|
|
|
|
|
|
|
const httpAuthentication = new webdav.HTTPBasicAuthentication(userManager); |
|
|
|
|
|
|
|
const serverOptions: webdav.WebDAVServerOptions = { |
|
|
|
port: config.port || 8080, |
|
|
|
requireAuthentification: true, |
|
|
|
httpAuthentication, |
|
|
|
privilegeManager, |
|
|
|
}; |
|
|
|
//serverOptions['userManager'] = userManager; |
|
|
|
|
|
|
|
const server = new webdav.WebDAVServer(serverOptions); |
|
|
|
|
|
|
|
let nMount = 0; |
|
|
|
|
|
|
|
//@ts-ignore |
|
|
|
function mountAndStart() : void { |
|
|
|
if (nMount < mountNames.length) { |
|
|
|
let fsEntry = mountNames[nMount]; |
|
|
|
let realPath = config['mounts'][fsEntry]; |
|
|
|
nMount++; |
|
|
|
console.log(`FileSystem ${fsEntry} -> ${realPath}`) |
|
|
|
server.setFileSystem( |
|
|
|
'/' + fsEntry, new webdav.PhysicalFileSystem(realPath), (_success) => {mountAndStart();}); |
|
|
|
} |
|
|
|
else { |
|
|
|
server.start(httpServer => { |
|
|
|
console.log('Server started with success on the port : ' + httpServer.address()['port']); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
mountAndStart(); |