@denzy-official/baileys 1.0.9 → 1.1.2
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/WAProto/index.d.ts +55058 -0
- package/WAProto/index.js +169662 -0
- package/WAProto/index.ts.ts +53474 -0
- package/lib/Defaults/index.d.ts +52 -0
- package/lib/Signal/Group/ciphertext-message.d.ts +10 -0
- package/lib/Signal/Group/group-session-builder.d.ts +15 -0
- package/lib/Signal/Group/group_cipher.d.ts +18 -0
- package/lib/Signal/Group/index.d.ts +12 -0
- package/lib/Signal/Group/keyhelper.d.ts +11 -0
- package/lib/Signal/Group/queue-job.d.ts +2 -0
- package/lib/Signal/Group/sender-chain-key.d.ts +14 -0
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
- package/lib/Signal/Group/sender-key-message.d.ts +19 -0
- package/lib/Signal/Group/sender-key-name.d.ts +18 -0
- package/lib/Signal/Group/sender-key-record.d.ts +31 -0
- package/lib/Signal/Group/sender-key-state.d.ts +39 -0
- package/lib/Signal/Group/sender-message-key.d.ts +12 -0
- package/lib/Signal/libsignal.d.ts +4 -0
- package/lib/Socket/Client/abstract-socket-client.d.ts +17 -0
- package/lib/Socket/Client/abstract-socket-client.js +13 -0
- package/lib/Socket/Client/index.d.ts +3 -0
- package/lib/Socket/Client/index.js +3 -3
- package/lib/Socket/Client/mobile-socket-client.d.ts +13 -0
- package/lib/Socket/Client/mobile-socket-client.js +65 -0
- package/lib/Socket/Client/types.d.ts +17 -0
- package/lib/Socket/Client/web-socket-client.d.ts +12 -0
- package/lib/Socket/Client/web-socket-client.js +62 -0
- package/lib/Socket/Client/websocket.d.ts +14 -0
- package/lib/Socket/business.d.ts +171 -0
- package/lib/Socket/business.js +0 -1
- package/lib/Socket/chats.d.ts +267 -0
- package/lib/Socket/chats.js +238 -140
- package/lib/Socket/dugong.d.ts +254 -0
- package/lib/Socket/dugong.js +343 -335
- package/lib/Socket/groups.d.ts +115 -0
- package/lib/Socket/groups.js +23 -39
- package/lib/Socket/index.d.ts +173 -0
- package/lib/Socket/index.js +3 -3
- package/lib/Socket/messages-recv.d.ts +161 -0
- package/lib/Socket/messages-recv.js +65 -10
- package/lib/Socket/messages-send.d.ts +149 -0
- package/lib/Socket/messages-send.js +392 -412
- package/lib/Socket/newsletter.d.ts +134 -0
- package/lib/Socket/newsletter.js +124 -72
- package/lib/Socket/registration.d.ts +267 -0
- package/lib/Socket/registration.js +166 -0
- package/lib/Socket/socket.d.ts +43 -0
- package/lib/Socket/socket.js +38 -63
- package/lib/Socket/usync.d.ts +36 -0
- package/lib/Socket/usync.js +0 -1
- package/lib/Store/index.d.ts +3 -0
- package/lib/Store/make-in-memory-store.d.ts +119 -0
- package/lib/Store/make-ordered-dictionary.d.ts +14 -0
- package/lib/Store/object-repository.d.ts +11 -0
- package/lib/Types/Auth.d.ts +104 -0
- package/lib/Types/Call.d.ts +14 -0
- package/lib/Types/Chat.d.ts +110 -0
- package/lib/Types/Contact.d.ts +24 -0
- package/lib/Types/Events.d.ts +200 -0
- package/lib/Types/GroupMetadata.d.ts +65 -0
- package/lib/Types/Label.d.ts +36 -0
- package/lib/Types/LabelAssociation.d.ts +30 -0
- package/lib/Types/Message.d.ts +401 -0
- package/lib/Types/Newsletter.d.ts +80 -0
- package/lib/Types/Product.d.ts +78 -0
- package/lib/Types/Signal.d.ts +57 -0
- package/lib/Types/Socket.d.ts +119 -0
- package/lib/Types/State.d.ts +27 -0
- package/lib/Types/USync.d.ts +25 -0
- package/lib/Types/index.d.ts +65 -0
- package/lib/Utils/auth-utils.d.ts +18 -0
- package/lib/Utils/auth-utils.js +13 -8
- package/lib/Utils/baileys-event-stream.d.ts +16 -0
- package/lib/Utils/baileys-event-stream.js +0 -1
- package/lib/Utils/business.d.ts +22 -0
- package/lib/Utils/business.js +2 -3
- package/lib/Utils/chat-utils.d.ts +71 -0
- package/lib/Utils/chat-utils.js +35 -37
- package/lib/Utils/crypto.d.ts +41 -0
- package/lib/Utils/crypto.js +29 -72
- package/lib/Utils/decode-wa-message.d.ts +19 -0
- package/lib/Utils/decode-wa-message.js +56 -66
- package/lib/Utils/event-buffer.d.ts +35 -0
- package/lib/Utils/event-buffer.js +7 -12
- package/lib/Utils/generics.d.ts +92 -0
- package/lib/Utils/generics.js +75 -96
- package/lib/Utils/history.d.ts +15 -0
- package/lib/Utils/history.js +6 -5
- package/lib/Utils/index.d.ts +17 -0
- package/lib/Utils/index.js +0 -1
- package/lib/Utils/link-preview.d.ts +21 -0
- package/lib/Utils/link-preview.js +1 -34
- package/lib/Utils/logger.d.ts +4 -0
- package/lib/Utils/lt-hash.d.ts +12 -0
- package/lib/Utils/lt-hash.js +6 -6
- package/lib/Utils/make-mutex.d.ts +7 -0
- package/lib/Utils/messages-media.d.ts +116 -0
- package/lib/Utils/messages-media.js +235 -296
- package/lib/Utils/messages.d.ts +77 -0
- package/lib/Utils/messages.js +60 -304
- package/lib/Utils/noise-handler.d.ts +21 -0
- package/lib/Utils/noise-handler.js +23 -18
- package/lib/Utils/process-message.d.ts +41 -0
- package/lib/Utils/process-message.js +25 -89
- package/lib/Utils/signal.d.ts +32 -0
- package/lib/Utils/signal.js +26 -26
- package/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
- package/lib/Utils/use-multi-file-auth-state.js +6 -51
- package/lib/Utils/validate-connection.d.ts +11 -0
- package/lib/Utils/validate-connection.js +52 -20
- package/lib/WABinary/constants.d.ts +28 -0
- package/lib/WABinary/decode.d.ts +7 -0
- package/lib/WABinary/encode.d.ts +3 -0
- package/lib/WABinary/generic-utils.d.ts +15 -0
- package/lib/WABinary/index.d.ts +6 -0
- package/lib/WABinary/jid-utils.d.ts +37 -0
- package/lib/WABinary/types.d.ts +19 -0
- package/lib/WAM/BinaryInfo.d.ts +9 -0
- package/lib/WAM/constants.d.ts +39 -0
- package/lib/WAM/encode.d.ts +3 -0
- package/lib/WAM/index.d.ts +4 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +9 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +25 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +8 -0
- package/lib/WAUSync/Protocols/index.d.ts +4 -0
- package/lib/WAUSync/USyncQuery.d.ts +28 -0
- package/lib/WAUSync/USyncUser.d.ts +12 -0
- package/lib/WAUSync/index.d.ts +3 -0
- package/lib/index.d.ts +17 -0
- package/package.json +1 -1
|
@@ -15,47 +15,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
15
15
|
}) : function(o, v) {
|
|
16
16
|
o["default"] = v;
|
|
17
17
|
});
|
|
18
|
-
var __importStar = (this && this.__importStar) ||
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
37
24
|
};
|
|
38
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.getStatusCodeForMediaRetry = exports.decryptMediaRetryData = exports.decodeMediaRetryNode = exports.encryptMediaRetryRequest = exports.getWAUploadToServer = exports.downloadEncryptedContent = exports.downloadContentFromMessage = exports.getUrlFromDirectPath = exports.encryptedStream = exports.prepareStream = exports.getHttpStream = exports.getStream = exports.toBuffer = exports.toReadable = exports.mediaMessageSHA256B64 = exports.generateProfilePicture = exports.encodeBase64EncodedStringForUpload = exports.extractImageThumb = exports.
|
|
40
|
-
exports.getMediaKeys = getMediaKeys;
|
|
41
|
-
exports.uploadFile = uploadFile;
|
|
42
|
-
exports.vid2jpg = vid2jpg;
|
|
43
|
-
exports.getAudioDuration = getAudioDuration;
|
|
44
|
-
exports.getAudioWaveform = getAudioWaveform;
|
|
45
|
-
exports.generateThumbnail = generateThumbnail;
|
|
46
|
-
exports.extensionForMediaMessage = extensionForMediaMessage;
|
|
26
|
+
exports.getStatusCodeForMediaRetry = exports.decryptMediaRetryData = exports.decodeMediaRetryNode = exports.encryptMediaRetryRequest = exports.getWAUploadToServer = exports.extensionForMediaMessage = exports.downloadEncryptedContent = exports.downloadContentFromMessage = exports.getUrlFromDirectPath = exports.encryptedStream = exports.prepareStream = exports.getHttpStream = exports.generateThumbnail = exports.getStream = exports.toBuffer = exports.toReadable = exports.getAudioWaveform = exports.getAudioDuration = exports.mediaMessageSHA256B64 = exports.generateProfilePicture = exports.encodeBase64EncodedStringForUpload = exports.extractImageThumb = exports.getMediaKeys = exports.hkdfInfoKey = void 0;
|
|
47
27
|
const boom_1 = require("@hapi/boom");
|
|
48
|
-
const
|
|
49
|
-
const form_data_1 = __importDefault(require("form-data"));
|
|
50
|
-
const cheerio = __importStar(require("cheerio"));
|
|
28
|
+
const child_process_1 = require("child_process");
|
|
51
29
|
const Crypto = __importStar(require("crypto"));
|
|
52
30
|
const events_1 = require("events");
|
|
53
31
|
const fs_1 = require("fs");
|
|
54
32
|
const os_1 = require("os");
|
|
55
33
|
const path_1 = require("path");
|
|
56
|
-
const jimp_1 = __importDefault(require("jimp"));
|
|
57
34
|
const stream_1 = require("stream");
|
|
58
|
-
const child_process_1 = require("child_process");
|
|
59
35
|
const WAProto_1 = require("../../WAProto");
|
|
60
36
|
const Defaults_1 = require("../Defaults");
|
|
61
37
|
const WABinary_1 = require("../WABinary");
|
|
@@ -65,11 +41,13 @@ const getTmpFilesDirectory = () => (0, os_1.tmpdir)();
|
|
|
65
41
|
const getImageProcessingLibrary = async () => {
|
|
66
42
|
const [_jimp, sharp] = await Promise.all([
|
|
67
43
|
(async () => {
|
|
68
|
-
const jimp = await (
|
|
44
|
+
const jimp = await (import('jimp')
|
|
45
|
+
.catch(() => { }));
|
|
69
46
|
return jimp;
|
|
70
47
|
})(),
|
|
71
48
|
(async () => {
|
|
72
|
-
const sharp = await (
|
|
49
|
+
const sharp = await (import('sharp')
|
|
50
|
+
.catch(() => { }));
|
|
73
51
|
return sharp;
|
|
74
52
|
})()
|
|
75
53
|
]);
|
|
@@ -88,7 +66,7 @@ const hkdfInfoKey = (type) => {
|
|
|
88
66
|
};
|
|
89
67
|
exports.hkdfInfoKey = hkdfInfoKey;
|
|
90
68
|
/** generates all the keys required to encrypt/decrypt & sign a media message */
|
|
91
|
-
|
|
69
|
+
function getMediaKeys(buffer, mediaType) {
|
|
92
70
|
if (!buffer) {
|
|
93
71
|
throw new boom_1.Boom('Cannot derive from empty media key');
|
|
94
72
|
}
|
|
@@ -96,183 +74,26 @@ async function getMediaKeys(buffer, mediaType) {
|
|
|
96
74
|
buffer = Buffer.from(buffer.replace('data:;base64,', ''), 'base64');
|
|
97
75
|
}
|
|
98
76
|
// expand using HKDF to 112 bytes, also pass in the relevant app info
|
|
99
|
-
const expandedMediaKey =
|
|
77
|
+
const expandedMediaKey = (0, crypto_1.hkdf)(buffer, 112, { info: (0, exports.hkdfInfoKey)(mediaType) });
|
|
100
78
|
return {
|
|
101
79
|
iv: expandedMediaKey.slice(0, 16),
|
|
102
80
|
cipherKey: expandedMediaKey.slice(16, 48),
|
|
103
81
|
macKey: expandedMediaKey.slice(48, 80),
|
|
104
82
|
};
|
|
105
83
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
{
|
|
114
|
-
name: "catbox",
|
|
115
|
-
url: "https://catbox.moe/user/api.php",
|
|
116
|
-
buildForm: () => {
|
|
117
|
-
const form = new form_data_1.default();
|
|
118
|
-
form.append("fileToUpload", buffer, {
|
|
119
|
-
filename: `file.${ext}`,
|
|
120
|
-
contentType: mime || "application/octet-stream"
|
|
121
|
-
});
|
|
122
|
-
form.append("reqtype", "fileupload");
|
|
123
|
-
return form;
|
|
124
|
-
},
|
|
125
|
-
parseResponse: res => res.data
|
|
126
|
-
},
|
|
127
|
-
{
|
|
128
|
-
name: "pdi.moe",
|
|
129
|
-
url: "https://scdn.pdi.moe/upload",
|
|
130
|
-
buildForm: () => {
|
|
131
|
-
const form = new form_data_1.default();
|
|
132
|
-
form.append("file", buffer, {
|
|
133
|
-
filename: `file.${ext}`,
|
|
134
|
-
contentType: mime
|
|
135
|
-
});
|
|
136
|
-
return form;
|
|
137
|
-
},
|
|
138
|
-
parseResponse: res => res.data.result.url
|
|
139
|
-
},
|
|
140
|
-
{
|
|
141
|
-
name: "qu.ax",
|
|
142
|
-
url: "https://qu.ax/upload.php",
|
|
143
|
-
buildForm: () => {
|
|
144
|
-
const form = new form_data_1.default();
|
|
145
|
-
form.append("files[]", buffer, {
|
|
146
|
-
filename: `file.${ext}`,
|
|
147
|
-
contentType: mime || "application/octet-stream"
|
|
148
|
-
});
|
|
149
|
-
return form;
|
|
150
|
-
},
|
|
151
|
-
parseResponse: res => {
|
|
152
|
-
var _a, _b, _c;
|
|
153
|
-
if (!((_c = (_b = (_a = res.data) === null || _a === void 0 ? void 0 : _a.files) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.url))
|
|
154
|
-
throw new Error("Failed to get URL from qu.ax");
|
|
155
|
-
return res.data.files[0].url;
|
|
156
|
-
}
|
|
157
|
-
},
|
|
158
|
-
{
|
|
159
|
-
name: "uguu.se",
|
|
160
|
-
url: "https://uguu.se/upload.php",
|
|
161
|
-
buildForm: () => {
|
|
162
|
-
const form = new form_data_1.default();
|
|
163
|
-
form.append("files[]", buffer, {
|
|
164
|
-
filename: `file.${ext}`,
|
|
165
|
-
contentType: mime || "application/octet-stream"
|
|
166
|
-
});
|
|
167
|
-
return form;
|
|
168
|
-
},
|
|
169
|
-
parseResponse: res => {
|
|
170
|
-
var _a, _b, _c;
|
|
171
|
-
if (!((_c = (_b = (_a = res.data) === null || _a === void 0 ? void 0 : _a.files) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.url))
|
|
172
|
-
throw new Error("Failed to get URL from uguu.se");
|
|
173
|
-
return res.data.files[0].url;
|
|
174
|
-
}
|
|
175
|
-
},
|
|
176
|
-
{
|
|
177
|
-
name: "tmpfiles",
|
|
178
|
-
url: "https://tmpfiles.org/api/v1/upload",
|
|
179
|
-
buildForm: () => {
|
|
180
|
-
const form = new form_data_1.default();
|
|
181
|
-
form.append("file", buffer, {
|
|
182
|
-
filename: `file.${ext}`,
|
|
183
|
-
contentType: mime
|
|
184
|
-
});
|
|
185
|
-
return form;
|
|
186
|
-
},
|
|
187
|
-
parseResponse: res => {
|
|
188
|
-
const match = res.data.data.url.match(/https:\/\/tmpfiles\.org\/(.*)/);
|
|
189
|
-
if (!match)
|
|
190
|
-
throw new Error("Failed to parse tmpfiles URL.");
|
|
191
|
-
return `https://tmpfiles.org/dl/${match[1]}`;
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
];
|
|
195
|
-
for (const service of services) {
|
|
196
|
-
try {
|
|
197
|
-
const form = service.buildForm();
|
|
198
|
-
const res = await axios_1.default.post(service.url, form, {
|
|
199
|
-
headers: form.getHeaders()
|
|
200
|
-
});
|
|
201
|
-
const url = service.parseResponse(res);
|
|
202
|
-
return url;
|
|
203
|
-
}
|
|
204
|
-
catch (error) {
|
|
205
|
-
logger === null || logger === void 0 ? void 0 : logger.debug(`[${service.name}] eror:`, (error === null || error === void 0 ? void 0 : error.message) || error);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
throw new Error("All upload services failed.");
|
|
209
|
-
}
|
|
210
|
-
async function vid2jpg(videoUrl) {
|
|
211
|
-
try {
|
|
212
|
-
const { data } = await axios_1.default.get(`https://ezgif.com/video-to-jpg?url=${encodeURIComponent(videoUrl)}`);
|
|
213
|
-
const $ = cheerio.load(data);
|
|
214
|
-
const fileToken = $('input[name="file"]').attr("value");
|
|
215
|
-
if (!fileToken) {
|
|
216
|
-
throw new Error("Failed to retrieve file token. The video URL may be invalid or inaccessible.");
|
|
217
|
-
}
|
|
218
|
-
const formData = new URLSearchParams();
|
|
219
|
-
formData.append("file", fileToken);
|
|
220
|
-
formData.append("end", "1");
|
|
221
|
-
formData.append("video-to-jpg", "Convert to JPG!");
|
|
222
|
-
const convert = await axios_1.default.post(`https://ezgif.com/video-to-jpg/${fileToken}`, formData);
|
|
223
|
-
const $2 = cheerio.load(convert.data);
|
|
224
|
-
let imageUrl = $2("#output img").first().attr("src");
|
|
225
|
-
if (!imageUrl) {
|
|
226
|
-
throw new Error("Could not locate the converted image output.");
|
|
227
|
-
}
|
|
228
|
-
if (imageUrl.startsWith("//")) {
|
|
229
|
-
imageUrl = "https:" + imageUrl;
|
|
84
|
+
exports.getMediaKeys = getMediaKeys;
|
|
85
|
+
/** Extracts video thumb using FFMPEG */
|
|
86
|
+
const extractVideoThumb = async (path, destPath, time, size) => new Promise((resolve, reject) => {
|
|
87
|
+
const cmd = `ffmpeg -ss ${time} -i ${path} -y -vf scale=${size.width}:-1 -vframes 1 -f image2 ${destPath}`;
|
|
88
|
+
(0, child_process_1.exec)(cmd, (err) => {
|
|
89
|
+
if (err) {
|
|
90
|
+
reject(err);
|
|
230
91
|
}
|
|
231
|
-
else
|
|
232
|
-
|
|
233
|
-
if (cdnMatch) {
|
|
234
|
-
imageUrl = "https://" + imageUrl.slice(2);
|
|
235
|
-
}
|
|
236
|
-
else {
|
|
237
|
-
imageUrl = "https://ezgif.com" + imageUrl;
|
|
238
|
-
}
|
|
92
|
+
else {
|
|
93
|
+
resolve();
|
|
239
94
|
}
|
|
240
|
-
return imageUrl;
|
|
241
|
-
}
|
|
242
|
-
catch (error) {
|
|
243
|
-
throw new Error("Failed to convert video to JPG: " + error.message);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
/**
|
|
247
|
-
* Extracts video thumbnail using FFmpeg
|
|
248
|
-
*/
|
|
249
|
-
const extractVideoThumb = async (videoPath, time = '00:00:00', size = { width: 256 }) => {
|
|
250
|
-
return new Promise((resolve, reject) => {
|
|
251
|
-
const args = [
|
|
252
|
-
'-ss', time,
|
|
253
|
-
'-i', videoPath,
|
|
254
|
-
'-y',
|
|
255
|
-
'-vf', `scale=${size.width}:-1`,
|
|
256
|
-
'-vframes', '1',
|
|
257
|
-
'-f', 'image2',
|
|
258
|
-
'-vcodec', 'mjpeg',
|
|
259
|
-
'pipe:1'
|
|
260
|
-
];
|
|
261
|
-
const ffmpeg = (0, child_process_1.spawn)('ffmpeg', args);
|
|
262
|
-
const chunks = [];
|
|
263
|
-
let errorOutput = '';
|
|
264
|
-
ffmpeg.stdout.on('data', chunk => chunks.push(chunk));
|
|
265
|
-
ffmpeg.stderr.on('data', data => {
|
|
266
|
-
errorOutput += data.toString();
|
|
267
|
-
});
|
|
268
|
-
ffmpeg.on('error', reject);
|
|
269
|
-
ffmpeg.on('close', code => {
|
|
270
|
-
if (code === 0) return resolve(Buffer.concat(chunks));
|
|
271
|
-
reject(new Error(`ffmpeg exited with code ${code}\n${errorOutput}`));
|
|
272
|
-
});
|
|
273
95
|
});
|
|
274
|
-
};
|
|
275
|
-
exports.extractVideoThumb = extractVideoThumb;
|
|
96
|
+
});
|
|
276
97
|
const extractImageThumb = async (bufferOrFilePath, width = 32) => {
|
|
277
98
|
var _a, _b;
|
|
278
99
|
if (bufferOrFilePath instanceof stream_1.Readable) {
|
|
@@ -321,8 +142,8 @@ const encodeBase64EncodedStringForUpload = (b64) => (encodeURIComponent(b64
|
|
|
321
142
|
.replace(/\=+$/, '')));
|
|
322
143
|
exports.encodeBase64EncodedStringForUpload = encodeBase64EncodedStringForUpload;
|
|
323
144
|
const generateProfilePicture = async (mediaUpload) => {
|
|
145
|
+
var _a, _b;
|
|
324
146
|
let bufferOrFilePath;
|
|
325
|
-
let img;
|
|
326
147
|
if (Buffer.isBuffer(mediaUpload)) {
|
|
327
148
|
bufferOrFilePath = mediaUpload;
|
|
328
149
|
}
|
|
@@ -332,11 +153,29 @@ const generateProfilePicture = async (mediaUpload) => {
|
|
|
332
153
|
else {
|
|
333
154
|
bufferOrFilePath = await (0, exports.toBuffer)(mediaUpload.stream);
|
|
334
155
|
}
|
|
335
|
-
const
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
.
|
|
339
|
-
|
|
156
|
+
const lib = await getImageProcessingLibrary();
|
|
157
|
+
let img;
|
|
158
|
+
if ('sharp' in lib && typeof ((_a = lib.sharp) === null || _a === void 0 ? void 0 : _a.default) === 'function') {
|
|
159
|
+
img = lib.sharp.default(bufferOrFilePath)
|
|
160
|
+
.resize(640, 640)
|
|
161
|
+
.jpeg({
|
|
162
|
+
quality: 50,
|
|
163
|
+
})
|
|
164
|
+
.toBuffer();
|
|
165
|
+
}
|
|
166
|
+
else if ('jimp' in lib && typeof ((_b = lib.jimp) === null || _b === void 0 ? void 0 : _b.read) === 'function') {
|
|
167
|
+
const { read, MIME_JPEG, RESIZE_BILINEAR } = lib.jimp;
|
|
168
|
+
const jimp = await read(bufferOrFilePath);
|
|
169
|
+
const min = Math.min(jimp.getWidth(), jimp.getHeight());
|
|
170
|
+
const cropped = jimp.crop(0, 0, min, min);
|
|
171
|
+
img = cropped
|
|
172
|
+
.quality(50)
|
|
173
|
+
.resize(640, 640, RESIZE_BILINEAR)
|
|
174
|
+
.getBufferAsync(MIME_JPEG);
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
throw new boom_1.Boom('No image processing library available');
|
|
178
|
+
}
|
|
340
179
|
return {
|
|
341
180
|
img: await img,
|
|
342
181
|
};
|
|
@@ -349,59 +188,138 @@ const mediaMessageSHA256B64 = (message) => {
|
|
|
349
188
|
};
|
|
350
189
|
exports.mediaMessageSHA256B64 = mediaMessageSHA256B64;
|
|
351
190
|
async function getAudioDuration(buffer) {
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
191
|
+
try {
|
|
192
|
+
const { PassThrough } = require('stream');
|
|
193
|
+
const ff = require('fluent-ffmpeg');
|
|
194
|
+
|
|
195
|
+
return await new Promise((resolve, reject) => {
|
|
196
|
+
const inputStream = new PassThrough();
|
|
197
|
+
inputStream.end(buffer);
|
|
198
|
+
|
|
199
|
+
ff(inputStream)
|
|
200
|
+
.ffprobe((err, data) => {
|
|
201
|
+
if (err) reject(err);
|
|
202
|
+
else resolve(data.format.duration);
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
} catch (error) {
|
|
206
|
+
const musicMetadata = await import('music-metadata');
|
|
207
|
+
let metadata;
|
|
208
|
+
if (Buffer.isBuffer(buffer)) {
|
|
209
|
+
metadata = await musicMetadata.parseBuffer(buffer, undefined, {
|
|
210
|
+
duration: true
|
|
211
|
+
});
|
|
212
|
+
} else if (typeof buffer === 'string') {
|
|
213
|
+
const rStream = (0, fs_1.createReadStream)(buffer);
|
|
214
|
+
try {
|
|
215
|
+
metadata = await musicMetadata.parseStream(rStream, undefined, {
|
|
216
|
+
duration: true
|
|
217
|
+
});
|
|
218
|
+
} finally {
|
|
219
|
+
rStream.destroy();
|
|
220
|
+
}
|
|
221
|
+
} else {
|
|
222
|
+
metadata = await musicMetadata.parseStream(buffer, undefined, {
|
|
223
|
+
duration: true
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
return metadata.format.duration;
|
|
365
227
|
}
|
|
366
|
-
return metadata.format.duration;
|
|
367
228
|
}
|
|
229
|
+
exports.getAudioDuration = getAudioDuration;
|
|
368
230
|
async function getAudioWaveform(buffer, logger) {
|
|
369
231
|
try {
|
|
370
|
-
const {
|
|
371
|
-
|
|
232
|
+
const { PassThrough } = require('stream');
|
|
233
|
+
const ff = require('fluent-ffmpeg');
|
|
234
|
+
|
|
235
|
+
let audioData;
|
|
372
236
|
if (Buffer.isBuffer(buffer)) {
|
|
373
|
-
audioData = buffer
|
|
237
|
+
audioData = buffer;
|
|
374
238
|
} else if (typeof buffer === 'string') {
|
|
375
|
-
const
|
|
376
|
-
|
|
377
|
-
audioData = await exports.toBuffer(rStream)
|
|
239
|
+
const rStream = require('fs').createReadStream(buffer);
|
|
240
|
+
audioData = await exports.toBuffer(rStream);
|
|
378
241
|
} else {
|
|
379
|
-
audioData = await exports.toBuffer(buffer)
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
const audioBuffer = await decodeAudio(audioData)
|
|
383
|
-
const rawData = audioBuffer.getChannelData(0)
|
|
384
|
-
const samples = 64
|
|
385
|
-
const blockSize = Math.floor(rawData.length / samples)
|
|
386
|
-
const filteredData = []
|
|
387
|
-
|
|
388
|
-
for (let i = 0; i < samples; i++) {
|
|
389
|
-
const blockStart = blockSize * i
|
|
390
|
-
let sum = 0
|
|
391
|
-
for (let j = 0; j < blockSize; j++) {
|
|
392
|
-
sum += Math.abs(rawData[blockStart + j])
|
|
393
|
-
}
|
|
394
|
-
filteredData.push(sum / blockSize)
|
|
242
|
+
audioData = await exports.toBuffer(buffer);
|
|
395
243
|
}
|
|
396
244
|
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
245
|
+
return await new Promise((resolve, reject) => {
|
|
246
|
+
const inputStream = new PassThrough();
|
|
247
|
+
inputStream.end(audioData);
|
|
248
|
+
const chunks = [];
|
|
249
|
+
const bars = 64;
|
|
250
|
+
|
|
251
|
+
ff(inputStream)
|
|
252
|
+
.audioChannels(1)
|
|
253
|
+
.audioFrequency(16000)
|
|
254
|
+
.format('s16le')
|
|
255
|
+
.on('error', reject)
|
|
256
|
+
.on('end', () => {
|
|
257
|
+
const rawData = Buffer.concat(chunks);
|
|
258
|
+
const samples = rawData.length / 2;
|
|
259
|
+
const amplitudes = [];
|
|
260
|
+
|
|
261
|
+
for (let i = 0; i < samples; i++) {
|
|
262
|
+
amplitudes.push(Math.abs(rawData.readInt16LE(i * 2)) / 32768);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const blockSize = Math.floor(amplitudes.length / bars);
|
|
266
|
+
const avg = [];
|
|
267
|
+
for (let i = 0; i < bars; i++) {
|
|
268
|
+
const block = amplitudes.slice(i * blockSize, (i + 1) * blockSize);
|
|
269
|
+
avg.push(block.reduce((a, b) => a + b, 0) / block.length);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const max = Math.max(...avg);
|
|
273
|
+
const normalized = avg.map(v => Math.floor((v / max) * 100));
|
|
274
|
+
resolve(new Uint8Array(normalized));
|
|
275
|
+
})
|
|
276
|
+
.pipe()
|
|
277
|
+
.on('data', chunk => chunks.push(chunk));
|
|
278
|
+
});
|
|
279
|
+
} catch (e) {
|
|
280
|
+
logger?.debug(e);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
exports.getAudioWaveform = getAudioWaveform;
|
|
284
|
+
async function convertToOpusBuffer(buffer, logger) {
|
|
285
|
+
try {
|
|
286
|
+
const { PassThrough } = require('stream');
|
|
287
|
+
const ff = require('fluent-ffmpeg');
|
|
288
|
+
|
|
289
|
+
return await new Promise((resolve, reject) => {
|
|
290
|
+
const inStream = new PassThrough();
|
|
291
|
+
const outStream = new PassThrough();
|
|
292
|
+
const chunks = [];
|
|
293
|
+
inStream.end(buffer);
|
|
294
|
+
|
|
295
|
+
ff(inStream)
|
|
296
|
+
.noVideo()
|
|
297
|
+
.audioCodec('libopus')
|
|
298
|
+
.format('ogg')
|
|
299
|
+
.audioBitrate('48k')
|
|
300
|
+
.audioChannels(1)
|
|
301
|
+
.audioFrequency(48000)
|
|
302
|
+
.outputOptions([
|
|
303
|
+
'-vn',
|
|
304
|
+
'-b:a 64k',
|
|
305
|
+
'-ac 2',
|
|
306
|
+
'-ar 48000',
|
|
307
|
+
'-map_metadata', '-1',
|
|
308
|
+
'-application', 'voip'
|
|
309
|
+
])
|
|
310
|
+
.on('error', reject)
|
|
311
|
+
.on('end', () => resolve(Buffer.concat(chunks)))
|
|
312
|
+
.pipe(outStream, {
|
|
313
|
+
end: true
|
|
314
|
+
});
|
|
315
|
+
outStream.on('data', c => chunks.push(c));
|
|
316
|
+
});
|
|
401
317
|
} catch (e) {
|
|
402
|
-
logger?.debug(
|
|
318
|
+
logger?.debug(e);
|
|
319
|
+
throw e;
|
|
403
320
|
}
|
|
404
321
|
}
|
|
322
|
+
exports.convertToOpusBuffer = convertToOpusBuffer;
|
|
405
323
|
const toReadable = (buffer) => {
|
|
406
324
|
const readable = new stream_1.Readable({ read: () => { } });
|
|
407
325
|
readable.push(buffer);
|
|
@@ -447,28 +365,12 @@ async function generateThumbnail(file, mediaType, options) {
|
|
|
447
365
|
}
|
|
448
366
|
}
|
|
449
367
|
else if (mediaType === 'video') {
|
|
368
|
+
const imgFilename = (0, path_1.join)(getTmpFilesDirectory(), (0, generics_1.generateMessageID)() + '.jpg');
|
|
450
369
|
try {
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
const buffer = Buffer.isBuffer(file) ? file : await (0, exports.toBuffer)(file);
|
|
455
|
-
await fs_1.promises.writeFile(videoPath, buffer);
|
|
456
|
-
}
|
|
457
|
-
const thumbnailBuffer = await (0, exports.extractVideoThumb)(videoPath);
|
|
458
|
-
const imgFilename = (0, path_1.join)(getTmpFilesDirectory(), (0, generics_1.generateMessageIDV2)() + '.jpg');
|
|
459
|
-
await fs_1.promises.writeFile(imgFilename, thumbnailBuffer);
|
|
460
|
-
const { buffer: processedThumbnailBuffer, original } = await (0, exports.extractImageThumb)(imgFilename);
|
|
461
|
-
thumbnail = processedThumbnailBuffer.toString('base64');
|
|
462
|
-
if (original.width && original.height) {
|
|
463
|
-
originalImageDimensions = {
|
|
464
|
-
width: original.width,
|
|
465
|
-
height: original.height,
|
|
466
|
-
};
|
|
467
|
-
}
|
|
370
|
+
await extractVideoThumb(file, imgFilename, '00:00:00', { width: 32, height: 32 });
|
|
371
|
+
const buff = await fs_1.promises.readFile(imgFilename);
|
|
372
|
+
thumbnail = buff.toString('base64');
|
|
468
373
|
await fs_1.promises.unlink(imgFilename);
|
|
469
|
-
if (videoPath !== file) {
|
|
470
|
-
await fs_1.promises.unlink(videoPath);
|
|
471
|
-
}
|
|
472
374
|
}
|
|
473
375
|
catch (err) {
|
|
474
376
|
(_a = options.logger) === null || _a === void 0 ? void 0 : _a.debug('could not generate video thumb: ' + err);
|
|
@@ -479,8 +381,10 @@ async function generateThumbnail(file, mediaType, options) {
|
|
|
479
381
|
originalImageDimensions
|
|
480
382
|
};
|
|
481
383
|
}
|
|
384
|
+
exports.generateThumbnail = generateThumbnail;
|
|
482
385
|
const getHttpStream = async (url, options = {}) => {
|
|
483
|
-
const
|
|
386
|
+
const { default: axios } = await import('axios');
|
|
387
|
+
const fetched = await axios.get(url.toString(), { ...options, responseType: 'stream' });
|
|
484
388
|
return fetched.data;
|
|
485
389
|
};
|
|
486
390
|
exports.getHttpStream = getHttpStream;
|
|
@@ -495,7 +399,7 @@ const prepareStream = async (media, mediaType, { logger, saveOriginalFileIfRequi
|
|
|
495
399
|
bodyPath = media.url;
|
|
496
400
|
}
|
|
497
401
|
else if (saveOriginalFileIfRequired) {
|
|
498
|
-
bodyPath = (0, path_1.join)(getTmpFilesDirectory(), mediaType + (0, generics_1.
|
|
402
|
+
bodyPath = (0, path_1.join)(getTmpFilesDirectory(), mediaType + (0, generics_1.generateMessageID)());
|
|
499
403
|
(0, fs_1.writeFileSync)(bodyPath, buffer);
|
|
500
404
|
didSaveToTmpPath = true;
|
|
501
405
|
}
|
|
@@ -514,6 +418,7 @@ const prepareStream = async (media, mediaType, { logger, saveOriginalFileIfRequi
|
|
|
514
418
|
};
|
|
515
419
|
}
|
|
516
420
|
catch (error) {
|
|
421
|
+
// destroy all streams with error
|
|
517
422
|
stream.destroy();
|
|
518
423
|
if (didSaveToTmpPath) {
|
|
519
424
|
try {
|
|
@@ -527,30 +432,45 @@ const prepareStream = async (media, mediaType, { logger, saveOriginalFileIfRequi
|
|
|
527
432
|
}
|
|
528
433
|
};
|
|
529
434
|
exports.prepareStream = prepareStream;
|
|
530
|
-
const encryptedStream = async (media, mediaType, { logger, saveOriginalFileIfRequired, opts } = {}) => {
|
|
435
|
+
const encryptedStream = async (media, mediaType, { logger, saveOriginalFileIfRequired, opts, isPtt, forceOpus } = {}) => {
|
|
531
436
|
const { stream, type } = await (0, exports.getStream)(media, opts);
|
|
532
|
-
|
|
437
|
+
|
|
438
|
+
let finalStream = stream;
|
|
439
|
+
if (mediaType === 'audio' && (isPtt === true || forceOpus === true)) {
|
|
440
|
+
try {
|
|
441
|
+
const buffer = await (0, exports.toBuffer)(stream);
|
|
442
|
+
const opusBuffer = await exports.convertToOpusBuffer(buffer, logger);
|
|
443
|
+
finalStream = (0, exports.toReadable)(opusBuffer);
|
|
444
|
+
} catch (error) {
|
|
445
|
+
const { stream: newStream } = await (0, exports.getStream)(media, opts);
|
|
446
|
+
finalStream = newStream;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
533
450
|
const mediaKey = Crypto.randomBytes(32);
|
|
534
|
-
const { cipherKey, iv, macKey } =
|
|
451
|
+
const { cipherKey, iv, macKey } = getMediaKeys(mediaKey, mediaType);
|
|
535
452
|
const encWriteStream = new stream_1.Readable({ read: () => { } });
|
|
536
453
|
let bodyPath;
|
|
537
454
|
let writeStream;
|
|
538
455
|
let didSaveToTmpPath = false;
|
|
456
|
+
|
|
539
457
|
if (type === 'file') {
|
|
540
458
|
bodyPath = media.url;
|
|
541
459
|
}
|
|
542
460
|
else if (saveOriginalFileIfRequired) {
|
|
543
|
-
bodyPath = (0, path_1.join)(getTmpFilesDirectory(), mediaType + (0, generics_1.
|
|
461
|
+
bodyPath = (0, path_1.join)(getTmpFilesDirectory(), mediaType + (0, generics_1.generateMessageID)());
|
|
544
462
|
writeStream = (0, fs_1.createWriteStream)(bodyPath);
|
|
545
463
|
didSaveToTmpPath = true;
|
|
546
464
|
}
|
|
465
|
+
|
|
547
466
|
let fileLength = 0;
|
|
548
467
|
const aes = Crypto.createCipheriv('aes-256-cbc', cipherKey, iv);
|
|
549
468
|
let hmac = Crypto.createHmac('sha256', macKey).update(iv);
|
|
550
469
|
let sha256Plain = Crypto.createHash('sha256');
|
|
551
470
|
let sha256Enc = Crypto.createHash('sha256');
|
|
471
|
+
|
|
552
472
|
try {
|
|
553
|
-
for await (const data of
|
|
473
|
+
for await (const data of finalStream) {
|
|
554
474
|
fileLength += data.length;
|
|
555
475
|
if (type === 'remote'
|
|
556
476
|
&& (opts === null || opts === void 0 ? void 0 : opts.maxContentLength)
|
|
@@ -559,6 +479,7 @@ const encryptedStream = async (media, mediaType, { logger, saveOriginalFileIfReq
|
|
|
559
479
|
data: { media, type }
|
|
560
480
|
});
|
|
561
481
|
}
|
|
482
|
+
|
|
562
483
|
sha256Plain = sha256Plain.update(data);
|
|
563
484
|
if (writeStream) {
|
|
564
485
|
if (!writeStream.write(data)) {
|
|
@@ -567,16 +488,18 @@ const encryptedStream = async (media, mediaType, { logger, saveOriginalFileIfReq
|
|
|
567
488
|
}
|
|
568
489
|
onChunk(aes.update(data));
|
|
569
490
|
}
|
|
491
|
+
|
|
570
492
|
onChunk(aes.final());
|
|
571
493
|
const mac = hmac.digest().slice(0, 10);
|
|
572
494
|
sha256Enc = sha256Enc.update(mac);
|
|
573
495
|
const fileSha256 = sha256Plain.digest();
|
|
574
496
|
const fileEncSha256 = sha256Enc.digest();
|
|
497
|
+
|
|
575
498
|
encWriteStream.push(mac);
|
|
576
499
|
encWriteStream.push(null);
|
|
577
500
|
writeStream === null || writeStream === void 0 ? void 0 : writeStream.end();
|
|
578
|
-
|
|
579
|
-
|
|
501
|
+
finalStream.destroy();
|
|
502
|
+
|
|
580
503
|
return {
|
|
581
504
|
mediaKey,
|
|
582
505
|
encWriteStream,
|
|
@@ -595,17 +518,18 @@ const encryptedStream = async (media, mediaType, { logger, saveOriginalFileIfReq
|
|
|
595
518
|
hmac.destroy();
|
|
596
519
|
sha256Plain.destroy();
|
|
597
520
|
sha256Enc.destroy();
|
|
598
|
-
|
|
521
|
+
finalStream.destroy();
|
|
522
|
+
|
|
599
523
|
if (didSaveToTmpPath) {
|
|
600
524
|
try {
|
|
601
525
|
await fs_1.promises.unlink(bodyPath);
|
|
602
526
|
}
|
|
603
527
|
catch (err) {
|
|
604
|
-
logger === null || logger === void 0 ? void 0 : logger.error({ err }, 'failed to save to tmp path');
|
|
605
528
|
}
|
|
606
529
|
}
|
|
607
530
|
throw error;
|
|
608
531
|
}
|
|
532
|
+
|
|
609
533
|
function onChunk(buff) {
|
|
610
534
|
sha256Enc = sha256Enc.update(buff);
|
|
611
535
|
hmac = hmac.update(buff);
|
|
@@ -620,20 +544,21 @@ const toSmallestChunkSize = (num) => {
|
|
|
620
544
|
};
|
|
621
545
|
const getUrlFromDirectPath = (directPath) => `https://${DEF_HOST}${directPath}`;
|
|
622
546
|
exports.getUrlFromDirectPath = getUrlFromDirectPath;
|
|
623
|
-
const downloadContentFromMessage =
|
|
624
|
-
const
|
|
625
|
-
const
|
|
626
|
-
if (!downloadUrl) {
|
|
627
|
-
throw new boom_1.Boom('No valid media URL or directPath present in message', { statusCode: 400 });
|
|
628
|
-
}
|
|
629
|
-
const keys = await getMediaKeys(mediaKey, type);
|
|
547
|
+
const downloadContentFromMessage = ({ mediaKey, directPath, url }, type, opts = {}) => {
|
|
548
|
+
const downloadUrl = url || (0, exports.getUrlFromDirectPath)(directPath);
|
|
549
|
+
const keys = getMediaKeys(mediaKey, type);
|
|
630
550
|
return (0, exports.downloadEncryptedContent)(downloadUrl, keys, opts);
|
|
631
551
|
};
|
|
632
552
|
exports.downloadContentFromMessage = downloadContentFromMessage;
|
|
553
|
+
/**
|
|
554
|
+
* Decrypts and downloads an AES256-CBC encrypted file given the keys.
|
|
555
|
+
* Assumes the SHA256 of the plaintext is appended to the end of the ciphertext
|
|
556
|
+
* */
|
|
633
557
|
const downloadEncryptedContent = async (downloadUrl, { cipherKey, iv }, { startByte, endByte, options } = {}) => {
|
|
634
558
|
let bytesFetched = 0;
|
|
635
559
|
let startChunk = 0;
|
|
636
560
|
let firstBlockIsIV = false;
|
|
561
|
+
// if a start byte is specified -- then we need to fetch the previous chunk as that will form the IV
|
|
637
562
|
if (startByte) {
|
|
638
563
|
const chunk = toSmallestChunkSize(startByte || 0);
|
|
639
564
|
if (chunk) {
|
|
@@ -653,6 +578,7 @@ const downloadEncryptedContent = async (downloadUrl, { cipherKey, iv }, { startB
|
|
|
653
578
|
headers.Range += endChunk;
|
|
654
579
|
}
|
|
655
580
|
}
|
|
581
|
+
// download the message
|
|
656
582
|
const fetched = await (0, exports.getHttpStream)(downloadUrl, {
|
|
657
583
|
...options || {},
|
|
658
584
|
headers,
|
|
@@ -685,6 +611,8 @@ const downloadEncryptedContent = async (downloadUrl, { cipherKey, iv }, { startB
|
|
|
685
611
|
data = data.slice(AES_CHUNK_SIZE);
|
|
686
612
|
}
|
|
687
613
|
aes = Crypto.createDecipheriv('aes-256-cbc', cipherKey, ivValue);
|
|
614
|
+
// if an end byte that is not EOF is specified
|
|
615
|
+
// stop auto padding (PKCS7) -- otherwise throws an error for decryption
|
|
688
616
|
if (endByte) {
|
|
689
617
|
aes.setAutoPadding(false);
|
|
690
618
|
}
|
|
@@ -725,9 +653,12 @@ function extensionForMediaMessage(message) {
|
|
|
725
653
|
}
|
|
726
654
|
return extension;
|
|
727
655
|
}
|
|
656
|
+
exports.extensionForMediaMessage = extensionForMediaMessage;
|
|
728
657
|
const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, options }, refreshMediaConn) => {
|
|
729
658
|
return async (stream, { mediaType, fileEncSha256B64, newsletter, timeoutMs }) => {
|
|
730
659
|
var _a, _b;
|
|
660
|
+
const { default: axios } = await import('axios');
|
|
661
|
+
// send a query JSON to obtain the url & auth token to upload our media
|
|
731
662
|
let uploadInfo = await refreshMediaConn(false);
|
|
732
663
|
let urls;
|
|
733
664
|
const hosts = [...customUploadHosts, ...uploadInfo.hosts];
|
|
@@ -745,14 +676,14 @@ const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, options },
|
|
|
745
676
|
}
|
|
746
677
|
for (const { hostname, maxContentLengthBytes } of hosts) {
|
|
747
678
|
logger.debug(`uploading to "${hostname}"`);
|
|
748
|
-
const auth = encodeURIComponent(uploadInfo.auth);
|
|
679
|
+
const auth = encodeURIComponent(uploadInfo.auth); // the auth token
|
|
749
680
|
const url = `https://${hostname}${media}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`;
|
|
750
681
|
let result;
|
|
751
682
|
try {
|
|
752
683
|
if (maxContentLengthBytes && reqBody.length > maxContentLengthBytes) {
|
|
753
684
|
throw new boom_1.Boom(`Body too large for "${hostname}"`, { statusCode: 413 });
|
|
754
685
|
}
|
|
755
|
-
const body = await
|
|
686
|
+
const body = await axios.post(url, reqBody, {
|
|
756
687
|
...options,
|
|
757
688
|
headers: {
|
|
758
689
|
...options.headers || {},
|
|
@@ -780,7 +711,7 @@ const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, options },
|
|
|
780
711
|
}
|
|
781
712
|
}
|
|
782
713
|
catch (error) {
|
|
783
|
-
if (
|
|
714
|
+
if (axios.isAxiosError(error)) {
|
|
784
715
|
result = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data;
|
|
785
716
|
}
|
|
786
717
|
const isLast = hostname === ((_b = hosts[uploadInfo.hosts.length - 1]) === null || _b === void 0 ? void 0 : _b.hostname);
|
|
@@ -797,11 +728,14 @@ exports.getWAUploadToServer = getWAUploadToServer;
|
|
|
797
728
|
const getMediaRetryKey = (mediaKey) => {
|
|
798
729
|
return (0, crypto_1.hkdf)(mediaKey, 32, { info: 'WhatsApp Media Retry Notification' });
|
|
799
730
|
};
|
|
800
|
-
|
|
731
|
+
/**
|
|
732
|
+
* Generate a binary node that will request the phone to re-upload the media & return the newly uploaded URL
|
|
733
|
+
*/
|
|
734
|
+
const encryptMediaRetryRequest = (key, mediaKey, meId) => {
|
|
801
735
|
const recp = { stanzaId: key.id };
|
|
802
736
|
const recpBuffer = WAProto_1.proto.ServerErrorReceipt.encode(recp).finish();
|
|
803
737
|
const iv = Crypto.randomBytes(12);
|
|
804
|
-
const retryKey =
|
|
738
|
+
const retryKey = getMediaRetryKey(mediaKey);
|
|
805
739
|
const ciphertext = (0, crypto_1.aesEncryptGCM)(recpBuffer, retryKey, iv, Buffer.from(key.id));
|
|
806
740
|
const req = {
|
|
807
741
|
tag: 'receipt',
|
|
@@ -811,6 +745,9 @@ const encryptMediaRetryRequest = async (key, mediaKey, meId) => {
|
|
|
811
745
|
type: 'server-error'
|
|
812
746
|
},
|
|
813
747
|
content: [
|
|
748
|
+
// this encrypt node is actually pretty useless
|
|
749
|
+
// the media is returned even without this node
|
|
750
|
+
// keeping it here to maintain parity with WA Web
|
|
814
751
|
{
|
|
815
752
|
tag: 'encrypt',
|
|
816
753
|
attrs: {},
|
|
@@ -824,6 +761,7 @@ const encryptMediaRetryRequest = async (key, mediaKey, meId) => {
|
|
|
824
761
|
attrs: {
|
|
825
762
|
jid: key.remoteJid,
|
|
826
763
|
'from_me': (!!key.fromMe).toString(),
|
|
764
|
+
// @ts-ignore
|
|
827
765
|
participant: key.participant || undefined
|
|
828
766
|
}
|
|
829
767
|
}
|
|
@@ -861,8 +799,8 @@ const decodeMediaRetryNode = (node) => {
|
|
|
861
799
|
return event;
|
|
862
800
|
};
|
|
863
801
|
exports.decodeMediaRetryNode = decodeMediaRetryNode;
|
|
864
|
-
const decryptMediaRetryData =
|
|
865
|
-
const retryKey =
|
|
802
|
+
const decryptMediaRetryData = ({ ciphertext, iv }, mediaKey, msgId) => {
|
|
803
|
+
const retryKey = getMediaRetryKey(mediaKey);
|
|
866
804
|
const plaintext = (0, crypto_1.aesDecryptGCM)(ciphertext, retryKey, iv, Buffer.from(msgId));
|
|
867
805
|
return WAProto_1.proto.MediaRetryNotification.decode(plaintext);
|
|
868
806
|
};
|
|
@@ -875,6 +813,7 @@ const MEDIA_RETRY_STATUS_MAP = {
|
|
|
875
813
|
[WAProto_1.proto.MediaRetryNotification.ResultType.NOT_FOUND]: 404,
|
|
876
814
|
[WAProto_1.proto.MediaRetryNotification.ResultType.GENERAL_ERROR]: 418,
|
|
877
815
|
};
|
|
816
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
878
817
|
function __importStar(arg0) {
|
|
879
818
|
throw new Error('Function not implemented.');
|
|
880
|
-
}
|
|
819
|
+
}
|