Spaces:
Running
Running
JOHN SILLVESTOR commited on
Revert join-movie preview; per-file 40/70/100 cap
Browse files
server.js
CHANGED
|
@@ -1125,80 +1125,10 @@ function ticketLvl(ticket) {
|
|
| 1125 |
return "anonymous";
|
| 1126 |
}
|
| 1127 |
|
| 1128 |
-
function
|
| 1129 |
-
const s = String(name || "");
|
| 1130 |
-
const m = /\.part(\d{1,2})-of-(\d{1,2})\.mp4$/i.exec(s);
|
| 1131 |
-
if (!m) return null;
|
| 1132 |
-
const idx = Number(m[1]);
|
| 1133 |
-
const total = Number(m[2]);
|
| 1134 |
-
if (!Number.isInteger(idx) || !Number.isInteger(total) || total < 2 || idx < 1 || idx > total) return null;
|
| 1135 |
-
const base = s.slice(0, m.index).toLowerCase().replace(/[._]+/g, " ").replace(/\s+/g, " ").trim();
|
| 1136 |
-
return { idx, total, base };
|
| 1137 |
-
}
|
| 1138 |
-
|
| 1139 |
-
function recByMsg(msgId) {
|
| 1140 |
-
const id = String(msgId || "");
|
| 1141 |
-
if (!id) return null;
|
| 1142 |
-
const p = id + ":";
|
| 1143 |
-
let best = null;
|
| 1144 |
-
for (const [k, v] of fileCache) {
|
| 1145 |
-
if ((k === id || k.startsWith(p)) && v && v.size) {
|
| 1146 |
-
if (!best || (v.ts || 0) > (best.ts || 0)) best = v;
|
| 1147 |
-
}
|
| 1148 |
-
}
|
| 1149 |
-
return best;
|
| 1150 |
-
}
|
| 1151 |
-
|
| 1152 |
-
function joinLayout(msgId, rec) {
|
| 1153 |
-
const j = parseJoinPart(rec && rec.name);
|
| 1154 |
-
if (!j) return null;
|
| 1155 |
-
const sizes = new Array(j.total).fill(0);
|
| 1156 |
-
sizes[j.idx - 1] = Number(rec.size) || 0;
|
| 1157 |
-
const id0 = Number(msgId) - (j.idx - 1);
|
| 1158 |
-
if (Number.isInteger(id0) && id0 > 0) {
|
| 1159 |
-
for (let i = 0; i < j.total; i++) {
|
| 1160 |
-
if (sizes[i]) continue;
|
| 1161 |
-
const r = recByMsg(id0 + i);
|
| 1162 |
-
if (!r) continue;
|
| 1163 |
-
const p = parseJoinPart(r.name);
|
| 1164 |
-
if (p && p.idx === i + 1 && p.total === j.total && p.base === j.base) sizes[i] = Number(r.size) || 0;
|
| 1165 |
-
}
|
| 1166 |
-
}
|
| 1167 |
-
for (const v of fileCache.values()) {
|
| 1168 |
-
if (!v || !v.name || !v.size) continue;
|
| 1169 |
-
const p = parseJoinPart(v.name);
|
| 1170 |
-
if (!p || p.total !== j.total || p.base !== j.base) continue;
|
| 1171 |
-
if (!sizes[p.idx - 1]) sizes[p.idx - 1] = Number(v.size) || 0;
|
| 1172 |
-
}
|
| 1173 |
-
const known = sizes.every((n) => n > 0);
|
| 1174 |
-
const z = known ? sizes.reduce((a, b) => a + b, 0) : (Number(rec.size) || 0) * j.total;
|
| 1175 |
-
const b = known ? sizes.slice(0, j.idx - 1).reduce((a, c) => a + c, 0) : (Number(rec.size) || 0) * (j.idx - 1);
|
| 1176 |
-
return { i: j.idx, n: j.total, z, b };
|
| 1177 |
-
}
|
| 1178 |
-
|
| 1179 |
-
function previewMaxEnd(size, l, ticket, msgId) {
|
| 1180 |
const pct = lvlPct(l);
|
| 1181 |
if (!size || pct >= 100) return size - 1;
|
| 1182 |
-
|
| 1183 |
-
let before = 0;
|
| 1184 |
-
const raw = ticket && ticket.j;
|
| 1185 |
-
if (Array.isArray(raw) && raw.length >= 4 && Number(raw[1]) > 1) {
|
| 1186 |
-
total = Number(raw[2]) || size * Number(raw[1]);
|
| 1187 |
-
before = Number(raw[3]) || 0;
|
| 1188 |
-
} else {
|
| 1189 |
-
const rec = recByMsg(msgId);
|
| 1190 |
-
const layout = rec ? joinLayout(msgId, rec) : null;
|
| 1191 |
-
if (layout && layout.n > 1) {
|
| 1192 |
-
total = layout.z;
|
| 1193 |
-
before = layout.b;
|
| 1194 |
-
}
|
| 1195 |
-
}
|
| 1196 |
-
if (!(total > 0)) total = size;
|
| 1197 |
-
const capAbs = Math.floor(Number(total) * pct / 100);
|
| 1198 |
-
if (capAbs <= before) return 0;
|
| 1199 |
-
const local = capAbs - before;
|
| 1200 |
-
if (local >= size) return size - 1;
|
| 1201 |
-
return Math.max(0, local - 1);
|
| 1202 |
}
|
| 1203 |
|
| 1204 |
function mp4IndexOff(size, msgId) {
|
|
@@ -1233,7 +1163,7 @@ function applyPreview(rng, size, l, msgId, ticket) {
|
|
| 1233 |
partial: rng.start > 0 || endCap < size - 1
|
| 1234 |
};
|
| 1235 |
}
|
| 1236 |
-
const maxEnd = previewMaxEnd(size, l
|
| 1237 |
const tailWin = Math.max(0, size - INDEX_WIN);
|
| 1238 |
if (rng.start >= tailWin) {
|
| 1239 |
return { start: rng.start, end: endCap, partial: true };
|
|
@@ -2241,8 +2171,6 @@ async function handleGenerate(req, res, url) {
|
|
| 2241 |
const durSec = Number(rec.duration) > 1 ? Math.floor(Number(rec.duration)) : 0;
|
| 2242 |
const exp = Math.floor(Date.now() / 1000) + TOKEN_TTL;
|
| 2243 |
const payload = { m: msgId, h: hash, u: uid || "anon", l: lvl, b: bot.idx, e: exp, s: rec.size, n: playMime, p: ipTag(ip), a: uaTag(req.headers["user-agent"]), d: durSec };
|
| 2244 |
-
const layout = joinLayout(msgId, rec);
|
| 2245 |
-
if (layout && layout.n > 1) payload.j = [layout.i, layout.n, layout.z, layout.b];
|
| 2246 |
const k = sealTicket(payload);
|
| 2247 |
const host = publicSpaceOrigin(req);
|
| 2248 |
const body = {
|
|
|
|
| 1125 |
return "anonymous";
|
| 1126 |
}
|
| 1127 |
|
| 1128 |
+
function previewMaxEnd(size, l) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1129 |
const pct = lvlPct(l);
|
| 1130 |
if (!size || pct >= 100) return size - 1;
|
| 1131 |
+
return Math.max(0, Math.floor(Number(size) * pct / 100) - 1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1132 |
}
|
| 1133 |
|
| 1134 |
function mp4IndexOff(size, msgId) {
|
|
|
|
| 1163 |
partial: rng.start > 0 || endCap < size - 1
|
| 1164 |
};
|
| 1165 |
}
|
| 1166 |
+
const maxEnd = previewMaxEnd(size, l);
|
| 1167 |
const tailWin = Math.max(0, size - INDEX_WIN);
|
| 1168 |
if (rng.start >= tailWin) {
|
| 1169 |
return { start: rng.start, end: endCap, partial: true };
|
|
|
|
| 2171 |
const durSec = Number(rec.duration) > 1 ? Math.floor(Number(rec.duration)) : 0;
|
| 2172 |
const exp = Math.floor(Date.now() / 1000) + TOKEN_TTL;
|
| 2173 |
const payload = { m: msgId, h: hash, u: uid || "anon", l: lvl, b: bot.idx, e: exp, s: rec.size, n: playMime, p: ipTag(ip), a: uaTag(req.headers["user-agent"]), d: durSec };
|
|
|
|
|
|
|
| 2174 |
const k = sealTicket(payload);
|
| 2175 |
const host = publicSpaceOrigin(req);
|
| 2176 |
const body = {
|