Skip to content

Commit

Permalink
fix(core.gbapp): Upload now saves the file in correct folder.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanPerdomo committed Apr 29, 2023
1 parent 0c1c02e commit e31cd4d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 35 deletions.
9 changes: 5 additions & 4 deletions packages/basic.gblib/services/DialogKeywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import QrScanner from 'qr-scanner';
import pkg from 'whatsapp-web.js';
import { ActivityTypes } from 'botbuilder';
const { List, Buttons } = pkg;
import mime from 'mime-types';
import mime from 'mime';

/**
* Default check interval for user replay
Expand Down Expand Up @@ -1171,7 +1171,7 @@ export class DialogKeywords {
if (!filename.startsWith('https://')) {
url = urlJoin(GBServer.globals.publicAddress, 'kb', gbaiName, 'assets', filename);
} else {
url = filename;
url = filename
}
}

Expand All @@ -1180,11 +1180,12 @@ export class DialogKeywords {

const imageData = await (await fetch(url)).arrayBuffer();
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'].push({
name: filename,
contentType: contentType,
contentType: ext,
contentUrl: `data:${contentType};base64,${base64Image}`
});

Expand Down
65 changes: 34 additions & 31 deletions packages/core.gbapp/services/GBMinService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ import { createKoaHttpServer } from '../../basic.gblib/index.js';
import { DebuggerService } from '../../basic.gblib/services/DebuggerService.js';
import { ImageProcessingServices } from '../../basic.gblib/services/ImageProcessingServices.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.
*/
Expand Down Expand Up @@ -1064,7 +1065,7 @@ export class GBMinService {
await this.processEventActivity(min, user, context, step);
}
} 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 : '') : ''
}`;
GBLog.error(msg);
Expand Down Expand Up @@ -1132,45 +1133,46 @@ export class GBMinService {
/**
* Private handler which receives the Attachment and persists to disk.
* during a HEAR attachment AS FILE upload.
*/
*/
// ...

private static async downloadAttachmentAndWrite(attachment) {
const url = attachment.contentUrl;
const localFolder = Path.join('work');
const path = DialogKeywords.getGBAIPath(this['min'].botId);
const localFileName = Path.join(localFolder, path, 'uploads', attachment.name);

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());
const url = attachment.contentUrl;
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());
}

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.
*
* */

public static isGlobalQuitUtterance(locale, utterance) {
return utterance.match(Messages.global_quit);
}
Expand Down Expand Up @@ -1260,6 +1262,7 @@ export class GBMinService {
filename: successfulSaves[0]['fileName']
};
accum.push(result);
return accum;
}, []) as GBFile[];

if (min.cbMap[userId] && min.cbMap[userId].promise == '!GBHEAR') {
Expand Down

0 comments on commit e31cd4d

Please sign in to comment.