Skip to content

Commit

Permalink
Fixed issue where uploaded attachments weren't being encoded and deco…
Browse files Browse the repository at this point in the history
…ded properly. (#1678)
  • Loading branch information
tonyanziano authored and cwhitten committed Jul 12, 2019
1 parent 1af1ed9 commit d3f264c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## Fixed
- [main] Fixed an issue where uploaded attachments weren't being encoded and decoded properly in PR [1678](https://github.com/microsoft/BotFramework-Emulator/pull/1678)

## v4.5.0 - 2019 - 07 - 11
## Added
- [main] Added ability to launch into a bot inspector mode session via protocol url in PR [1617](https://github.com/microsoft/BotFramework-Emulator/pull/1617)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ describe('The getAttachment middleware', () => {
let facilities;
let attachments;
let attachmentId;
const mockAttachmentData = new Uint8Array(Buffer.from('aGk='));

beforeEach(() => {
attachments = new Attachments();
attachmentId = attachments.uploadAttachment({
name: 'an attachment',
originalBase64: new Uint8Array(Buffer.from('aGk=')),
originalBase64: mockAttachmentData,
type: 'application/text',
thumbnailBase64: new Uint8Array(Buffer.from('aGk=')),
thumbnailBase64: mockAttachmentData,
});
facilities = {
attachments,
Expand Down Expand Up @@ -80,7 +82,7 @@ describe('The getAttachment middleware', () => {
} as any
);

expect(sendSpy).toHaveBeenCalledWith(HttpStatus.OK, Buffer.from('aGk=', 'base64'));
expect(sendSpy).toHaveBeenCalledWith(HttpStatus.OK, Buffer.from(mockAttachmentData));
expect(res.contentType).toBe('application/text');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ export default function getAttachment(bot: BotEmulator) {
if (attachmentBase64) {
// can be an ArrayBuffer if uploaded via the Web Chat paperclip control, or can be
// an already-encoded base64 content string if sent from the bot
const bufferContents = attachmentBase64.buffer
? Buffer.from(attachmentBase64.buffer as ArrayBuffer).toString()
: attachmentBase64.toString();
const buffer = Buffer.from(bufferContents, 'base64');
let buffer;
if (attachmentBase64.buffer) {
buffer = Buffer.from(attachmentBase64 as any);
} else {
buffer = Buffer.from(attachmentBase64.toString(), 'base64');
}

res.contentType = attachment.type;
res.send(HttpStatus.OK, buffer);
Expand Down
8 changes: 4 additions & 4 deletions packages/emulator/core/src/directLine/middleware/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ export default function upload(botEmulator: BotEmulator) {
const name = (upload1 as any).name || 'file.dat';
const type = upload1.type;
const path = upload1.path;
const buf: Buffer = fs.readFileSync(path);
const base64Buffer = Buffer.from(buf.toString('base64'));
const base64EncodedContent = fs.readFileSync(path, { encoding: 'base64' });
const base64Buf = Buffer.from(base64EncodedContent, 'base64');
const attachmentData: AttachmentData = {
type,
name,
originalBase64: new Uint8Array(base64Buffer.buffer),
thumbnailBase64: new Uint8Array(base64Buffer.buffer),
originalBase64: new Uint8Array(base64Buf),
thumbnailBase64: new Uint8Array(base64Buf),
};
const attachmentId = botEmulator.facilities.attachments.uploadAttachment(attachmentData);
const attachment: Attachment = {
Expand Down

0 comments on commit d3f264c

Please sign in to comment.