diff --git a/.gitignore b/.gitignore index ba66166..e111c2a 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,5 @@ yt-dlp-webui session.dat config.yml cookies.txt -__debug* \ No newline at end of file +__debug* +app/ \ No newline at end of file diff --git a/server/rest/container.go b/server/rest/container.go index ee3c0f9..8a40bab 100644 --- a/server/rest/container.go +++ b/server/rest/container.go @@ -30,6 +30,8 @@ func ApplyRouter(db *sql.DB, mdb *internal.MemoryDB, mq *internal.MessageQueue) r.Post("/template", h.AddTemplate()) r.Get("/template/all", h.GetTemplates()) r.Delete("/template/{id}", h.DeleteTemplate()) + r.Get("/tree", h.DirectoryTree()) + r.Get("/d/{id}", h.DownloadFile()) } } diff --git a/server/rest/handlers.go b/server/rest/handlers.go index 8c4d8b9..ea77068 100644 --- a/server/rest/handlers.go +++ b/server/rest/handlers.go @@ -2,7 +2,10 @@ package rest import ( "encoding/json" + "io" "net/http" + "os" + "path/filepath" "github.com/go-chi/chi/v5" "github.com/marcopeocchi/yt-dlp-web-ui/server/internal" @@ -173,3 +176,36 @@ func (h *Handler) DirectoryTree() http.HandlerFunc { } } } + +func (h *Handler) DownloadFile() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + defer r.Body.Close() + + w.Header().Set("Content-Type", "application/json") + + id := chi.URLParam(r, "id") + + path, err := h.service.DownloadFile(r.Context(), id) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Add( + "Content-Disposition", + "inline; filename="+filepath.Base(*path), + ) + w.Header().Set( + "Content-Type", + "application/octet-stream", + ) + + fd, err := os.Open(*path) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + io.Copy(w, fd) + } +} diff --git a/server/rest/service.go b/server/rest/service.go index b03d48e..f0d51c9 100644 --- a/server/rest/service.go +++ b/server/rest/service.go @@ -123,3 +123,12 @@ func (s *Service) DeleteTemplate(ctx context.Context, id string) error { func (s *Service) DirectoryTree(ctx context.Context) (*internal.Stack[sys.FSNode], error) { return sys.DirectoryTree() } + +func (s *Service) DownloadFile(ctx context.Context, id string) (*string, error) { + p, err := s.mdb.Get(id) + if err != nil { + return nil, err + } + + return &p.Output.Path, nil +} diff --git a/ui/package.json b/ui/package.json index b5b3381..7ce45f0 100644 --- a/ui/package.json +++ b/ui/package.json @@ -25,6 +25,7 @@ "dependencies": { "@fontsource/roboto": "^5.0.8", "fp-ts": "^2.16.2", - "lucide-svelte": "^0.323.0" + "lucide-svelte": "^0.323.0", + "svelte-spa-router": "^4.0.1" } } diff --git a/ui/pnpm-lock.yaml b/ui/pnpm-lock.yaml index b766409..ee49cec 100644 --- a/ui/pnpm-lock.yaml +++ b/ui/pnpm-lock.yaml @@ -14,6 +14,9 @@ dependencies: lucide-svelte: specifier: ^0.323.0 version: 0.323.0(svelte@4.2.10) + svelte-spa-router: + specifier: ^4.0.1 + version: 4.0.1 devDependencies: '@sveltejs/vite-plugin-svelte': @@ -1254,6 +1257,11 @@ packages: picomatch: 2.3.1 dev: true + /regexparam@2.0.2: + resolution: {integrity: sha512-A1PeDEYMrkLrfyOwv2jwihXbo9qxdGD3atBYQA9JJgreAx8/7rC6IUkWOw2NQlOxLp2wL0ifQbh1HuidDfYA6w==} + engines: {node: '>=8'} + dev: false + /resolve-from@4.0.0: resolution: {integrity: sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g==} engines: {node: '>=4'} @@ -1498,6 +1506,12 @@ packages: typescript: 5.3.3 dev: true + /svelte-spa-router@4.0.1: + resolution: {integrity: sha512-2JkmUQ2f9jRluijL58LtdQBIpynSbem2eBGp4zXdi7aDY1znbR6yjw0KsonD0aq2QLwf4Yx4tBJQjxIjgjXHKg==} + dependencies: + regexparam: 2.0.2 + dev: false + /svelte@4.2.10: resolution: {integrity: sha512-Ep06yCaCdgG1Mafb/Rx8sJ1QS3RW2I2BxGp2Ui9LBHSZ2/tO/aGLc5WqPjgiAP6KAnLJGaIr/zzwQlOo1b8MxA==} engines: {node: '>=16'} diff --git a/ui/src/App.svelte b/ui/src/App.svelte index cbde770..baa33f9 100644 --- a/ui/src/App.svelte +++ b/ui/src/App.svelte @@ -1,15 +1,24 @@ - + diff --git a/ui/src/lib/Button.svelte b/ui/src/lib/Button.svelte index 08abb4c..94402c8 100644 --- a/ui/src/lib/Button.svelte +++ b/ui/src/lib/Button.svelte @@ -5,7 +5,7 @@ {text} diff --git a/ui/src/lib/DownloadCard.svelte b/ui/src/lib/DownloadCard.svelte index 2c3e609..8c6a268 100644 --- a/ui/src/lib/DownloadCard.svelte +++ b/ui/src/lib/DownloadCard.svelte @@ -1,10 +1,10 @@ - + Settings diff --git a/ui/src/lib/Spinner.svelte b/ui/src/lib/Spinner.svelte index 96163c7..2c1de57 100644 --- a/ui/src/lib/Spinner.svelte +++ b/ui/src/lib/Spinner.svelte @@ -1,7 +1,7 @@ {label} { setInterval(() => $client.running(), 750); diff --git a/ui/src/views/SettingsView.svelte b/ui/src/views/SettingsView.svelte new file mode 100644 index 0000000..8e89143 --- /dev/null +++ b/ui/src/views/SettingsView.svelte @@ -0,0 +1,13 @@ + + + + +