new(all): WebDav support for all bots.

This commit is contained in:
Rodrigo Rodriguez 2024-08-30 14:15:02 -03:00
parent 3952724f7a
commit 053ff4c8f6
4 changed files with 51 additions and 20 deletions

View file

@ -34,7 +34,7 @@
'use strict';
import { GBLog, IGBCoreService, IGBInstallationDeployer, IGBInstance, IGBPackage } from 'botlib';
import { GBLog, GBMinInstance, IGBCoreService, IGBInstallationDeployer, IGBInstance, IGBPackage } from 'botlib';
import * as Fs from 'fs';
import { Sequelize, SequelizeOptions } from 'sequelize-typescript';
import { Op, Dialect } from 'sequelize';
@ -47,6 +47,7 @@ import { GBCorePackage } from '../../core.gbapp/index.js';
import { GBCustomerSatisfactionPackage } from '../../customer-satisfaction.gbapp/index.js';
import { GBKBPackage } from '../../kb.gbapp/index.js';
import { GBSecurityPackage } from '../../security.gbapp/index.js';
import { v2 as webdav } from 'webdav-server';
import { GBWhatsappPackage } from '../../whatsapp.gblib/index.js';
import { GuaribasApplications, GuaribasInstance, GuaribasLog } from '../models/GBModel.js';
import { GBConfigService } from './GBConfigService.js';
@ -892,4 +893,36 @@ ENDPOINT_UPDATE=true
}
}
}
public static async createWebDavServer(minInstances: GBMinInstance[]) {
const userManager = new webdav.SimpleUserManager();
const privilegeManager = new webdav.SimplePathPrivilegeManager();
// Create the WebDAV server
const server = new webdav.WebDAVServer({
port: 1900,
httpAuthentication: new webdav.HTTPDigestAuthentication(userManager, 'Default realm'),
privilegeManager: privilegeManager
});
GBServer.globals.webDavServer = server;
minInstances.forEach(min => {
const user = min.core.getParam(min.instance, 'WebDav Username', GBConfigService.get('WEBDAV_USERNAME'));
const pass = min.core.getParam(min.instance, 'WebDav Password', GBConfigService.get('WEBDAV_PASSWORD'));
if (user && pass) {
const objUser = userManager.addUser(user, pass);
const virtualPath = '/' + min.botId;
let path = DialogKeywords.getGBAIPath(min.botId, null);
const gbaiRoot = Path.join(GBConfigService.get('STORAGE_LIBRARY'), path);
server.setFileSystem(virtualPath, new webdav.PhysicalFileSystem(gbaiRoot), successed => {
GBLogEx.info(min.instance.instanceId, `WebDav online for ${min.botId}...`);
});
privilegeManager.setRights(objUser, virtualPath, ['all']);
}
});
server.start(1900);
}
}

View file

@ -147,7 +147,7 @@ export class GBMinService {
/**
* Constructs a new minimal instance for each bot.
*/
public async buildMin(instances: IGBInstance[]) {
public async buildMin(instances: IGBInstance[]) : Promise<GBMinInstance[]> {
// Servers default UI on root address '/' if web enabled.
if (process.env.DISABLE_WEB !== 'true') {
@ -166,6 +166,7 @@ export class GBMinService {
// Calls mountBot event to all bots.
let i = 1;
const minInstances = [];
await CollectionUtil.asyncForEach(
instances,
@ -173,7 +174,8 @@ export class GBMinService {
try {
GBLog.info(`Mounting ${instance.botId}...`);
await this['mountBot'](instance);
const min = await this['mountBot'](instance);
minInstances.push(min);
} catch (error) {
GBLog.error(`Error mounting bot ${instance.botId}: ${error.message}\n${error.stack}`);
}
@ -187,7 +189,9 @@ export class GBMinService {
const service = new ScheduleServices();
await service.scheduleAll();
GBLogEx.info(0, `All Bot instances loaded.`);
GBLogEx.info(0, `All Bot service instances loaded.`);
return minInstances;
}
public async startSimpleTest(min) {
@ -348,15 +352,6 @@ export class GBMinService {
mkdirp.sync(dir);
}
if (!GBConfigService.get('STORAGE_NAME')) {
dir = Path.join(GBConfigService.get('STORAGE_LIBRARY'), 'work', gbai);
const server = GBServer.globals.webDavServer;
server.setFileSystem(`/${botId}`, new webdav.PhysicalFileSystem(dir), success => {
GBLogEx.info(1, `WebDav for ${botId} loaded.`);
});
}
// Calls the loadBot context.activity for all packages.
await this.invokeLoadBot(min.appPackages, GBServer.globals.sysPackages, min);
@ -464,6 +459,8 @@ export class GBMinService {
await this.ensureAPI();
return min;
}
public static getProviderName(req: any, res: any) {

View file

@ -40,7 +40,6 @@ import bodyParser from 'body-parser';
import { GBLog, GBMinInstance, IGBCoreService, IGBInstance } from 'botlib';
import child_process from 'child_process';
import express from 'express';
import { v2 as webdav } from 'webdav-server';
import fs from 'fs';
import http from 'http';
import httpProxy from 'http-proxy';
@ -104,8 +103,6 @@ export class GBServer {
GBServer.globals.debuggers = [];
GBServer.globals.users = [];
GBServer.globals.indexSemaphore = new Mutex();
GBServer.globals.webDavServer = new webdav.WebDAVServer();
GBServer.globals.webDavServer.start();
server.use(bodyParser.json());
server.use(bodyParser.json({ limit: '1mb' }));
@ -252,7 +249,9 @@ export class GBServer {
// Builds minimal service infrastructure.
await minService.buildMin(instances);
const minInstances = await minService.buildMin(instances);
GBServer.globals.webDavServer = await GBCoreService.createWebDavServer(minInstances);
server.all('*', async (req, res, next) => {
const host = req.headers.host;

View file

@ -47,8 +47,10 @@ Twitter Access Token,
Twitter Access Token Secret,
Twitter Consumer Key,
Twitter Consumer Key Secret,
Website, https://www.iochpe.com.br/en
Website, https:/github.com/GeneralBots
Welcome Article,
WhatsApp Admins,
WhatsApp Group ID,
WhatsApp Group Shortcuts,
WebDav Username,admin
WebDav Password,&y5F9$B.1*Q6
1 name value
47 Twitter Access Token Secret
48 Twitter Consumer Key
49 Twitter Consumer Key Secret
50 Website https://www.iochpe.com.br/en https:/github.com/GeneralBots
51 Welcome Article
52 WhatsApp Admins
53 WhatsApp Group ID
54 WhatsApp Group Shortcuts
55 WebDav Username admin
56 WebDav Password &y5F9$B.1*Q6