JOHN SILLVESTOR commited on
Commit
5088731
·
verified ·
1 Parent(s): 4b92fd9

Update Node HTTP service

Browse files
Files changed (1) hide show
  1. server.js +33 -18
server.js CHANGED
@@ -483,27 +483,39 @@ async function fileFromPool(msgId, prefer, skipIdx) {
483
  }
484
 
485
  function durationFromMp4(buf) {
486
- const i = buf.indexOf(Buffer.from("mvhd"));
487
- if (i < 4 || i + 24 > buf.length) return 0;
488
- const ver = buf[i + 4];
489
- try {
490
- if (ver === 1) {
491
- if (i + 36 > buf.length) return 0;
492
- const timescale = buf.readUInt32BE(i + 24);
493
- const duration = Number(buf.readBigUInt64BE(i + 28));
494
- return timescale > 0 ? duration / timescale : 0;
495
- }
496
- const timescale = buf.readUInt32BE(i + 16);
497
- const duration = buf.readUInt32BE(i + 20);
498
- return timescale > 0 ? duration / timescale : 0;
499
- } catch {
500
- return 0;
 
 
 
 
 
 
 
 
 
 
 
501
  }
 
502
  }
503
 
504
  async function peekDuration(bot, rec) {
505
  if (!bot || !rec || !rec.size) return 0;
506
- const take = Math.min(rec.size, 262144);
507
  async function readSpan(start, end) {
508
  let acc = Buffer.alloc(0);
509
  for await (const c of rangeChunks(bot, rec, start, end, () => acc.length >= take)) {
@@ -700,7 +712,11 @@ async function handleGenerate(req, res, url) {
700
  const rec = got.rec;
701
  if (rec.hash && rec.hash !== hash) return json(res, 400, { error: "hash" });
702
  const cachedDur = durCache.get(String(msgId));
703
- if (cachedDur) rec.duration = cachedDur;
 
 
 
 
704
  const playMime = httpVideoType(rec.mime, rec.name);
705
  const exp = Math.floor(Date.now() / 1000) + TOKEN_TTL;
706
  const k = sealTicket({ m: msgId, h: hash, u: uid || "anon", l: lvl, b: bot.idx, e: exp, s: rec.size, n: playMime });
@@ -714,7 +730,6 @@ async function handleGenerate(req, res, url) {
714
  accessLevel: lvl,
715
  expiry: exp
716
  });
717
- if (!rec.duration) fillDuration(bot, rec, msgId).catch(function () {});
718
  }
719
 
720
  async function handleStream(req, res, url, msgId) {
 
483
  }
484
 
485
  function durationFromMp4(buf) {
486
+ if (!buf || buf.length < 32) return 0;
487
+ const tag = Buffer.from("mvhd");
488
+ let from = 0;
489
+ while (from + 28 < buf.length) {
490
+ const i = buf.indexOf(tag, from);
491
+ if (i < 4 || i + 24 > buf.length) break;
492
+ const ver = buf[i + 4];
493
+ try {
494
+ if (ver === 1) {
495
+ if (i + 36 > buf.length) break;
496
+ const timescale = buf.readUInt32BE(i + 24);
497
+ const duration = Number(buf.readBigUInt64BE(i + 28));
498
+ if (timescale > 0 && duration > 0) {
499
+ const s = duration / timescale;
500
+ if (s > 1 && s < 86400) return s;
501
+ }
502
+ } else if (ver === 0) {
503
+ const timescale = buf.readUInt32BE(i + 16);
504
+ const duration = buf.readUInt32BE(i + 20);
505
+ if (timescale > 0 && duration > 0) {
506
+ const s = duration / timescale;
507
+ if (s > 1 && s < 86400) return s;
508
+ }
509
+ }
510
+ } catch {}
511
+ from = i + 4;
512
  }
513
+ return 0;
514
  }
515
 
516
  async function peekDuration(bot, rec) {
517
  if (!bot || !rec || !rec.size) return 0;
518
+ const take = Math.min(rec.size, MB);
519
  async function readSpan(start, end) {
520
  let acc = Buffer.alloc(0);
521
  for await (const c of rangeChunks(bot, rec, start, end, () => acc.length >= take)) {
 
712
  const rec = got.rec;
713
  if (rec.hash && rec.hash !== hash) return json(res, 400, { error: "hash" });
714
  const cachedDur = durCache.get(String(msgId));
715
+ if (cachedDur > 1) rec.duration = cachedDur;
716
+ if (!(rec.duration > 1)) {
717
+ const d = await withTime(fillDuration(bot, rec, msgId), 3500, 0);
718
+ if (d > 1) rec.duration = d;
719
+ }
720
  const playMime = httpVideoType(rec.mime, rec.name);
721
  const exp = Math.floor(Date.now() / 1000) + TOKEN_TTL;
722
  const k = sealTicket({ m: msgId, h: hash, u: uid || "anon", l: lvl, b: bot.idx, e: exp, s: rec.size, n: playMime });
 
730
  accessLevel: lvl,
731
  expiry: exp
732
  });
 
733
  }
734
 
735
  async function handleStream(req, res, url, msgId) {