Skip to content

Commit

Permalink
initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
sprsquish committed Jul 11, 2024
0 parents commit 989b32d
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Docker Image CI

on:
push:
branches: [ main ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: BuildX Install
uses: docker/setup-buildx-action@v3
id: buildx
with:
install: true

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository }}
tags: |
type=raw,value=latest,enable={{is_default_branch}}
type=sha,format=long,prefix=
- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM --platform=$BUILDPLATFORM golang:1.22 as builder
ARG TARGETOS
ARG TARGETARCH

WORKDIR "/app"
COPY index.html index.html
COPY server.go server.go
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
go build -o server server.go

FROM jauderho/yt-dlp:latest
WORKDIR "/app"
COPY --from=builder /app/server .

ENTRYPOINT []
CMD "/app/server"
27 changes: 27 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<html>
<head>
<style>
body {
text-align: center;
}
form {
margin-top: 10%;
margin-left: 15%;
margin-right:15%;
width: 70%;
}
input {
font-size: xx-large;
}
input[type=text] {
width: 50%;
}
</style>
</head>
<body>
<form action="" method="post">
<input type="text" name="link" />
<input type="submit" value="Download" />
</form>
</body>
</html>
69 changes: 69 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
_ "embed"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
)

//go:embed index.html
var index []byte

func main() {
cliCmd := append(
strings.Split(os.Getenv("EXEC"), " "),
"-x",
"--audio-format",
"mp3",
"--audio-quality",
"0")

bind := os.Getenv("BIND")

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
w.Write(index)
return
}

cli := append(cliCmd, r.FormValue("link"))
cmd := exec.Command(cli[0], cli[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
_ = cmd.Run()

var file string
_ = filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if matched, err := filepath.Match("*.mp3", filepath.Base(path)); err != nil {
return err
} else if matched {
file = path
}
return nil
})

f, _ := os.Open(file)
defer func() {
f.Close()
os.Remove(file)
}()

w.Header().Set("Content-Type", "audio/mpeg")
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment;filename="%s"`, filepath.Base(file)))
w.WriteHeader(http.StatusOK)
io.Copy(w, f)
})

http.ListenAndServe(bind, nil)
}

0 comments on commit 989b32d

Please sign in to comment.