Add HTMX
This commit is contained in:
		
							parent
							
								
									ad39db4aac
								
							
						
					
					
						commit
						3030588ffd
					
				| 
						 | 
				
			
			@ -20,6 +20,7 @@ type routes struct {
 | 
			
		|||
 | 
			
		||||
func (r *routes) Register(app *fiber.App) {
 | 
			
		||||
	app.Get("/", r.IndexHandler)
 | 
			
		||||
	app.Get("/home", r.HomeHandler)
 | 
			
		||||
	app.Get("/download", r.DownloadHandler)
 | 
			
		||||
	app.Head("/download/proxy", r.DownloadProxyHandler)
 | 
			
		||||
	app.Get("/download/proxy", r.DownloadProxyHandler)
 | 
			
		||||
| 
						 | 
				
			
			@ -35,6 +36,13 @@ func (r *routes) IndexHandler(c *fiber.Ctx) error {
 | 
			
		|||
	}, "views/layouts/main")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (r *routes) HomeHandler(c *fiber.Ctx) error {
 | 
			
		||||
	return c.Render("views/index", fiber.Map{
 | 
			
		||||
		"BasePath": viper.GetString("base_path"),
 | 
			
		||||
		"Flash":    flash.Get(c),
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (r *routes) DownloadHandler(c *fiber.Ctx) error {
 | 
			
		||||
	urlBytes, err := url.QueryUnescape(c.Query("url"))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
| 
						 | 
				
			
			@ -60,14 +68,11 @@ func (r *routes) DownloadHandler(c *fiber.Ctx) error {
 | 
			
		|||
	}
 | 
			
		||||
 | 
			
		||||
	return c.Render("views/download", fiber.Map{
 | 
			
		||||
		"BasePath":     viper.GetString("base_path"),
 | 
			
		||||
		"Url":          url,
 | 
			
		||||
		"Meta":         meta,
 | 
			
		||||
		"Videos":       GetVideos(meta),
 | 
			
		||||
		"Version":      version.Version,
 | 
			
		||||
		"Build":        version.Build,
 | 
			
		||||
		"YtdlpVersion": r.ytdl.Version,
 | 
			
		||||
	}, "views/layouts/main")
 | 
			
		||||
		"BasePath": viper.GetString("base_path"),
 | 
			
		||||
		"Url":      url,
 | 
			
		||||
		"Meta":     meta,
 | 
			
		||||
		"Videos":   GetVideos(meta),
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (r *routes) DownloadProxyHandler(c *fiber.Ctx) error {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,35 +2,50 @@
 | 
			
		|||
  <h1>Download Video</h1>
 | 
			
		||||
  <h2 class="fs-4 text-muted text-center">{{.Meta.Title}}</h2>
 | 
			
		||||
  <p style="font-size: 0.85rem">{{.Url}}</p>
 | 
			
		||||
  <a href="{{.BasePath}}/" class="btn btn-secondary btn-sm mt-3" style="width: 30rem; max-width: 100%">
 | 
			
		||||
  <button
 | 
			
		||||
    hx-get="{{.BasePath}}/home"
 | 
			
		||||
    hx-trigger="click"
 | 
			
		||||
    hx-target="#main-content"
 | 
			
		||||
    class="btn btn-secondary btn-sm mt-3"
 | 
			
		||||
    style="width: 30rem; max-width: 100%"
 | 
			
		||||
  >
 | 
			
		||||
    Download Another Video
 | 
			
		||||
  </a>
 | 
			
		||||
  </button>
 | 
			
		||||
</div>
 | 
			
		||||
 | 
			
		||||
{{$root := .}}
 | 
			
		||||
 | 
			
		||||
{{range $vidIndex, $video := .Videos}}
 | 
			
		||||
{{if not (eq $vidIndex 0)}}
 | 
			
		||||
{{$root := .}} {{range $vidIndex, $video := .Videos}} {{if not (eq $vidIndex
 | 
			
		||||
0)}}
 | 
			
		||||
<hr class="mt-5" />
 | 
			
		||||
{{end}}
 | 
			
		||||
<div class="d-flex flex-column flex-lg-row justify-content-center gap-5 mt-5">
 | 
			
		||||
  <div class="d-flex justify-content-center">
 | 
			
		||||
    <img src="{{.Meta.Thumbnail}}" alt="{{.Meta.Title}}" style="max-height: 25rem; max-width: 100%; margin: 0 auto;" />
 | 
			
		||||
    <img
 | 
			
		||||
      src="{{.Meta.Thumbnail}}"
 | 
			
		||||
      alt="{{.Meta.Title}}"
 | 
			
		||||
      style="max-height: 25rem; max-width: 100%; margin: 0 auto"
 | 
			
		||||
    />
 | 
			
		||||
  </div>
 | 
			
		||||
  <div class="downloads flex-lg-grow-1">
 | 
			
		||||
    {{range $index, $format := $video.Formats}}
 | 
			
		||||
    <div style="font-size: smaller">{{$format.Format}}</div>
 | 
			
		||||
    <div class="flex-grow-1 d-flex gap-3">
 | 
			
		||||
      <a class="btn btn-primary flex-grow-1" download="{{$root.Meta.ID}}-{{$format.Resolution}}.{{$format.Ext}}" P
 | 
			
		||||
        href="{{$format.Url}}">
 | 
			
		||||
      <a
 | 
			
		||||
        class="btn btn-primary flex-grow-1"
 | 
			
		||||
        download="{{$root.Meta.ID}}-{{$format.Resolution}}.{{$format.Ext}}"
 | 
			
		||||
        P
 | 
			
		||||
        href="{{$format.Url}}"
 | 
			
		||||
      >
 | 
			
		||||
        Download (direct)
 | 
			
		||||
      </a>
 | 
			
		||||
      <a class="btn btn-primary flex-grow-1" download="{{$root.Meta.ID}}-{{$format.Resolution}}.{{$format.Ext}}"
 | 
			
		||||
        href="{{$root.BasePath}}/download/proxy?url={{queryEscape $root.Url}}&format={{$format.FormatID}}&index={{$vidIndex}}">
 | 
			
		||||
      <a
 | 
			
		||||
        class="btn btn-primary flex-grow-1"
 | 
			
		||||
        download="{{$root.Meta.ID}}-{{$format.Resolution}}.{{$format.Ext}}"
 | 
			
		||||
        href="{{$root.BasePath}}/download/proxy?url={{queryEscape $root.Url}}&format={{$format.FormatID}}&index={{$vidIndex}}"
 | 
			
		||||
      >
 | 
			
		||||
        Download (proxied)
 | 
			
		||||
      </a>
 | 
			
		||||
    </div>
 | 
			
		||||
    {{end}}
 | 
			
		||||
  </div>
 | 
			
		||||
</div>
 | 
			
		||||
{{end}}
 | 
			
		||||
{{end}}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,32 +1,67 @@
 | 
			
		|||
<h1 class="text-center">YTDL Web</h1>
 | 
			
		||||
<p class="text-center mb-5">
 | 
			
		||||
  Download videos from over a thousand websites with the help of
 | 
			
		||||
  <a href="https://github.com/yt-dlp/yt-dlp">yt-dlp</a>,
 | 
			
		||||
  a fork of youtube-dl with more features and fixes.
 | 
			
		||||
  <a href="https://github.com/yt-dlp/yt-dlp">yt-dlp</a>, a fork of youtube-dl
 | 
			
		||||
  with more features and fixes.
 | 
			
		||||
  <br />
 | 
			
		||||
  View a complete list of supported websites
 | 
			
		||||
  <a href="https://github.com/yt-dlp/yt-dlp/blob/master/supportedsites.md">here</a>.
 | 
			
		||||
  <a href="https://github.com/yt-dlp/yt-dlp/blob/master/supportedsites.md"
 | 
			
		||||
    >here</a
 | 
			
		||||
  >.
 | 
			
		||||
</p>
 | 
			
		||||
 | 
			
		||||
<form action="{{.BasePath}}/download" method="get">
 | 
			
		||||
<form
 | 
			
		||||
  hx-get="{{.BasePath}}/download"
 | 
			
		||||
  hx-trigger="submit"
 | 
			
		||||
  hx-target="#main-content"
 | 
			
		||||
  hx-swap="innerHTML"
 | 
			
		||||
>
 | 
			
		||||
  <div class="mb-3">
 | 
			
		||||
    <label for="url" class="form-label visually-hidden">Url</label>
 | 
			
		||||
    <div class="input-group">
 | 
			
		||||
      <input type="url" name="url" id="url" class="form-control" required
 | 
			
		||||
        placeholder="Enter url here then click download" />
 | 
			
		||||
      <button id="paste-button" class="btn btn-outline-secondary" type="button" title="Paste">
 | 
			
		||||
        <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"
 | 
			
		||||
          style="width: 1.5rem; height: 1.5rem">
 | 
			
		||||
          <path stroke-linecap="round" stroke-linejoin="round"
 | 
			
		||||
            d="M15.666 3.888A2.25 2.25 0 0013.5 2.25h-3c-1.03 0-1.9.693-2.166 1.638m7.332 0c.055.194.084.4.084.612v0a.75.75 0 01-.75.75H9a.75.75 0 01-.75-.75v0c0-.212.03-.418.084-.612m7.332 0c.646.049 1.288.11 1.927.184 1.1.128 1.907 1.077 1.907 2.185V19.5a2.25 2.25 0 01-2.25 2.25H6.75A2.25 2.25 0 014.5 19.5V6.257c0-1.108.806-2.057 1.907-2.185a48.208 48.208 0 011.927-.184" />
 | 
			
		||||
      <input
 | 
			
		||||
        type="url"
 | 
			
		||||
        name="url"
 | 
			
		||||
        id="url"
 | 
			
		||||
        class="form-control"
 | 
			
		||||
        required
 | 
			
		||||
        placeholder="Enter url here then click download"
 | 
			
		||||
      />
 | 
			
		||||
      <button
 | 
			
		||||
        id="paste-button"
 | 
			
		||||
        class="btn btn-outline-secondary"
 | 
			
		||||
        type="button"
 | 
			
		||||
        title="Paste"
 | 
			
		||||
      >
 | 
			
		||||
        <svg
 | 
			
		||||
          xmlns="http://www.w3.org/2000/svg"
 | 
			
		||||
          fill="none"
 | 
			
		||||
          viewBox="0 0 24 24"
 | 
			
		||||
          stroke-width="1.5"
 | 
			
		||||
          stroke="currentColor"
 | 
			
		||||
          style="width: 1.5rem; height: 1.5rem"
 | 
			
		||||
        >
 | 
			
		||||
          <path
 | 
			
		||||
            stroke-linecap="round"
 | 
			
		||||
            stroke-linejoin="round"
 | 
			
		||||
            d="M15.666 3.888A2.25 2.25 0 0013.5 2.25h-3c-1.03 0-1.9.693-2.166 1.638m7.332 0c.055.194.084.4.084.612v0a.75.75 0 01-.75.75H9a.75.75 0 01-.75-.75v0c0-.212.03-.418.084-.612m7.332 0c.646.049 1.288.11 1.927.184 1.1.128 1.907 1.077 1.907 2.185V19.5a2.25 2.25 0 01-2.25 2.25H6.75A2.25 2.25 0 014.5 19.5V6.257c0-1.108.806-2.057 1.907-2.185a48.208 48.208 0 011.927-.184"
 | 
			
		||||
          />
 | 
			
		||||
        </svg>
 | 
			
		||||
      </button>
 | 
			
		||||
    </div>
 | 
			
		||||
  </div>
 | 
			
		||||
  <div class="d-grid">
 | 
			
		||||
    <button type="submit" class="btn btn-primary">Download</button>
 | 
			
		||||
    <button type="submit" class="btn btn-primary">
 | 
			
		||||
      Download
 | 
			
		||||
      <div
 | 
			
		||||
        class="spinner-border spinner-border-sm htmx-indicator ms-1"
 | 
			
		||||
        role="status"
 | 
			
		||||
      >
 | 
			
		||||
        <span class="visually-hidden">Loading...</span>
 | 
			
		||||
      </div>
 | 
			
		||||
    </button>
 | 
			
		||||
  </div>
 | 
			
		||||
  {{if .Flash}}
 | 
			
		||||
  <div class="alert alert-danger mt-4" role="" alert>{{.Flash.message}}</div>
 | 
			
		||||
  {{end}}
 | 
			
		||||
</form>
 | 
			
		||||
</form>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,63 +1,84 @@
 | 
			
		|||
<!DOCTYPE html>
 | 
			
		||||
<html lang="en" class="h-100">
 | 
			
		||||
 | 
			
		||||
<head>
 | 
			
		||||
  <meta charset="UTF-8" />
 | 
			
		||||
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
 | 
			
		||||
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
 | 
			
		||||
  <title>YTDL Web</title>
 | 
			
		||||
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"
 | 
			
		||||
    integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ" crossorigin="anonymous" />
 | 
			
		||||
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/toastr@2.1.4/build/toastr.min.css"
 | 
			
		||||
    integrity="sha256-R91pD48xW+oHbpJYGn5xR0Q7tMhH4xOrWn1QqMRINtA=" crossorigin="anonymous" />
 | 
			
		||||
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"
 | 
			
		||||
    integrity="sha384-ENjdO4Dr2bkBIFxQpeoTz1HIcje39Wm4jDKdf19U8gI4ddQ3GYNS7NTKfAdVQSZe" crossorigin="anonymous"
 | 
			
		||||
    defer></script>
 | 
			
		||||
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js" defer></script>
 | 
			
		||||
  <script src="https://cdn.jsdelivr.net/npm/toastr@2.1.4/build/toastr.min.js"
 | 
			
		||||
    integrity="sha256-Hgwq1OBpJ276HUP9H3VJkSv9ZCGRGQN+JldPJ8pNcUM=" crossorigin="anonymous" defer></script>
 | 
			
		||||
  <style>
 | 
			
		||||
    #toast-container>div {
 | 
			
		||||
      -moz-box-shadow: none !important;
 | 
			
		||||
      -webkit-box-shadow: none !important;
 | 
			
		||||
      box-shadow: none !important;
 | 
			
		||||
      -ms-filter: none;
 | 
			
		||||
      filter: none;
 | 
			
		||||
      opacity: 1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    .downloads {
 | 
			
		||||
      display: grid;
 | 
			
		||||
      grid-template-columns: minmax(auto, max-content) auto;
 | 
			
		||||
      gap: 1.5rem;
 | 
			
		||||
      align-items: center;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    .see-more-btn,
 | 
			
		||||
    .collapse {
 | 
			
		||||
      grid-column: span 2;
 | 
			
		||||
    }
 | 
			
		||||
  </style>
 | 
			
		||||
</head>
 | 
			
		||||
 | 
			
		||||
<body class="d-flex flex-column h-100" data-bs-theme="dark">
 | 
			
		||||
  <div class="flex-shrink-0">
 | 
			
		||||
    {{template "views/partials/navbar" .}}
 | 
			
		||||
    <main class="container my-5">{{embed}}</main>
 | 
			
		||||
  </div>
 | 
			
		||||
  {{template "views/partials/footer" .}}
 | 
			
		||||
  <script>
 | 
			
		||||
    const pasteButton = document.getElementById("paste-button");
 | 
			
		||||
    const urlField = document.getElementById("url");
 | 
			
		||||
    pasteButton.addEventListener("click", async () => {
 | 
			
		||||
      try {
 | 
			
		||||
        const text = await navigator.clipboard.readText();
 | 
			
		||||
        urlField.value = text;
 | 
			
		||||
      } catch (error) {
 | 
			
		||||
        toastr.error("Failed to paste url from clipboard.");
 | 
			
		||||
  <head>
 | 
			
		||||
    <meta charset="UTF-8" />
 | 
			
		||||
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
 | 
			
		||||
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
 | 
			
		||||
    <title>YTDL Web</title>
 | 
			
		||||
    <link
 | 
			
		||||
      href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css"
 | 
			
		||||
      rel="stylesheet"
 | 
			
		||||
      integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ"
 | 
			
		||||
      crossorigin="anonymous"
 | 
			
		||||
    />
 | 
			
		||||
    <link
 | 
			
		||||
      rel="stylesheet"
 | 
			
		||||
      href="https://cdn.jsdelivr.net/npm/toastr@2.1.4/build/toastr.min.css"
 | 
			
		||||
      integrity="sha256-R91pD48xW+oHbpJYGn5xR0Q7tMhH4xOrWn1QqMRINtA="
 | 
			
		||||
      crossorigin="anonymous"
 | 
			
		||||
    />
 | 
			
		||||
    <script
 | 
			
		||||
      src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"
 | 
			
		||||
      integrity="sha384-ENjdO4Dr2bkBIFxQpeoTz1HIcje39Wm4jDKdf19U8gI4ddQ3GYNS7NTKfAdVQSZe"
 | 
			
		||||
      crossorigin="anonymous"
 | 
			
		||||
      defer
 | 
			
		||||
    ></script>
 | 
			
		||||
    <script
 | 
			
		||||
      src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"
 | 
			
		||||
      defer
 | 
			
		||||
    ></script>
 | 
			
		||||
    <script
 | 
			
		||||
      src="https://cdn.jsdelivr.net/npm/toastr@2.1.4/build/toastr.min.js"
 | 
			
		||||
      integrity="sha256-Hgwq1OBpJ276HUP9H3VJkSv9ZCGRGQN+JldPJ8pNcUM="
 | 
			
		||||
      crossorigin="anonymous"
 | 
			
		||||
      defer
 | 
			
		||||
    ></script>
 | 
			
		||||
    <script
 | 
			
		||||
      src="https://cdn.jsdelivr.net/npm/htmx.org@1.9.4/dist/htmx.min.js"
 | 
			
		||||
      integrity="sha256-XIivRAE99i/eil5P31JNihaDSiix0V40rgmUrCfNTH4="
 | 
			
		||||
      crossorigin="anonymous"
 | 
			
		||||
    ></script>
 | 
			
		||||
    <style>
 | 
			
		||||
      #toast-container > div {
 | 
			
		||||
        -moz-box-shadow: none !important;
 | 
			
		||||
        -webkit-box-shadow: none !important;
 | 
			
		||||
        box-shadow: none !important;
 | 
			
		||||
        -ms-filter: none;
 | 
			
		||||
        filter: none;
 | 
			
		||||
        opacity: 1;
 | 
			
		||||
      }
 | 
			
		||||
    });
 | 
			
		||||
  </script>
 | 
			
		||||
</body>
 | 
			
		||||
 | 
			
		||||
</html>
 | 
			
		||||
      .downloads {
 | 
			
		||||
        display: grid;
 | 
			
		||||
        grid-template-columns: minmax(auto, max-content) auto;
 | 
			
		||||
        gap: 1.5rem;
 | 
			
		||||
        align-items: center;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      .see-more-btn,
 | 
			
		||||
      .collapse {
 | 
			
		||||
        grid-column: span 2;
 | 
			
		||||
      }
 | 
			
		||||
    </style>
 | 
			
		||||
  </head>
 | 
			
		||||
 | 
			
		||||
  <body class="d-flex flex-column h-100" data-bs-theme="dark">
 | 
			
		||||
    <div class="flex-shrink-0">
 | 
			
		||||
      {{template "views/partials/navbar" .}}
 | 
			
		||||
      <main id="main-content" class="container my-5">{{embed}}</main>
 | 
			
		||||
    </div>
 | 
			
		||||
    {{template "views/partials/footer" .}}
 | 
			
		||||
    <script>
 | 
			
		||||
      const pasteButton = document.getElementById("paste-button");
 | 
			
		||||
      const urlField = document.getElementById("url");
 | 
			
		||||
      pasteButton.addEventListener("click", async () => {
 | 
			
		||||
        try {
 | 
			
		||||
          const text = await navigator.clipboard.readText();
 | 
			
		||||
          urlField.value = text;
 | 
			
		||||
        } catch (error) {
 | 
			
		||||
          toastr.error("Failed to paste url from clipboard.");
 | 
			
		||||
        }
 | 
			
		||||
      });
 | 
			
		||||
    </script>
 | 
			
		||||
  </body>
 | 
			
		||||
</html>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue