mirror of
https://github.com/MasterAcnolo/Freedom-Loader.git
synced 2026-11-13 15:09:20 +01:00
feat(download): update fetch requests to use JSON format and improve error handling
This commit is contained in:
@@ -53,17 +53,32 @@ form.addEventListener("submit", async (e) => {
|
|||||||
|
|
||||||
const formData = new FormData(form);
|
const formData = new FormData(form);
|
||||||
const params = new URLSearchParams(formData);
|
const params = new URLSearchParams(formData);
|
||||||
|
const body = Object.fromEntries(params.entries());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await fetch("/download", {
|
const res = await fetch("/download", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
headers: {"Content-Type": "application/json"},
|
||||||
body: params,
|
body: JSON.stringify(body),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
if (!userStoppedDownload) {
|
if (!userStoppedDownload) {
|
||||||
const errorText = await res.text();
|
let errorText = "Server Error";
|
||||||
|
try {
|
||||||
|
const contentType = res.headers.get("content-type") || "";
|
||||||
|
if (contentType.includes("application/json")) {
|
||||||
|
const json = await res.json();
|
||||||
|
errorText =
|
||||||
|
(json && json.error && json.error.message) ||
|
||||||
|
(json && json.message) ||
|
||||||
|
errorText;
|
||||||
|
} else {
|
||||||
|
errorText = await res.text();
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
window.showError(errorText);
|
window.showError(errorText);
|
||||||
resetProgressBar();
|
resetProgressBar();
|
||||||
}
|
}
|
||||||
@@ -71,19 +86,27 @@ form.addEventListener("submit", async (e) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Download successful - check if notifications are enabled
|
// Download successful - check if notifications are enabled
|
||||||
|
try {
|
||||||
const features = await window.electronAPI.getFeatures();
|
const features = await window.electronAPI.getFeatures();
|
||||||
if (features.notifySystem) {
|
if (features.notifySystem) {
|
||||||
window.showSuccess("Download completed!");
|
window.showSuccess("Download completed!");
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
|
window.showSuccess("Download completed!");
|
||||||
|
}
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (!isDownloading && !userStoppedDownload) {
|
if (!isDownloading && !userStoppedDownload) {
|
||||||
statusDiv.style.display = "none";
|
statusDiv.style.display = "none";
|
||||||
}
|
}
|
||||||
|
if (!userStoppedDownload) {
|
||||||
|
window.showError("Network error while starting download");
|
||||||
|
resetProgressBar();
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
isDownloading = false;
|
isDownloading = false;
|
||||||
button.disabled = false;
|
button.disabled = false;
|
||||||
stopBtn.style.display = "none";
|
if (stopBtn) stopBtn.style.display = "none";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -135,7 +158,7 @@ async function stopDownload() {
|
|||||||
try {
|
try {
|
||||||
await fetch("/download/cancel", {
|
await fetch("/download/cancel", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
headers: {"Content-Type": "application/json"},
|
||||||
});
|
});
|
||||||
|
|
||||||
window.showWarning("Download stopped");
|
window.showWarning("Download stopped");
|
||||||
|
|||||||
@@ -32,14 +32,27 @@ async function fetchVideoInfo(url) {
|
|||||||
try {
|
try {
|
||||||
const res = await fetch(`/info?url=${encodeURIComponent(url)}`, {
|
const res = await fetch(`/info?url=${encodeURIComponent(url)}`, {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {"Accept": "application/json"}
|
headers: {Accept: "application/json"},
|
||||||
});
|
});
|
||||||
|
|
||||||
const data = await res.json();
|
let data;
|
||||||
|
try {
|
||||||
|
data = await res.json();
|
||||||
|
} catch (e) {
|
||||||
|
return {error: "Network or JSON Issue"};
|
||||||
|
}
|
||||||
|
|
||||||
if (!res.ok) return {error: `An Error occured when fetching info`};
|
if (!res.ok) {
|
||||||
|
if (data && data.error) {
|
||||||
|
return {error: data.error.message || "An Error occured when fetching info"};
|
||||||
|
}
|
||||||
|
return {error: "An Error occured when fetching info"};
|
||||||
|
}
|
||||||
|
|
||||||
if (!data) return { error: "Data is Missing" };
|
if (!data) return { error: "Data is Missing" };
|
||||||
|
if (data.error) {
|
||||||
|
return {error: data.error.message || data.error};
|
||||||
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user