JOHN SILLVESTOR commited on
Commit
2987850
·
verified ·
1 Parent(s): 76c8123

Update Node HTTP service

Browse files
Files changed (1) hide show
  1. server.js +59 -17
server.js CHANGED
@@ -14,7 +14,7 @@ const STREAM_SECRET = process.env.STREAM_SECRET || "";
14
  const ROLE = (process.env.NODE_ROLE || "stream").toLowerCase();
15
  const HASH_LEN = 6;
16
  const TOKEN_TTL = 900;
17
- const CHUNK = 512 * 1024;
18
  const MIN_ALIGN = 4096;
19
  const BOT_MAX = 10;
20
  const FB_PROJECT = process.env.FIREBASE_PROJECT_ID || "";
@@ -256,9 +256,11 @@ async function fileFromMsg(bot, msgId) {
256
  const doc = msg.video || msg.document;
257
  if (!doc) throw new Error("media");
258
  let name = "video.mp4";
 
259
  const attrs = doc.attributes || [];
260
  for (const a of attrs) {
261
  if (a && a.className === "DocumentAttributeFilename" && a.fileName) name = a.fileName;
 
262
  }
263
  const loc = new Api.InputDocumentFileLocation({
264
  id: doc.id,
@@ -274,6 +276,7 @@ async function fileFromMsg(bot, msgId) {
274
  id: String(doc.id),
275
  hash: packHash(name, Number(doc.size || 0), doc.mimeType || "video/mp4", String(doc.id)),
276
  dcId: doc.dcId,
 
277
  doc,
278
  ts: Date.now()
279
  };
@@ -281,12 +284,54 @@ async function fileFromMsg(bot, msgId) {
281
  return rec;
282
  }
283
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
284
  function parseRange(h, size) {
285
  if (!h || !size) return { start: 0, end: size - 1, partial: false };
286
  const m = /^bytes=(\d*)-(\d*)$/.exec(String(h).trim());
287
  if (!m) return { start: 0, end: size - 1, partial: false };
288
- let start = m[1] === "" ? size - Number(m[2] || 0) : Number(m[1]);
289
- let end = m[2] === "" ? size - 1 : Number(m[2]);
 
 
 
 
 
 
 
 
290
  if (!Number.isFinite(start) || start < 0) start = 0;
291
  if (!Number.isFinite(end) || end >= size) end = size - 1;
292
  if (start > end) start = 0;
@@ -298,10 +343,15 @@ async function* rangeChunks(bot, rec, start, end) {
298
  let skip = start - aligned;
299
  let sent = 0;
300
  const need = end - start + 1;
 
 
 
 
 
301
  const it = bot.client.iterDownload(rec.doc || rec.loc, {
302
  offset: aligned,
303
- limit: skip + need,
304
- requestSize: CHUNK,
305
  dcId: rec.dcId
306
  });
307
  for await (const chunk of it) {
@@ -326,22 +376,12 @@ async function* rangeChunks(bot, rec, start, end) {
326
  async function pipeRange(bot, rec, start, end, res, status, headers) {
327
  bot.load++;
328
  try {
329
- const gen = rangeChunks(bot, rec, start, end);
330
- const first = await gen.next();
331
- if (first.done || !first.value || !first.value.length) {
332
- if (!res.headersSent) {
333
- res.writeHead(502);
334
- res.end();
335
- }
336
- return;
337
- }
338
  res.writeHead(status, headers);
339
  if (typeof res.flushHeaders === "function") res.flushHeaders();
340
- let ok = res.write(first.value);
341
- if (!ok) await new Promise((r) => res.once("drain", r));
342
  for await (const buf of gen) {
343
  if (res.destroyed || res.writableEnded) break;
344
- ok = res.write(buf);
345
  if (!ok) await new Promise((r) => res.once("drain", r));
346
  }
347
  } finally {
@@ -375,6 +415,7 @@ async function handleGenerate(req, res, url) {
375
  let rec;
376
  try {
377
  rec = await fileFromMsg(bot, msgId);
 
378
  } catch (e) {
379
  dropFile(msgId, bot);
380
  return json(res, 400, { error: "file" });
@@ -388,6 +429,7 @@ async function handleGenerate(req, res, url) {
388
  fileSize: rec.size,
389
  mime: playMime,
390
  name: rec.name,
 
391
  accessLevel: "subscribed",
392
  expiry: exp
393
  });
 
14
  const ROLE = (process.env.NODE_ROLE || "stream").toLowerCase();
15
  const HASH_LEN = 6;
16
  const TOKEN_TTL = 900;
17
+ const CHUNK = 64 * 1024;
18
  const MIN_ALIGN = 4096;
19
  const BOT_MAX = 10;
20
  const FB_PROJECT = process.env.FIREBASE_PROJECT_ID || "";
 
256
  const doc = msg.video || msg.document;
257
  if (!doc) throw new Error("media");
258
  let name = "video.mp4";
259
+ let duration = 0;
260
  const attrs = doc.attributes || [];
261
  for (const a of attrs) {
262
  if (a && a.className === "DocumentAttributeFilename" && a.fileName) name = a.fileName;
263
+ if (a && Number(a.duration) > 0) duration = Number(a.duration);
264
  }
265
  const loc = new Api.InputDocumentFileLocation({
266
  id: doc.id,
 
276
  id: String(doc.id),
277
  hash: packHash(name, Number(doc.size || 0), doc.mimeType || "video/mp4", String(doc.id)),
278
  dcId: doc.dcId,
279
+ duration,
280
  doc,
281
  ts: Date.now()
282
  };
 
284
  return rec;
285
  }
286
 
287
+ function durationFromMp4(buf) {
288
+ const i = buf.indexOf(Buffer.from("mvhd"));
289
+ if (i < 4 || i + 24 > buf.length) return 0;
290
+ const ver = buf[i + 4];
291
+ try {
292
+ if (ver === 1) {
293
+ if (i + 36 > buf.length) return 0;
294
+ const timescale = buf.readUInt32BE(i + 24);
295
+ const duration = Number(buf.readBigUInt64BE(i + 28));
296
+ return timescale > 0 ? duration / timescale : 0;
297
+ }
298
+ const timescale = buf.readUInt32BE(i + 16);
299
+ const duration = buf.readUInt32BE(i + 20);
300
+ return timescale > 0 ? duration / timescale : 0;
301
+ } catch {
302
+ return 0;
303
+ }
304
+ }
305
+
306
+ async function fillDuration(bot, rec) {
307
+ if (rec.duration > 0 || !rec.size) return;
308
+ const n = Math.min(rec.size, 1024 * 1024);
309
+ const start = rec.size - n;
310
+ let acc = Buffer.alloc(0);
311
+ try {
312
+ for await (const c of rangeChunks(bot, rec, start, rec.size - 1)) {
313
+ acc = Buffer.concat([acc, c]);
314
+ rec.duration = durationFromMp4(acc);
315
+ if (rec.duration > 0) return;
316
+ if (acc.length >= n) return;
317
+ }
318
+ } catch {}
319
+ }
320
+
321
  function parseRange(h, size) {
322
  if (!h || !size) return { start: 0, end: size - 1, partial: false };
323
  const m = /^bytes=(\d*)-(\d*)$/.exec(String(h).trim());
324
  if (!m) return { start: 0, end: size - 1, partial: false };
325
+ let start;
326
+ let end;
327
+ if (m[1] === "") {
328
+ const n = Number(m[2] || 0);
329
+ start = Math.max(0, size - n);
330
+ end = size - 1;
331
+ } else {
332
+ start = Number(m[1]);
333
+ end = m[2] === "" ? size - 1 : Number(m[2]);
334
+ }
335
  if (!Number.isFinite(start) || start < 0) start = 0;
336
  if (!Number.isFinite(end) || end >= size) end = size - 1;
337
  if (start > end) start = 0;
 
343
  let skip = start - aligned;
344
  let sent = 0;
345
  const need = end - start + 1;
346
+ let reqSize = CHUNK;
347
+ const want = skip + need;
348
+ if (want < CHUNK) {
349
+ reqSize = Math.max(MIN_ALIGN, Math.ceil(want / MIN_ALIGN) * MIN_ALIGN);
350
+ }
351
  const it = bot.client.iterDownload(rec.doc || rec.loc, {
352
  offset: aligned,
353
+ limit: want,
354
+ requestSize: reqSize,
355
  dcId: rec.dcId
356
  });
357
  for await (const chunk of it) {
 
376
  async function pipeRange(bot, rec, start, end, res, status, headers) {
377
  bot.load++;
378
  try {
 
 
 
 
 
 
 
 
 
379
  res.writeHead(status, headers);
380
  if (typeof res.flushHeaders === "function") res.flushHeaders();
381
+ const gen = rangeChunks(bot, rec, start, end);
 
382
  for await (const buf of gen) {
383
  if (res.destroyed || res.writableEnded) break;
384
+ const ok = res.write(buf);
385
  if (!ok) await new Promise((r) => res.once("drain", r));
386
  }
387
  } finally {
 
415
  let rec;
416
  try {
417
  rec = await fileFromMsg(bot, msgId);
418
+ await fillDuration(bot, rec);
419
  } catch (e) {
420
  dropFile(msgId, bot);
421
  return json(res, 400, { error: "file" });
 
429
  fileSize: rec.size,
430
  mime: playMime,
431
  name: rec.name,
432
+ duration: rec.duration || 0,
433
  accessLevel: "subscribed",
434
  expiry: exp
435
  });