@hbmodsofc/baileys 3.0.0 → 3.2.0

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.
@@ -67,46 +67,132 @@ const assertColor = async (color) => {
67
67
  }
68
68
  };
69
69
  const prepareWAMessageMedia = async (message, options) => {
70
- let mediaType = MEDIA_KEYS.find(key => key in message);
71
- if (!mediaType) throw new Boom('Invalid media type', { statusCode: 400 });
72
-
73
- const uploadData = { ...message, media: message[mediaType] };
70
+ const logger = options.logger;
71
+ let mediaType;
72
+ for (const key of Defaults_1.MEDIA_KEYS) {
73
+ if (key in message) {
74
+ mediaType = key;
75
+ }
76
+ }
77
+ if (!mediaType) {
78
+ throw new boom_1.Boom('Invalid media type', { statusCode: 400 });
79
+ }
80
+ const uploadData = {
81
+ ...message,
82
+ media: message[mediaType]
83
+ };
74
84
  delete uploadData[mediaType];
75
-
76
- const cacheableKey = typeof uploadData.media === 'object' && 'url' in uploadData.media && uploadData.media.url && options.mediaCache
77
- ? `${mediaType}:${uploadData.media.url.toString()}` : null;
78
-
79
- if (mediaType === 'document' && !uploadData.fileName) uploadData.fileName = 'file';
80
- if (!uploadData.mimetype) uploadData.mimetype = MIMETYPE_MAP[mediaType];
81
-
85
+ // check if cacheable + generate cache key
86
+ const cacheableKey = typeof uploadData.media === 'object' &&
87
+ ('url' in uploadData.media) &&
88
+ !!uploadData.media.url &&
89
+ !!options.mediaCache && (
90
+ // generate the key
91
+ mediaType + ':' + uploadData.media.url.toString());
92
+ if (mediaType === 'document' && !uploadData.fileName) {
93
+ uploadData.fileName = 'file';
94
+ }
95
+ if (!uploadData.mimetype) {
96
+ uploadData.mimetype = MIMETYPE_MAP[mediaType];
97
+ }
98
+ // check for cache hit
82
99
  if (cacheableKey) {
83
- const cached = await options.mediaCache?.get(cacheableKey);
84
- if (cached) {
85
- const obj = proto.Message.decode(cached);
86
- Object.assign(obj[`${mediaType}Message`], { ...uploadData, media: undefined });
100
+ const mediaBuff = options.mediaCache.get(cacheableKey);
101
+ if (mediaBuff) {
102
+ logger === null || logger === void 0 ? void 0 : logger.debug({ cacheableKey }, 'got media cache hit');
103
+ const obj = Types_1.WAProto.Message.decode(mediaBuff);
104
+ const key = `${mediaType}Message`;
105
+ Object.assign(obj[key], { ...uploadData, media: undefined });
87
106
  return obj;
88
107
  }
89
108
  }
90
-
91
- if (isJidNewsletter(options.jid)) {
92
- const { filePath, fileSha256, fileLength } = await getRawMediaUploadData(uploadData.media, options.mediaTypeOverride || mediaType, options.logger);
93
- const { mediaUrl, directPath } = await options.upload(filePath, {
94
- fileEncSha256B64: fileSha256.toString('base64'),
95
- mediaType, timeoutMs: options.mediaUploadTimeoutMs
96
- });
97
- await fs.unlink(filePath);
98
- const obj = WAProto.Message.fromObject({
99
- [`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject({
100
- url: mediaUrl, directPath, fileSha256, fileLength, ...uploadData, media: undefined
101
- })
102
- });
103
- if (uploadData.ptv) { obj.ptvMessage = obj.videoMessage; delete obj.videoMessage; }
104
- if (obj.stickerMessage) obj.stickerMessage.stickerSentTs = Date.now();
105
- if (cacheableKey) await options.mediaCache?.set(cacheableKey, WAProto.Message.encode(obj).finish());
106
- return obj;
109
+ const requiresDurationComputation = mediaType === 'audio' && typeof uploadData.seconds === 'undefined';
110
+ const requiresThumbnailComputation = (mediaType === 'image' || mediaType === 'video') &&
111
+ (typeof uploadData['jpegThumbnail'] === 'undefined');
112
+ const requiresWaveformProcessing = mediaType === 'audio' && uploadData.ptt === true;
113
+ const requiresAudioBackground = options.backgroundColor && mediaType === 'audio' && uploadData.ptt === true;
114
+ const requiresOriginalForSomeProcessing = requiresDurationComputation || requiresThumbnailComputation;
115
+ const { mediaKey, encWriteStream, bodyPath, fileEncSha256, fileSha256, fileLength, didSaveToTmpPath, } = await (options.newsletter ? messages_media_1.prepareStream : messages_media_1.encryptedStream)(uploadData.media, options.mediaTypeOverride || mediaType, {
116
+ logger,
117
+ saveOriginalFileIfRequired: requiresOriginalForSomeProcessing,
118
+ opts: options.options
119
+ });
120
+ // url safe Base64 encode the SHA256 hash of the body
121
+ const fileEncSha256B64 = (options.newsletter ? fileSha256 : fileEncSha256 !== null && fileEncSha256 !== void 0 ? fileEncSha256 : fileSha256).toString('base64');
122
+ const [{ mediaUrl, directPath, handle }] = await Promise.all([
123
+ (async () => {
124
+ const result = await options.upload(encWriteStream, { fileEncSha256B64, mediaType, timeoutMs: options.mediaUploadTimeoutMs });
125
+ logger === null || logger === void 0 ? void 0 : logger.debug({ mediaType, cacheableKey }, 'uploaded media');
126
+ return result;
127
+ })(),
128
+ (async () => {
129
+ try {
130
+ if (requiresThumbnailComputation) {
131
+ const { thumbnail, originalImageDimensions } = await (0, messages_media_1.generateThumbnail)(bodyPath, mediaType, options);
132
+ uploadData.jpegThumbnail = thumbnail;
133
+ if (!uploadData.width && originalImageDimensions) {
134
+ uploadData.width = originalImageDimensions.width;
135
+ uploadData.height = originalImageDimensions.height;
136
+ logger === null || logger === void 0 ? void 0 : logger.debug('set dimensions');
137
+ }
138
+ logger === null || logger === void 0 ? void 0 : logger.debug('generated thumbnail');
139
+ }
140
+ if (requiresDurationComputation) {
141
+ uploadData.seconds = await (0, messages_media_1.getAudioDuration)(bodyPath);
142
+ logger === null || logger === void 0 ? void 0 : logger.debug('computed audio duration');
143
+ }
144
+ if (requiresWaveformProcessing) {
145
+ uploadData.waveform = await (0, messages_media_1.getAudioWaveform)(bodyPath, logger);
146
+ logger === null || logger === void 0 ? void 0 : logger.debug('processed waveform');
147
+ }
148
+ if (requiresAudioBackground) {
149
+ uploadData.backgroundArgb = await assertColor(options.backgroundColor);
150
+ logger === null || logger === void 0 ? void 0 : logger.debug('computed backgroundColor audio status');
151
+ }
152
+ }
153
+ catch (error) {
154
+ logger === null || logger === void 0 ? void 0 : logger.warn({ trace: error.stack }, 'failed to obtain extra info');
155
+ }
156
+ })(),
157
+ ])
158
+ .finally(async () => {
159
+ if (!Buffer.isBuffer(encWriteStream)) {
160
+ encWriteStream.destroy();
161
+ }
162
+ // remove tmp files
163
+ if (didSaveToTmpPath && bodyPath) {
164
+ try {
165
+ await fs_1.promises.access(bodyPath);
166
+ await fs_1.promises.unlink(bodyPath);
167
+ logger === null || logger === void 0 ? void 0 : logger.debug('removed tmp file');
168
+ }
169
+ catch (error) {
170
+ logger === null || logger === void 0 ? void 0 : logger.warn('failed to remove tmp file');
171
+ }
172
+ }
173
+ });
174
+ const obj = Types_1.WAProto.Message.fromObject({
175
+ [`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject({
176
+ url: handle ? undefined : mediaUrl,
177
+ directPath,
178
+ mediaKey: mediaKey,
179
+ fileEncSha256: fileEncSha256,
180
+ fileSha256,
181
+ fileLength,
182
+ mediaKeyTimestamp: handle ? undefined : (0, generics_1.unixTimestampSeconds)(),
183
+ ...uploadData,
184
+ media: undefined
185
+ })
186
+ });
187
+ if (uploadData.ptv) {
188
+ obj.ptvMessage = obj.videoMessage;
189
+ delete obj.videoMessage;
107
190
  }
108
-
109
- return createMediaMessage(uploadData, mediaType, options, cacheableKey);
191
+ if (cacheableKey) {
192
+ logger === null || logger === void 0 ? void 0 : logger.debug({ cacheableKey }, 'set cache');
193
+ options.mediaCache.set(cacheableKey, Types_1.WAProto.Message.encode(obj).finish());
194
+ }
195
+ return obj;
110
196
  };
111
197
  exports.prepareWAMessageMedia = prepareWAMessageMedia;
112
198
  const prepareDisappearingMessageSettingContent = (ephemeralExpiration) => {
package/lib/index.js CHANGED
@@ -80,6 +80,5 @@ __exportStar(require("./Defaults"), exports);
80
80
  __exportStar(require("./WABinary"), exports);
81
81
  __exportStar(require("./WAM"), exports);
82
82
  __exportStar(require("./WAUSync"), exports);
83
- __exportStar(require("./Api"), exports);
84
83
 
85
84
  exports.default = Socket_1.default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hbmodsofc/baileys",
3
- "version": "3.0.0",
3
+ "version": "3.2.0",
4
4
  "description": "HBMods-OFC modified Baileys WhatsApp API for custom bots and personal projects",
5
5
  "keywords": [
6
6
  "HBWABot",