From 0963450c1fb8f48bdb92fa680253986e9a9ab331 Mon Sep 17 00:00:00 2001 From: nxrmqlly Date: Sun, 20 Oct 2024 23:38:08 +0530 Subject: [PATCH] Windows builds dont show cmd, nicer toasts, loader added, more effecient theme switcher (css based) --- backend/controllers/download.go | 2 +- backend/controllers/get.go | 9 +- backend/models/video.go | 2 +- backend/utils/run_cmd.go | 14 +++ frontend/src/App.svelte | 114 ++++++++++-------- .../src/components/ExpandableToast.svelte | 70 +++++++++++ frontend/src/components/Loader.svelte | 35 ++++++ frontend/src/components/ThemeSwitcher.svelte | 47 ++++---- frontend/src/components/VideoModal.svelte | 78 +++++++++--- frontend/src/style.css | 14 ++- main.go | 4 + 11 files changed, 300 insertions(+), 89 deletions(-) create mode 100644 backend/utils/run_cmd.go create mode 100644 frontend/src/components/ExpandableToast.svelte create mode 100644 frontend/src/components/Loader.svelte diff --git a/backend/controllers/download.go b/backend/controllers/download.go index ea5a1bd..bc6eb15 100644 --- a/backend/controllers/download.go +++ b/backend/controllers/download.go @@ -78,7 +78,7 @@ func (a *App) DownloadVideo(videoInfo models.VideoInfo, opts models.DownloadOpti cmdArgs = append(cmdArgs, "-o", pathToSave) cmd := exec.Command(ytdlpPath, cmdArgs...) - output, err := cmd.CombinedOutput() + output, err := utils.RunCmd(cmd) if err != nil { fmt.Println(string(output)) diff --git a/backend/controllers/get.go b/backend/controllers/get.go index 09b9ce0..e3394dc 100644 --- a/backend/controllers/get.go +++ b/backend/controllers/get.go @@ -3,6 +3,7 @@ package controllers import ( "encoding/json" "fmt" + "net/url" "os/exec" "youtube-downloader-go/backend/models" @@ -10,12 +11,18 @@ import ( ) func (a *App) GetVideoInfo(videoURL string) (models.VideoInfo, error) { + + _, err := url.ParseRequestURI(videoURL) + if err != nil { + return models.VideoInfo{}, fmt.Errorf("invalid URL: %w", err) + } + _, ytDlpPath := utils.YtDlpSetup() cmd := exec.Command(ytDlpPath, "-J", videoURL) // Capture the output - output, err := cmd.CombinedOutput() + output, err := utils.RunCmd(cmd) if err != nil { return models.VideoInfo{}, fmt.Errorf("%s; output: %s", err, output) } diff --git a/backend/models/video.go b/backend/models/video.go index ae8ad0b..e641d31 100644 --- a/backend/models/video.go +++ b/backend/models/video.go @@ -7,7 +7,7 @@ type VideoInfo struct { FPS float32 `json:"fps"` Uploader string `json:"uploader"` UploaderURL string `json:"uploader_url"` - Duration int `json:"duration"` // Duration in seconds + Duration float32 `json:"duration"` // Duration in seconds ViewCount int `json:"view_count"` Thumbnail string `json:"thumbnail"` Height int `json:"height"` diff --git a/backend/utils/run_cmd.go b/backend/utils/run_cmd.go new file mode 100644 index 0000000..ca01a34 --- /dev/null +++ b/backend/utils/run_cmd.go @@ -0,0 +1,14 @@ +package utils + +import ( + "os/exec" + "runtime" + "syscall" +) + +func RunCmd(cmd *exec.Cmd) ([]byte, error) { + if runtime.GOOS == "windows" { + cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} + } + return cmd.CombinedOutput() +} diff --git a/frontend/src/App.svelte b/frontend/src/App.svelte index eb91a53..2b1c0cf 100644 --- a/frontend/src/App.svelte +++ b/frontend/src/App.svelte @@ -1,49 +1,41 @@
- + + +

Youtube Downloader

-
- - - +
+ {#if loading} + + {/if} + - - +

Works with almost anything. Blazingly fast, I guess.

Made by @nxrmqlly with ❤ using Go and Svelte

+
+ {#each visibleToasts as toast} + + {/each} +
+ {#if showModal} {/if} - - {#if showToast} - - {/if} - -
\ No newline at end of file + diff --git a/frontend/src/components/ExpandableToast.svelte b/frontend/src/components/ExpandableToast.svelte new file mode 100644 index 0000000..48ddb03 --- /dev/null +++ b/frontend/src/components/ExpandableToast.svelte @@ -0,0 +1,70 @@ + + + + + + +
+ +
{message}
+
+ {detail} +
+ +
diff --git a/frontend/src/components/Loader.svelte b/frontend/src/components/Loader.svelte new file mode 100644 index 0000000..ef6f8c1 --- /dev/null +++ b/frontend/src/components/Loader.svelte @@ -0,0 +1,35 @@ + + + \ No newline at end of file diff --git a/frontend/src/components/ThemeSwitcher.svelte b/frontend/src/components/ThemeSwitcher.svelte index 6cb6edf..7940bdc 100644 --- a/frontend/src/components/ThemeSwitcher.svelte +++ b/frontend/src/components/ThemeSwitcher.svelte @@ -3,34 +3,37 @@ import { onMount } from "svelte"; import { onAccesibilityKeydown } from "../utils/accessibility.js"; import "@fortawesome/fontawesome-free/css/all.min.css"; -let isDarkMode = window.matchMedia("(prefers-color-scheme: dark)").matches; -window - .matchMedia("(prefers-color-scheme: dark)") - .addEventListener("change", (e) => { - isDarkMode = e.matches; - toggleTheme(); - }); -console.log(isDarkMode); +let isDarkMode; -function toggleTheme() { - isDarkMode = !isDarkMode; - if (!isDarkMode) { - document.documentElement.style.setProperty("--bg-color", "#100a0d"); - document.documentElement.style.setProperty("--duo-bg-color", "#212021"); - document.documentElement.style.setProperty("--fg-color", "#f6f3f4"); - } else { - document.documentElement.style.setProperty("--bg-color", "#f6f3f4"); - document.documentElement.style.setProperty("--duo-bg-color", "#f6f3f4"); - document.documentElement.style.setProperty("--fg-color", "#100a0d"); - } +function setTheme(theme) { + document.querySelector("html").setAttribute("data-theme", theme); + localStorage.setItem("theme", theme); } -// sset default theme onMount(() => { - toggleTheme(); + const savedTheme = localStorage.getItem("theme"); + if (savedTheme) { + isDarkMode = savedTheme === "dark"; + } else { + isDarkMode = window.matchMedia("(prefers-color-scheme: dark)").matches; + } + setTheme(isDarkMode ? "dark" : "light"); + + // system theme changes listening + window + .matchMedia("(prefers-color-scheme: dark)") + .addEventListener("change", (e) => { + isDarkMode = e.matches; + setTheme(isDarkMode ? "dark" : "light"); + }); }); - +// Toggle theme function +function toggleTheme() { + isDarkMode = !isDarkMode; + setTheme(isDarkMode ? "dark" : "light"); +} +