Fix Real-Debrid resume size mismatch handling
This commit is contained in:
parent
157feb8eb7
commit
87212ddf76
@ -118,6 +118,8 @@ const ARCHIVE_SETTLE_MAX_WAIT_MS = 5000;
|
|||||||
|
|
||||||
const MAX_SAME_DIRECT_URL_ATTEMPTS = 3;
|
const MAX_SAME_DIRECT_URL_ATTEMPTS = 3;
|
||||||
|
|
||||||
|
const REALDEBRID_TOTAL_MISMATCH_TOLERANCE_BYTES = 64 * 1024;
|
||||||
|
|
||||||
const LARGE_BINARY_FILE_RE = /\.(?:part\d+\.rar|rar|r\d{2,3}|zip(?:\.\d+)?|7z(?:\.\d+)?|tar|gz|bz2|xz|iso|mkv|mp4|avi|mov|wmv|m4v|ts|m2ts|webm|mp3|flac|aac|wav)$/i;
|
const LARGE_BINARY_FILE_RE = /\.(?:part\d+\.rar|rar|r\d{2,3}|zip(?:\.\d+)?|7z(?:\.\d+)?|tar|gz|bz2|xz|iso|mkv|mp4|avi|mov|wmv|m4v|ts|m2ts|webm|mp3|flac|aac|wav)$/i;
|
||||||
|
|
||||||
function itemExpectedMinBytes(item: DownloadItem): number {
|
function itemExpectedMinBytes(item: DownloadItem): number {
|
||||||
@ -412,6 +414,67 @@ function isResumeHardResetReason(errorText: string): boolean {
|
|||||||
return text.startsWith("resume_download_underflow:");
|
return text.startsWith("resume_download_underflow:");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isRealDebridProvider(provider: string | null | undefined): boolean {
|
||||||
|
return String(provider || "").trim().toLowerCase() === "realdebrid";
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getAuthoritativeRealDebridTotal(
|
||||||
|
provider: string | null | undefined,
|
||||||
|
knownTotal: number,
|
||||||
|
existingBytes: number,
|
||||||
|
responseStatus: number,
|
||||||
|
contentLength: number,
|
||||||
|
totalFromRange: number | null,
|
||||||
|
resumeHardResetUsed: boolean
|
||||||
|
): { totalBytes: number; source: "content-range" | "content-length"; mismatchBytes: number } | null {
|
||||||
|
if (!isRealDebridProvider(provider) || !knownTotal || knownTotal <= 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const evaluateCandidate = (
|
||||||
|
candidateTotal: number,
|
||||||
|
source: "content-range" | "content-length"
|
||||||
|
): { totalBytes: number; source: "content-range" | "content-length"; mismatchBytes: number } | null => {
|
||||||
|
if (!Number.isFinite(candidateTotal) || candidateTotal <= 0 || candidateTotal >= knownTotal) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const mismatchBytes = knownTotal - candidateTotal;
|
||||||
|
if (mismatchBytes > REALDEBRID_TOTAL_MISMATCH_TOLERANCE_BYTES) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (candidateTotal + ALLOCATION_UNIT_SIZE < existingBytes) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (responseStatus === 206) {
|
||||||
|
if (existingBytes <= 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const maxReachableBytes = existingBytes + Math.max(0, contentLength);
|
||||||
|
if (candidateTotal > maxReachableBytes + ALLOCATION_UNIT_SIZE) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
} else if (responseStatus === 200) {
|
||||||
|
if (!resumeHardResetUsed || source !== "content-length") {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
totalBytes: candidateTotal,
|
||||||
|
source,
|
||||||
|
mismatchBytes
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
return evaluateCandidate(totalFromRange || 0, "content-range")
|
||||||
|
|| evaluateCandidate(contentLength, "content-length");
|
||||||
|
}
|
||||||
|
|
||||||
function isPermanentLinkError(errorText: string): boolean {
|
function isPermanentLinkError(errorText: string): boolean {
|
||||||
const text = String(errorText || "").toLowerCase();
|
const text = String(errorText || "").toLowerCase();
|
||||||
return text.includes("permanent ungültig")
|
return text.includes("permanent ungültig")
|
||||||
@ -6755,9 +6818,10 @@ export class DownloadManager extends EventEmitter {
|
|||||||
active.resumeHardResetUsed = true;
|
active.resumeHardResetUsed = true;
|
||||||
item.retries += 1;
|
item.retries += 1;
|
||||||
logger.warn(`Resume-Neustart: item=${item.fileName || item.id}, error=${exhaustedReason}, provider=${item.provider || "?"}`);
|
logger.warn(`Resume-Neustart: item=${item.fileName || item.id}, error=${exhaustedReason}, provider=${item.provider || "?"}`);
|
||||||
if (claimedTargetPath) {
|
const resetTargetPath = claimedTargetPath || String(item.targetPath || "").trim();
|
||||||
|
if (resetTargetPath) {
|
||||||
try {
|
try {
|
||||||
fs.rmSync(claimedTargetPath, { force: true });
|
fs.rmSync(resetTargetPath, { force: true });
|
||||||
} catch {
|
} catch {
|
||||||
// ignore
|
// ignore
|
||||||
}
|
}
|
||||||
@ -7219,7 +7283,10 @@ export class DownloadManager extends EventEmitter {
|
|||||||
const contentLength = Number.isFinite(rawContentLength) && rawContentLength > 0 ? rawContentLength : 0;
|
const contentLength = Number.isFinite(rawContentLength) && rawContentLength > 0 ? rawContentLength : 0;
|
||||||
const totalFromRange = parseContentRangeTotal(response.headers.get("content-range"));
|
const totalFromRange = parseContentRangeTotal(response.headers.get("content-range"));
|
||||||
const serverIgnoredRange = existingBytes > 0 && response.status === 200;
|
const serverIgnoredRange = existingBytes > 0 && response.status === 200;
|
||||||
if (serverIgnoredRange) {
|
const allowFreshOverwriteAfterResumeReset = serverIgnoredRange
|
||||||
|
&& active.resumeHardResetUsed
|
||||||
|
&& isRealDebridProvider(item.provider);
|
||||||
|
if (serverIgnoredRange && !allowFreshOverwriteAfterResumeReset) {
|
||||||
logger.warn(`Server ignorierte Range-Header (HTTP 200 statt 206), verwerfe Direktlink und behalte Teil-Datei: ${item.fileName}`);
|
logger.warn(`Server ignorierte Range-Header (HTTP 200 statt 206), verwerfe Direktlink und behalte Teil-Datei: ${item.fileName}`);
|
||||||
logAttemptEvent("WARN", "Server ignorierte Range-Header beim Resume", {
|
logAttemptEvent("WARN", "Server ignorierte Range-Header beim Resume", {
|
||||||
attempt,
|
attempt,
|
||||||
@ -7234,8 +7301,45 @@ export class DownloadManager extends EventEmitter {
|
|||||||
}
|
}
|
||||||
throw new Error(`range_ignored_on_resume:${existingBytes}/${contentLength || 0}`);
|
throw new Error(`range_ignored_on_resume:${existingBytes}/${contentLength || 0}`);
|
||||||
}
|
}
|
||||||
|
if (allowFreshOverwriteAfterResumeReset) {
|
||||||
|
logger.warn(
|
||||||
|
`Server ignorierte Range-Header nach Resume-Reset, ueberschreibe alte Teil-Datei frisch: ${item.fileName}`
|
||||||
|
);
|
||||||
|
logAttemptEvent("WARN", "Range ignoriert nach Resume-Reset, frischer Vollstart erlaubt", {
|
||||||
|
attempt,
|
||||||
|
existingBytes,
|
||||||
|
contentLength,
|
||||||
|
directUrl
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (knownTotal && knownTotal > 0) {
|
const correctedRealDebridTotal = getAuthoritativeRealDebridTotal(
|
||||||
|
item.provider,
|
||||||
|
knownTotal || 0,
|
||||||
|
existingBytes,
|
||||||
|
response.status,
|
||||||
|
contentLength,
|
||||||
|
totalFromRange,
|
||||||
|
Boolean(active.resumeHardResetUsed)
|
||||||
|
);
|
||||||
|
if (correctedRealDebridTotal) {
|
||||||
|
item.totalBytes = correctedRealDebridTotal.totalBytes;
|
||||||
|
logger.warn(
|
||||||
|
`Real-Debrid-Zielgroesse korrigiert: ${item.fileName} ` +
|
||||||
|
`known=${knownTotal}, corrected=${correctedRealDebridTotal.totalBytes}, ` +
|
||||||
|
`source=${correctedRealDebridTotal.source}`
|
||||||
|
);
|
||||||
|
logAttemptEvent("WARN", "Real-Debrid-Zielgroesse aus HTTP korrigiert", {
|
||||||
|
attempt,
|
||||||
|
source: correctedRealDebridTotal.source,
|
||||||
|
knownTotal,
|
||||||
|
correctedTotal: correctedRealDebridTotal.totalBytes,
|
||||||
|
mismatchBytes: correctedRealDebridTotal.mismatchBytes,
|
||||||
|
existingBytes,
|
||||||
|
contentLength,
|
||||||
|
totalFromRange
|
||||||
|
});
|
||||||
|
} else if (knownTotal && knownTotal > 0) {
|
||||||
item.totalBytes = knownTotal;
|
item.totalBytes = knownTotal;
|
||||||
} else if (totalFromRange) {
|
} else if (totalFromRange) {
|
||||||
item.totalBytes = totalFromRange;
|
item.totalBytes = totalFromRange;
|
||||||
|
|||||||
@ -3805,7 +3805,7 @@ export function App(): ReactElement {
|
|||||||
<svg viewBox="0 0 24 24" width="16" height="16"><path d="M12 20l7-7h-4.5V4h-5v9H5z" fill="currentColor" /></svg>
|
<svg viewBox="0 0 24 24" width="16" height="16"><path d="M12 20l7-7h-4.5V4h-5v9H5z" fill="currentColor" /></svg>
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
{snapshot.reconnectSeconds > 0 && (
|
{snapshot.reconnectSeconds > 0 && tab !== "downloads" && (
|
||||||
<div className="reconnect-badge" style={{ marginLeft: "auto" }}>Reconnect: {snapshot.reconnectSeconds}s</div>
|
<div className="reconnect-badge" style={{ marginLeft: "auto" }}>Reconnect: {snapshot.reconnectSeconds}s</div>
|
||||||
)}
|
)}
|
||||||
</section>
|
</section>
|
||||||
|
|||||||
@ -5,7 +5,7 @@ import http from "node:http";
|
|||||||
import { EventEmitter, once } from "node:events";
|
import { EventEmitter, once } from "node:events";
|
||||||
import AdmZip from "adm-zip";
|
import AdmZip from "adm-zip";
|
||||||
import { afterEach, describe, expect, it } from "vitest";
|
import { afterEach, describe, expect, it } from "vitest";
|
||||||
import { DownloadManager } from "../src/main/download-manager";
|
import { DownloadManager, getAuthoritativeRealDebridTotal } from "../src/main/download-manager";
|
||||||
import { defaultSettings } from "../src/main/constants";
|
import { defaultSettings } from "../src/main/constants";
|
||||||
import { parseDebridLinkApiKeys } from "../src/shared/debrid-link-keys";
|
import { parseDebridLinkApiKeys } from "../src/shared/debrid-link-keys";
|
||||||
import { getProviderUsageDayKey } from "../src/shared/provider-daily-limits";
|
import { getProviderUsageDayKey } from "../src/shared/provider-daily-limits";
|
||||||
@ -475,6 +475,187 @@ describe("download manager", () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("treats tiny Real-Debrid resume size mismatches as completed instead of looping", async () => {
|
||||||
|
const root = fs.mkdtempSync(path.join(os.tmpdir(), "rd-dm-"));
|
||||||
|
tempDirs.push(root);
|
||||||
|
const actual = Buffer.alloc(192 * 1024, 17);
|
||||||
|
const advertisedSize = actual.length + 5000;
|
||||||
|
const pkgDir = path.join(root, "downloads", "rd-mismatch");
|
||||||
|
fs.mkdirSync(pkgDir, { recursive: true });
|
||||||
|
const existingTargetPath = path.join(pkgDir, "rd-mismatch.part01.rar");
|
||||||
|
fs.writeFileSync(existingTargetPath, actual);
|
||||||
|
|
||||||
|
let unrestrictCalls = 0;
|
||||||
|
let resumeCalls = 0;
|
||||||
|
const resumeStarts: number[] = [];
|
||||||
|
|
||||||
|
const server = http.createServer((req, res) => {
|
||||||
|
if ((req.url || "") !== "/rd-mismatch") {
|
||||||
|
res.statusCode = 404;
|
||||||
|
res.end("not-found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
resumeCalls += 1;
|
||||||
|
const range = String(req.headers.range || "");
|
||||||
|
const match = range.match(/bytes=(\d+)-/i);
|
||||||
|
const start = match ? Number(match[1]) : 0;
|
||||||
|
resumeStarts.push(start);
|
||||||
|
|
||||||
|
if (start >= actual.length) {
|
||||||
|
res.statusCode = 206;
|
||||||
|
res.setHeader("Accept-Ranges", "bytes");
|
||||||
|
res.setHeader("Content-Range", `bytes 0-${actual.length - 1}/${actual.length}`);
|
||||||
|
res.setHeader("Content-Length", "0");
|
||||||
|
res.end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const chunk = actual.subarray(start);
|
||||||
|
if (start > 0) {
|
||||||
|
res.statusCode = 206;
|
||||||
|
res.setHeader("Content-Range", `bytes ${start}-${actual.length - 1}/${actual.length}`);
|
||||||
|
} else {
|
||||||
|
res.statusCode = 200;
|
||||||
|
}
|
||||||
|
res.setHeader("Accept-Ranges", "bytes");
|
||||||
|
res.setHeader("Content-Length", String(chunk.length));
|
||||||
|
res.end(chunk);
|
||||||
|
});
|
||||||
|
|
||||||
|
server.listen(0, "127.0.0.1");
|
||||||
|
await once(server, "listening");
|
||||||
|
|
||||||
|
const address = server.address();
|
||||||
|
if (!address || typeof address === "string") {
|
||||||
|
throw new Error("server address unavailable");
|
||||||
|
}
|
||||||
|
const directUrl = `http://127.0.0.1:${address.port}/rd-mismatch`;
|
||||||
|
|
||||||
|
globalThis.fetch = async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
|
||||||
|
const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
|
||||||
|
if (url.includes("/unrestrict/link")) {
|
||||||
|
unrestrictCalls += 1;
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
download: directUrl,
|
||||||
|
filename: "rd-mismatch.part01.rar",
|
||||||
|
filesize: advertisedSize
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
status: 200,
|
||||||
|
headers: { "Content-Type": "application/json" }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return originalFetch(input, init);
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const session = emptySession();
|
||||||
|
const packageId = "rd-mismatch-pkg";
|
||||||
|
const itemId = "rd-mismatch-item";
|
||||||
|
const createdAt = Date.now() - 10_000;
|
||||||
|
|
||||||
|
session.packageOrder = [packageId];
|
||||||
|
session.packages[packageId] = {
|
||||||
|
id: packageId,
|
||||||
|
name: "rd-mismatch",
|
||||||
|
outputDir: pkgDir,
|
||||||
|
extractDir: path.join(root, "extract", "rd-mismatch"),
|
||||||
|
status: "queued",
|
||||||
|
itemIds: [itemId],
|
||||||
|
cancelled: false,
|
||||||
|
enabled: true,
|
||||||
|
createdAt,
|
||||||
|
updatedAt: createdAt
|
||||||
|
};
|
||||||
|
session.items[itemId] = {
|
||||||
|
id: itemId,
|
||||||
|
packageId,
|
||||||
|
url: "https://dummy/rd-mismatch",
|
||||||
|
provider: "realdebrid",
|
||||||
|
status: "queued",
|
||||||
|
retries: 0,
|
||||||
|
speedBps: 0,
|
||||||
|
downloadedBytes: actual.length,
|
||||||
|
totalBytes: advertisedSize,
|
||||||
|
progressPercent: Math.floor((actual.length / advertisedSize) * 100),
|
||||||
|
fileName: "rd-mismatch.part01.rar",
|
||||||
|
targetPath: existingTargetPath,
|
||||||
|
resumable: true,
|
||||||
|
attempts: 0,
|
||||||
|
lastError: "",
|
||||||
|
fullStatus: "Wartet",
|
||||||
|
createdAt,
|
||||||
|
updatedAt: createdAt
|
||||||
|
};
|
||||||
|
|
||||||
|
const manager = new DownloadManager(
|
||||||
|
{
|
||||||
|
...defaultSettings(),
|
||||||
|
token: "rd-token",
|
||||||
|
outputDir: path.join(root, "downloads"),
|
||||||
|
extractDir: path.join(root, "extract"),
|
||||||
|
retryLimit: 1,
|
||||||
|
autoExtract: false,
|
||||||
|
autoReconnect: false
|
||||||
|
},
|
||||||
|
session,
|
||||||
|
createStoragePaths(path.join(root, "state"))
|
||||||
|
);
|
||||||
|
|
||||||
|
await manager.start();
|
||||||
|
await waitFor(() => !manager.getSnapshot().session.running, 12000);
|
||||||
|
|
||||||
|
const item = manager.getSnapshot().session.items[itemId];
|
||||||
|
expect(item?.status).toBe("completed");
|
||||||
|
expect(item?.downloadedBytes).toBe(actual.length);
|
||||||
|
expect(item?.totalBytes).toBe(actual.length);
|
||||||
|
expect(unrestrictCalls).toBe(1);
|
||||||
|
expect(resumeCalls).toBeGreaterThanOrEqual(1);
|
||||||
|
expect(resumeStarts).toContain(actual.length);
|
||||||
|
expect(fs.statSync(existingTargetPath).size).toBe(actual.length);
|
||||||
|
} finally {
|
||||||
|
server.close();
|
||||||
|
await once(server, "close");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("accepts the smaller Real-Debrid full response after a resume hard reset", () => {
|
||||||
|
const actualSize = 224 * 1024;
|
||||||
|
const advertisedSize = actualSize + 5000;
|
||||||
|
const partialSize = actualSize - 48 * 1024;
|
||||||
|
|
||||||
|
expect(
|
||||||
|
getAuthoritativeRealDebridTotal(
|
||||||
|
"realdebrid",
|
||||||
|
advertisedSize,
|
||||||
|
partialSize,
|
||||||
|
200,
|
||||||
|
actualSize,
|
||||||
|
null,
|
||||||
|
true
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
totalBytes: actualSize,
|
||||||
|
source: "content-length",
|
||||||
|
mismatchBytes: 5000
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
getAuthoritativeRealDebridTotal(
|
||||||
|
"realdebrid",
|
||||||
|
advertisedSize,
|
||||||
|
partialSize,
|
||||||
|
200,
|
||||||
|
actualSize,
|
||||||
|
null,
|
||||||
|
false
|
||||||
|
)
|
||||||
|
).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
it("does not renew direct links when the file is already complete on disk", async () => {
|
it("does not renew direct links when the file is already complete on disk", async () => {
|
||||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), "rd-dm-"));
|
const root = fs.mkdtempSync(path.join(os.tmpdir(), "rd-dm-"));
|
||||||
tempDirs.push(root);
|
tempDirs.push(root);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user