From d89590434b84d2dc8be9ad78141eaeec2f634e30 Mon Sep 17 00:00:00 2001 From: arabcoders Date: Mon, 28 Apr 2025 18:40:23 +0300 Subject: [PATCH] Switched newDownload form to use HttpAPI for easier unlocking of download button. --- app/library/HttpAPI.py | 15 +++++---- ui/components/NewDownload.vue | 61 ++++++++++++++++------------------- 2 files changed, 35 insertions(+), 41 deletions(-) diff --git a/app/library/HttpAPI.py b/app/library/HttpAPI.py index 8e1c5912..6a611938 100644 --- a/app/library/HttpAPI.py +++ b/app/library/HttpAPI.py @@ -722,14 +722,15 @@ class HttpAPI(Common): except ValueError as e: return web.json_response(data={"error": str(e), "data": item}, status=web.HTTPBadRequest.status_code) - return web.json_response( - data=await asyncio.wait_for( - fut=asyncio.gather(*[self.add(item=item) for item in items]), - timeout=None, - ), - status=web.HTTPOk.status_code, - dumps=self.encoder.encode, + status = await asyncio.wait_for( + fut=asyncio.gather(*[self.add(item=item) for item in items]), + timeout=None, ) + response = [] + for i, item in enumerate(items): + response.append({"item": item, "status": status[i].get("status") == "ok", "msg": status[i].get("msg")}) + + return web.json_response(data=response, status=web.HTTPOk.status_code, dumps=self.encoder.encode) @route("GET", "api/logs") async def logs(self, request: Request) -> Response: diff --git a/ui/components/NewDownload.vue b/ui/components/NewDownload.vue index 58084dd5..de488b08 100644 --- a/ui/components/NewDownload.vue +++ b/ui/components/NewDownload.vue @@ -206,12 +206,13 @@ const addDownload = async () => { } } - addInProgress.value = true + const request_data = [] - form.value.url.split(',').forEach(url => { + form.value.url.split(',').forEach(async (url) => { if (!url.trim()) { return } + const data = { url: url, preset: config.app.basic_mode ? config.app.default_preset : form.value.preset, @@ -225,8 +226,31 @@ const addDownload = async () => { data.extras = form.value.extras } - socket.emit('add_url', data) + request_data.push(data) }) + + try { + addInProgress.value = true + const response = await request('/api/history', { + credentials: 'include', + method: 'POST', + body: JSON.stringify(request_data), + }) + + if (!response.ok) { + const data = await response.json() + throw new Error(data.error) + } + + form.value.url = '' + emitter('clear_form') + } + catch (e) { + console.error(e) + toast.error(`Error: ${e.message}`) + } finally { + addInProgress.value = false + } } const resetConfig = () => { @@ -250,29 +274,6 @@ const resetConfig = () => { toast.success('Local configuration has been reset.') } -const statusHandler = async stream => { - const { status, msg } = JSON.parse(stream) - - addInProgress.value = false - - if ('error' === status) { - toast.error(msg) - return - } - - form.value.url = '' -} - -const unlockDownload = async stream => { - const json = JSON.parse(stream) - if (!json?.data) { - return - } - if ("unlock" in json.data && true === json.data.unlock) { - addInProgress.value = false - } -} - const convertOptions = async args => { try { const response = await convertCliOptions(args) @@ -294,9 +295,6 @@ const convertOptions = async args => { } onMounted(async () => { - socket.on('status', statusHandler) - socket.on('error', unlockDownload) - await nextTick() if ('' === form.value?.preset) { @@ -317,11 +315,6 @@ onMounted(async () => { } }) -onUnmounted(() => { - socket.off('status', statusHandler) - socket.off('error', unlockDownload) -}) - const hasFormatInConfig = computed(() => { if (!form?.value?.value) { return false