Skip to content

Commit

Permalink
Merge pull request #334 from jaison-x/adjust-filename-chatwoot
Browse files Browse the repository at this point in the history
fix(chatwoot): when possible use the original file extension
  • Loading branch information
DavidsonGomes authored Dec 29, 2023
2 parents 5bd3f28 + fcd8815 commit af94a0e
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/whatsapp/services/chatwoot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1527,20 +1527,21 @@ export class ChatwootService {
},
});

let prependFilename: string;
if (
body?.message[body?.messageType]?.fileName ||
body?.message[body?.messageType]?.message?.documentMessage?.fileName
) {
prependFilename = path.parse(
body?.message[body?.messageType]?.fileName ||
body?.message[body?.messageType]?.message?.documentMessage?.fileName,
).name;
prependFilename += `-${Math.floor(Math.random() * (99 - 10 + 1) + 10)}`;
} else {
prependFilename = Math.random().toString(36).substring(7);
let nameFile: string;
const messageBody = body?.message[body?.messageType];
const originalFilename = messageBody?.fileName || messageBody?.message?.documentMessage?.fileName;
if (originalFilename) {
const parsedFile = path.parse(originalFilename);
if (parsedFile.name && parsedFile.ext) {
nameFile = `${parsedFile.name}-${Math.floor(Math.random() * (99 - 10 + 1) + 10)}${parsedFile.ext}`;
}
}

if (!nameFile) {
nameFile = `${Math.random().toString(36).substring(7)}.${
mimeTypes.extension(downloadBase64.mimetype) || ''
}`;
}
const nameFile = `${prependFilename}.${mimeTypes.extension(downloadBase64.mimetype)}`;

const fileData = Buffer.from(downloadBase64.base64, 'base64');

Expand Down

0 comments on commit af94a0e

Please sign in to comment.