fix(core.gbapp): Upload now saves the file in correct folder.

This commit is contained in:
Alan 2023-04-29 14:18:10 -03:00
parent 0c1c02e07a
commit e31cd4d819
2 changed files with 39 additions and 35 deletions

View file

@ -60,7 +60,7 @@ import QrScanner from 'qr-scanner';
import pkg from 'whatsapp-web.js'; import pkg from 'whatsapp-web.js';
import { ActivityTypes } from 'botbuilder'; import { ActivityTypes } from 'botbuilder';
const { List, Buttons } = pkg; const { List, Buttons } = pkg;
import mime from 'mime-types'; import mime from 'mime';
/** /**
* Default check interval for user replay * Default check interval for user replay
@ -1171,7 +1171,7 @@ export class DialogKeywords {
if (!filename.startsWith('https://')) { if (!filename.startsWith('https://')) {
url = urlJoin(GBServer.globals.publicAddress, 'kb', gbaiName, 'assets', filename); url = urlJoin(GBServer.globals.publicAddress, 'kb', gbaiName, 'assets', filename);
} else { } else {
url = filename; url = filename
} }
} }
@ -1180,11 +1180,12 @@ export class DialogKeywords {
const imageData = await (await fetch(url)).arrayBuffer(); const imageData = await (await fetch(url)).arrayBuffer();
const base64Image = Buffer.from(imageData).toString('base64'); const base64Image = Buffer.from(imageData).toString('base64');
const contentType = mime.lookup(url); const contentType = mime.getType(url);
const ext = mime.getExtension(contentType);
reply['attachments'] = []; reply['attachments'] = [];
reply['attachments'].push({ reply['attachments'].push({
name: filename, name: filename,
contentType: contentType, contentType: ext,
contentUrl: `data:${contentType};base64,${base64Image}` contentUrl: `data:${contentType};base64,${base64Image}`
}); });

View file

@ -99,6 +99,7 @@ import { createKoaHttpServer } from '../../basic.gblib/index.js';
import { DebuggerService } from '../../basic.gblib/services/DebuggerService.js'; import { DebuggerService } from '../../basic.gblib/services/DebuggerService.js';
import { ImageProcessingServices } from '../../basic.gblib/services/ImageProcessingServices.js'; import { ImageProcessingServices } from '../../basic.gblib/services/ImageProcessingServices.js';
import { ScheduleServices } from '../../basic.gblib/services/ScheduleServices.js'; import { ScheduleServices } from '../../basic.gblib/services/ScheduleServices.js';
import mime from 'mime';
/** /**
* Minimal service layer for a bot and encapsulation of BOT Framework calls. * Minimal service layer for a bot and encapsulation of BOT Framework calls.
*/ */
@ -1064,7 +1065,7 @@ export class GBMinService {
await this.processEventActivity(min, user, context, step); await this.processEventActivity(min, user, context, step);
} }
} catch (error) { } catch (error) {
const msg = `ERROR: ${error.message} ${error.error ? error.error.body : ''} ${ const msg = `ERROR: ${error.message} ${error.stack} ${error.error ? error.error.body : ''} ${
error.error ? (error.error.stack ? error.error.stack : '') : '' error.error ? (error.error.stack ? error.error.stack : '') : ''
}`; }`;
GBLog.error(msg); GBLog.error(msg);
@ -1133,44 +1134,45 @@ export class GBMinService {
* Private handler which receives the Attachment and persists to disk. * Private handler which receives the Attachment and persists to disk.
* during a HEAR attachment AS FILE upload. * during a HEAR attachment AS FILE upload.
*/ */
private static async downloadAttachmentAndWrite(attachment) { // ...
const url = attachment.contentUrl;
const localFolder = Path.join('work'); private static async downloadAttachmentAndWrite(attachment) {
const path = DialogKeywords.getGBAIPath(this['min'].botId); const url = attachment.contentUrl;
const localFileName = Path.join(localFolder, path, 'uploads', attachment.name); const localFolder = Path.join('work');
const path = DialogKeywords.getGBAIPath(this['min'].botId);
const localFileName = Path.join(localFolder, path, 'uploads',
attachment.name
);
let buffer;
if (url.startsWith('data:')) {
const base64Data = url.split(';base64,')[1];
buffer = Buffer.from(base64Data, 'base64');
} else {
const options = {
method: 'GET',
encoding: 'binary'
};
const res = await fetch(url, options);
buffer = arrayBufferToBuffer(await res.arrayBuffer());
}
let res;
if (url.startsWith('data:')) {
var regex = /^data:.+\/(.+);base64,(.*)$/;
var matches = url.match(regex);
var ext = matches[1];
var data = matches[2];
res = Buffer.from(data, 'base64');
} else {
// arraybuffer is necessary for images
const options = {
method: 'GET',
encoding: 'binary'
};
res = await fetch(url, options);
const buffer = arrayBufferToBuffer(await res.arrayBuffer());
Fs.writeFileSync(localFileName, buffer); Fs.writeFileSync(localFileName, buffer);
return {
fileName:
attachment.name
,
localPath: localFileName
};
} }
// If no error was thrown while writing to disk,return the attachment's name
// and localFilePath for the response back to the user.
return {
fileName: attachment.name,
localPath: localFileName
};
}
/** /**
* *
* Checks for global exit kewywords cancelling any active dialogs. * Checks for global exit kewywords cancelling any active dialogs.
* *
* */ * */
public static isGlobalQuitUtterance(locale, utterance) { public static isGlobalQuitUtterance(locale, utterance) {
return utterance.match(Messages.global_quit); return utterance.match(Messages.global_quit);
} }
@ -1260,6 +1262,7 @@ export class GBMinService {
filename: successfulSaves[0]['fileName'] filename: successfulSaves[0]['fileName']
}; };
accum.push(result); accum.push(result);
return accum;
}, []) as GBFile[]; }, []) as GBFile[];
if (min.cbMap[userId] && min.cbMap[userId].promise == '!GBHEAR') { if (min.cbMap[userId] && min.cbMap[userId].promise == '!GBHEAR') {