Skip to content

Commit ebf1578

Browse files
committed
Refactor saveMediaToFile to use options object and update all usages
- Introduce SaveMediaOptions type for saveMediaToFile parameters - Replace direct destination param with options object in all calls - Add optional persistant and baseFolder fields to options - Update relativePath logic to use baseFolder or persistant flag - Refactor SendMessage and WbotServices to use new options format - Improve code clarity and future extensibility
1 parent 00b7c0c commit ebf1578

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

backend/src/helpers/SendMessage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export const SendMessage = async (
6464
mime.lookup(originalFilename) || "application/octet-stream",
6565
filename: messageData.mediaPath.split("/").pop() || "file.bin"
6666
},
67-
whatsapp.companyId
67+
{ destination: whatsapp.companyId }
6868
)
6969
);
7070
if (!fileUrl.startsWith("http")) {

backend/src/helpers/saveMediaFile.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@ import { logger } from "../utils/logger";
66
import { makeRandomId } from "./MakeRandomId";
77
import Ticket from "../models/Ticket";
88

9+
type SaveMediaOptions = {
10+
destination: Ticket | number;
11+
persistant?: boolean;
12+
baseFolder?: string;
13+
};
14+
915
export default async function saveMediaToFile(
1016
media: {
1117
data: FileContents;
1218
mimetype: string;
1319
filename: string;
1420
},
15-
destination: Ticket | number
21+
{ destination, persistant, baseFolder }: SaveMediaOptions
1622
): Promise<string> {
1723
if (!media || !media.data || !media.mimetype || !destination) {
1824
logger.error("saveMediaToFile: Invalid media or destination provided");
@@ -33,7 +39,9 @@ export default async function saveMediaToFile(
3339
typeof destination === "number" ? undefined : destination.contactId;
3440
const ticketId = typeof destination === "number" ? undefined : destination.id;
3541

36-
let relativePath = `media/${companyId}/`;
42+
const basePath = baseFolder || persistant ? "media-persistant" : "media";
43+
44+
let relativePath = `${basePath || "media"}/${companyId}/`;
3745

3846
if (contactId && ticketId) {
3947
relativePath += `${contactId}/${ticketId}/`;

backend/src/services/WbotServices/SendWhatsAppMedia.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ export const SendWhatsAppMedia = async ({
183183
mimetype: media.mimetype,
184184
filename: fileName || media.originalname
185185
},
186-
ticket
186+
{ destination: ticket }
187187
);
188188
readableFile.destroy();
189189

backend/src/services/WbotServices/wbotMessageListener.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -573,13 +573,15 @@ const storeQuotedMessage = async (
573573
let mediaUrl = null;
574574
if (media) {
575575
// eslint-disable-next-line no-use-before-define
576-
mediaUrl = await saveMediaToFile(media, ticket);
576+
mediaUrl = await saveMediaToFile(media, { destination: ticket });
577577
}
578578

579579
let thumbnailUrl = null;
580580
if (thumbnailMedia) {
581581
// eslint-disable-next-line no-use-before-define
582-
thumbnailUrl = await saveMediaToFile(thumbnailMedia, ticket);
582+
thumbnailUrl = await saveMediaToFile(thumbnailMedia, {
583+
destination: ticket
584+
});
583585
}
584586

585587
const mediaType = media?.mimetype.split("/")[0];
@@ -696,12 +698,14 @@ export const verifyMediaMessage = async (
696698

697699
let mediaUrl = mediaInfo?.mediaUrl || null;
698700
if (media) {
699-
mediaUrl = await saveMediaToFile(media, ticket);
701+
mediaUrl = await saveMediaToFile(media, { destination: ticket });
700702
}
701703

702704
let thumbnailUrl = null;
703705
if (thumbnailMedia) {
704-
thumbnailUrl = await saveMediaToFile(thumbnailMedia, ticket);
706+
thumbnailUrl = await saveMediaToFile(thumbnailMedia, {
707+
destination: ticket
708+
});
705709
}
706710

707711
const mimetype = mediaInfo?.mimetype || media?.mimetype || "";

0 commit comments

Comments
 (0)