web: basic api interaction & downloading

download button now acts the way it should with various states
This commit is contained in:
wukko 2024-06-16 18:22:44 +06:00
parent 324729eb21
commit 1f2c28bd02
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2
4 changed files with 104 additions and 5 deletions

View file

@ -60,7 +60,7 @@
<ClearButton click={() => (link = "")} /> <ClearButton click={() => (link = "")} />
{/if} {/if}
{#if validLink(link)} {#if validLink(link)}
<DownloadButton /> <DownloadButton url={link} />
{/if} {/if}
</div> </div>

View file

@ -1,9 +1,78 @@
<script> <script lang="ts">
import '@fontsource-variable/noto-sans-mono'; import '@fontsource-variable/noto-sans-mono';
import API from "$lib/api";
export let url: string;
$: buttonText = '>>';
$: isDisabled = false;
export const changeDownloadButton = (state: string) => {
isDisabled = true;
switch(state) {
case "think":
buttonText = '...';
break;
case "check":
buttonText = '..?';
break;
case "done":
buttonText = '>>>';
break;
case "error":
buttonText = '!!';
break;
}
}
export const restoreDownloadButton = () => {
setTimeout(() => {
buttonText = '>>';
isDisabled = false;
}, 2500)
}
const download = async (link: string) => {
changeDownloadButton("think");
const response = await API.request(link);
if (!response) {
changeDownloadButton("error");
restoreDownloadButton();
console.log("couldn't access the api")
}
if (["error", "rate-limit"].includes(response?.status) && response?.text) {
changeDownloadButton("error");
restoreDownloadButton();
console.log(`error from api: ${response?.text}`)
}
if (response?.url) {
if (response?.status === "redirect") {
changeDownloadButton("done");
window.open(response?.url, '_blank');
restoreDownloadButton();
}
if (response?.status === "stream") {
changeDownloadButton("check");
const probeResult = await API.probeCobaltStream(response?.url);
if (probeResult === 200) {
changeDownloadButton("done");
window.open(response?.url, '_blank');
restoreDownloadButton();
}
}
}
};
</script> </script>
<button id="download-button"> <button id="download-button" disabled={isDisabled} on:click={() => download(url)}>
<span id="download-state">&gt;&gt;</span> <span id="download-state">{buttonText}</span>
</button> </button>
<style> <style>

31
web/src/lib/api.ts Normal file
View file

@ -0,0 +1,31 @@
const apiURL = "https://api.cobalt.tools";
const request = async (url: string) => {
const request = {
url
}
const response = await fetch(`${apiURL}/api/json`, {
method: "POST",
body: JSON.stringify(request),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
}).then(r => r.json()).catch(() => {});
return response;
}
const probeCobaltStream = async (url: string) => {
const request = await fetch(`${url}&p=1`).catch(() => {});
if (request?.status === 200) {
return request?.status;
}
return 0;
}
export default {
request,
probeCobaltStream,
}

View file

@ -1 +0,0 @@
// place files you want to import through the `$lib` alias in this folder.