@rexxhayanasi/elaina-baileys 1.1.7 → 1.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/Utils/messages.js +74 -13
- package/package.json +1 -1
package/lib/Utils/messages.js
CHANGED
|
@@ -101,20 +101,20 @@ const prepareWAMessageMedia = async (message, options) => {
|
|
|
101
101
|
media: message[mediaType]
|
|
102
102
|
};
|
|
103
103
|
delete uploadData[mediaType];
|
|
104
|
-
|
|
104
|
+
|
|
105
105
|
const cacheableKey = typeof uploadData.media === 'object' &&
|
|
106
106
|
('url' in uploadData.media) &&
|
|
107
107
|
!!uploadData.media.url &&
|
|
108
108
|
!!options.mediaCache && (
|
|
109
|
-
|
|
110
|
-
|
|
109
|
+
mediaType + ':' + uploadData.media.url.toString());
|
|
110
|
+
|
|
111
111
|
if (mediaType === 'document' && !uploadData.fileName) {
|
|
112
112
|
uploadData.fileName = 'file';
|
|
113
113
|
}
|
|
114
114
|
if (!uploadData.mimetype) {
|
|
115
115
|
uploadData.mimetype = MIMETYPE_MAP[mediaType];
|
|
116
116
|
}
|
|
117
|
-
|
|
117
|
+
|
|
118
118
|
if (cacheableKey) {
|
|
119
119
|
const mediaBuff = options.mediaCache.get(cacheableKey);
|
|
120
120
|
if (mediaBuff) {
|
|
@@ -125,19 +125,77 @@ const prepareWAMessageMedia = async (message, options) => {
|
|
|
125
125
|
return obj;
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
|
+
|
|
129
|
+
const isNewsletter = (0, WABinary_1.isJidNewsletter)(options.jid);
|
|
130
|
+
|
|
131
|
+
if (isNewsletter) {
|
|
132
|
+
logger === null || logger === void 0 ? void 0 : logger.debug({ key: cacheableKey }, 'Preparing raw media for newsletter');
|
|
133
|
+
|
|
134
|
+
const { bodyPath, fileSha256, fileLength, didSaveToTmpPath } = await (0, messages_media_1.prepareStream)(
|
|
135
|
+
uploadData.media,
|
|
136
|
+
options.mediaTypeOverride || mediaType,
|
|
137
|
+
{ logger, opts: options.options }
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
const fileEncSha256B64 = fileSha256.toString('base64');
|
|
141
|
+
|
|
142
|
+
const { mediaUrl, directPath } = await options.upload(bodyPath, {
|
|
143
|
+
fileEncSha256B64,
|
|
144
|
+
mediaType,
|
|
145
|
+
timeoutMs: options.mediaUploadTimeoutMs
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
if (didSaveToTmpPath && bodyPath) {
|
|
149
|
+
try {
|
|
150
|
+
await fs_1.promises.unlink(bodyPath);
|
|
151
|
+
} catch (error) {
|
|
152
|
+
logger === null || logger === void 0 ? void 0 : logger.warn('failed to remove tmp file');
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const obj = Types_1.WAProto.Message.fromObject({
|
|
157
|
+
[`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject({
|
|
158
|
+
url: mediaUrl,
|
|
159
|
+
directPath,
|
|
160
|
+
fileSha256,
|
|
161
|
+
fileLength,
|
|
162
|
+
...uploadData,
|
|
163
|
+
media: undefined
|
|
164
|
+
})
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
if (uploadData.ptv) {
|
|
168
|
+
obj.ptvMessage = obj.videoMessage;
|
|
169
|
+
delete obj.videoMessage;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (obj.stickerMessage) {
|
|
173
|
+
obj.stickerMessage.stickerSentTs = Date.now();
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (cacheableKey) {
|
|
177
|
+
logger === null || logger === void 0 ? void 0 : logger.debug({ cacheableKey }, 'set cache');
|
|
178
|
+
options.mediaCache.set(cacheableKey, Types_1.WAProto.Message.encode(obj).finish());
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return obj;
|
|
182
|
+
}
|
|
183
|
+
|
|
128
184
|
const requiresDurationComputation = mediaType === 'audio' && typeof uploadData.seconds === 'undefined';
|
|
129
185
|
const requiresThumbnailComputation = (mediaType === 'image' || mediaType === 'video') &&
|
|
130
186
|
(typeof uploadData['jpegThumbnail'] === 'undefined');
|
|
131
187
|
const requiresWaveformProcessing = mediaType === 'audio' && uploadData.ptt === true;
|
|
132
188
|
const requiresAudioBackground = options.backgroundColor && mediaType === 'audio' && uploadData.ptt === true;
|
|
133
189
|
const requiresOriginalForSomeProcessing = requiresDurationComputation || requiresThumbnailComputation;
|
|
134
|
-
|
|
190
|
+
|
|
191
|
+
const { mediaKey, encWriteStream, bodyPath, fileEncSha256, fileSha256, fileLength, didSaveToTmpPath, } = await (0, messages_media_1.encryptedStream)(uploadData.media, options.mediaTypeOverride || mediaType, {
|
|
135
192
|
logger,
|
|
136
193
|
saveOriginalFileIfRequired: requiresOriginalForSomeProcessing,
|
|
137
194
|
opts: options.options
|
|
138
195
|
});
|
|
139
|
-
|
|
140
|
-
const fileEncSha256B64 = (
|
|
196
|
+
|
|
197
|
+
const fileEncSha256B64 = (fileEncSha256 !== null && fileEncSha256 !== void 0 ? fileEncSha256 : fileSha256).toString('base64');
|
|
198
|
+
|
|
141
199
|
const [{ mediaUrl, directPath, handle }] = await Promise.all([
|
|
142
200
|
(async () => {
|
|
143
201
|
const result = await options.upload(encWriteStream, { fileEncSha256B64, mediaType, timeoutMs: options.mediaUploadTimeoutMs });
|
|
@@ -174,11 +232,10 @@ const prepareWAMessageMedia = async (message, options) => {
|
|
|
174
232
|
}
|
|
175
233
|
})(),
|
|
176
234
|
])
|
|
177
|
-
|
|
235
|
+
.finally(async () => {
|
|
178
236
|
if (!Buffer.isBuffer(encWriteStream)) {
|
|
179
237
|
encWriteStream.destroy();
|
|
180
238
|
}
|
|
181
|
-
// remove tmp files
|
|
182
239
|
if (didSaveToTmpPath && bodyPath) {
|
|
183
240
|
try {
|
|
184
241
|
await fs_1.promises.access(bodyPath);
|
|
@@ -190,6 +247,7 @@ const prepareWAMessageMedia = async (message, options) => {
|
|
|
190
247
|
}
|
|
191
248
|
}
|
|
192
249
|
});
|
|
250
|
+
|
|
193
251
|
const obj = Types_1.WAProto.Message.fromObject({
|
|
194
252
|
[`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject({
|
|
195
253
|
url: handle ? undefined : mediaUrl,
|
|
@@ -203,10 +261,13 @@ const prepareWAMessageMedia = async (message, options) => {
|
|
|
203
261
|
media: undefined
|
|
204
262
|
})
|
|
205
263
|
});
|
|
264
|
+
|
|
265
|
+
|
|
206
266
|
if (uploadData.ptv) {
|
|
207
267
|
obj.ptvMessage = obj.videoMessage;
|
|
208
268
|
delete obj.videoMessage;
|
|
209
269
|
}
|
|
270
|
+
|
|
210
271
|
if (cacheableKey) {
|
|
211
272
|
logger === null || logger === void 0 ? void 0 : logger.debug({ cacheableKey }, 'set cache');
|
|
212
273
|
options.mediaCache.set(cacheableKey, Types_1.WAProto.Message.encode(obj).finish());
|
|
@@ -398,11 +459,11 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
398
459
|
}
|
|
399
460
|
}
|
|
400
461
|
else if ('ptv' in message && message.ptv) {
|
|
401
|
-
const {
|
|
462
|
+
const { ptvMessage } = await (0, exports.prepareWAMessageMedia)({
|
|
402
463
|
video: message.ptv,
|
|
403
|
-
ptv: true
|
|
464
|
+
ptv: true
|
|
404
465
|
}, options);
|
|
405
|
-
m.ptvMessage =
|
|
466
|
+
m.ptvMessage = ptvMessage;
|
|
406
467
|
}
|
|
407
468
|
else if ('product' in message) {
|
|
408
469
|
const { imageMessage } = await (0, exports.prepareWAMessageMedia)({ image: message.product.productImage }, options);
|
|
@@ -1225,4 +1286,4 @@ const prepareStickerPackMessage = async (stickerPack, options) => {
|
|
|
1225
1286
|
|
|
1226
1287
|
return { stickerPackMessage };
|
|
1227
1288
|
};
|
|
1228
|
-
exports.prepareStickerPackMessage = prepareStickerPackMessage;
|
|
1289
|
+
exports.prepareStickerPackMessage = prepareStickerPackMessage;
|