diff --git a/packages/core.gbapp/services/GBCoreService.ts b/packages/core.gbapp/services/GBCoreService.ts index 9aa69639..cdd38dc6 100644 --- a/packages/core.gbapp/services/GBCoreService.ts +++ b/packages/core.gbapp/services/GBCoreService.ts @@ -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); + } } diff --git a/packages/core.gbapp/services/GBMinService.ts b/packages/core.gbapp/services/GBMinService.ts index 11af269b..746114fd 100644 --- a/packages/core.gbapp/services/GBMinService.ts +++ b/packages/core.gbapp/services/GBMinService.ts @@ -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 { // 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) { diff --git a/src/app.ts b/src/app.ts index 6c7f1b11..d3efabdf 100644 --- a/src/app.ts +++ b/src/app.ts @@ -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,9 +103,7 @@ 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' })); server.use(bodyParser.urlencoded({ limit: '1mb', extended: true })); @@ -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; diff --git a/templates/default.gbai/default.gbot/config.csv b/templates/default.gbai/default.gbot/config.csv index 4b092678..a324b941 100644 --- a/templates/default.gbai/default.gbot/config.csv +++ b/templates/default.gbai/default.gbot/config.csv @@ -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, \ No newline at end of file +WhatsApp Group Shortcuts, +WebDav Username,admin +WebDav Password,&y5F9$B.1*Q6 \ No newline at end of file