fix: queue now work for normal downloads

This commit is contained in:
Fredrik Burmester
2024-09-29 11:59:37 +02:00
parent b0f7cfd013
commit b6c6bac06a
4 changed files with 77 additions and 38 deletions

View File

@@ -53,9 +53,12 @@ const downloads: React.FC = () => {
<View className="px-4 py-4">
<View className="mb-4 flex flex-col space-y-4">
{settings?.downloadMethod === "remux" && (
<View>
<Text className="text-2xl font-bold mb-2">Queue</Text>
<View className="flex flex-col space-y-2">
<View className="bg-neutral-900 p-4 rounded-2xl">
<Text className="text-lg font-bold">Queue</Text>
<Text className="text-xs opacity-70 text-red-600">
Queue and downloads will be lost on app restart
</Text>
<View className="flex flex-col space-y-2 mt-2">
{queue.map((q) => (
<TouchableOpacity
onPress={() =>

View File

@@ -164,6 +164,7 @@ export const DownloadItem: React.FC<DownloadProps> = ({ item, ...props }) => {
selectedAudioStream,
selectedSubtitleStream,
maxBitrate,
settings?.downloadMethod,
]);
/**
@@ -291,14 +292,17 @@ export const DownloadItem: React.FC<DownloadProps> = ({ item, ...props }) => {
throw new Error("No item id");
}
closeModal();
initiateDownload();
// Remove for now
// queueActions.enqueue(queue, setQueue, {
// id: item.Id,
// execute: async () => {
// },
// item,
// });
if (settings?.downloadMethod === "remux") {
queueActions.enqueue(queue, setQueue, {
id: item.Id,
execute: async () => {
await initiateDownload();
},
item,
});
} else {
initiateDownload();
}
} else {
toast.error("You are not allowed to download files.");
}

View File

@@ -48,7 +48,7 @@ export const ActiveDownloads: React.FC<Props> = ({ ...props }) => {
},
onError: (e) => {
console.log(e);
toast.error("Failed to cancel download");
toast.error("Failed to cancel download on the server");
},
});

View File

@@ -161,39 +161,66 @@ function useDownloadProvider() {
const updatedProcesses = await Promise.all(
processes.map(async (process) => {
if (!settings.optimizedVersionsServerUrl) return;
if (!settings.optimizedVersionsServerUrl) return process;
if (process.state === "queued" || process.state === "optimizing") {
const job = await checkJobStatus(
process.id,
settings.optimizedVersionsServerUrl,
authHeader
);
try {
const job = await checkJobStatus(
process.id,
settings.optimizedVersionsServerUrl,
authHeader
);
if (!job) {
return null;
if (!job) {
return process;
}
let newState: ProcessItem["state"] = process.state;
if (job.status === "queued") {
newState = "queued";
} else if (job.status === "running") {
newState = "optimizing";
} else if (job.status === "completed") {
startDownload(process);
return null;
} else if (job.status === "failed") {
newState = "error";
} else if (job.status === "cancelled") {
newState = "canceled";
}
return { ...process, state: newState, progress: job.progress };
} catch (error) {
if (axios.isAxiosError(error) && !error.response) {
// Network error occurred (server might be down)
console.error("Network error occurred:", error.message);
toast.error(
"Network error: Unable to connect to optimization server"
);
return {
...process,
state: "error",
errorMessage:
"Network error: Unable to connect to optimization server",
};
} else {
// Other types of errors
console.error("Error checking job status:", error);
toast.error(
"An unexpected error occurred while checking job status"
);
return {
...process,
state: "error",
errorMessage: "An unexpected error occurred",
};
}
}
let newState: ProcessItem["state"] = process.state;
if (job.status === "queued") {
newState = "queued";
} else if (job.status === "running") {
newState = "optimizing";
} else if (job.status === "completed") {
startDownload(process);
return null;
} else if (job.status === "failed") {
newState = "error";
} else if (job.status === "cancelled") {
newState = "canceled";
}
return { ...process, state: newState, progress: job.progress };
}
return process;
})
);
// Filter out null values (completed or cancelled jobs)
// Filter out null values (completed jobs)
const filteredProcesses = updatedProcesses.filter(
(process) => process !== null
) as ProcessItem[];
@@ -205,7 +232,12 @@ function useDownloadProvider() {
const intervalId = setInterval(checkJobStatusPeriodically, 2000);
return () => clearInterval(intervalId);
}, [processes, settings?.optimizedVersionsServerUrl]);
}, [
processes,
settings?.optimizedVersionsServerUrl,
authHeader,
startDownload,
]);
const startBackgroundDownload = useCallback(
async (url: string, item: BaseItemDto) => {