Skip to content

Commit

Permalink
Fix version display bug
Browse files Browse the repository at this point in the history
  • Loading branch information
rroller committed Oct 21, 2024
1 parent 5fd2d64 commit 1a5f808
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
46 changes: 24 additions & 22 deletions src/media/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ type Media struct {
HumanSize string
}

type Results struct {
Medias []Media
Url string
ErrorMessage string
}

var fetchIndexTmpl = template.Must(template.ParseFiles("templates/media/index.html"))

// Where the media files are saved. Always has a trailing slash
Expand All @@ -55,37 +49,49 @@ func Index(w http.ResponseWriter, _ *http.Request) {
}

func FetchMedia(w http.ResponseWriter, r *http.Request) {
response, err := getMediaResults(r)
url := getUrl(r)

media, ytdlpErrorMessage, err := getMediaResults(url)
data := map[string]interface{}{
"url": url,
"media": media,
"error": ytdlpErrorMessage,
"ytDlpVersion": CachedYtDlpVersion,
}
if err != nil {
_ = fetchIndexTmpl.Execute(w, response)
_ = fetchIndexTmpl.Execute(w, data)
return
}

if err := fetchIndexTmpl.Execute(w, response); err != nil {
if err := fetchIndexTmpl.Execute(w, data); err != nil {
log.Error().Msgf("Error rendering template: %v", err)
http.Error(w, "Internal error", http.StatusInternalServerError)
}
}

func FetchMediaApi(w http.ResponseWriter, r *http.Request) {
response, err := getMediaResults(r)
url := getUrl(r)
medias, _, err := getMediaResults(url)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if len(response.Medias) == 0 {
if len(medias) == 0 {
http.Error(w, "Media not found", http.StatusBadRequest)
return
}

// just take the first one
streamFileToClientById(w, r, response.Medias[0].Id)
streamFileToClientById(w, r, medias[0].Id)
}

func getUrl(r *http.Request) string {
return strings.TrimSpace(r.URL.Query().Get("url"))
}

func getMediaResults(r *http.Request) (Results, error) {
url := r.URL.Query().Get("url")
func getMediaResults(url string) ([]Media, string, error) {
if url == "" {
return Results{ErrorMessage: "Missing URL"}, errors.New("missing URL")
return nil, "", errors.New("missing URL")
}

// NOTE: This system is for a simple use case, meant to run at home. This is not a great design for a robust system.
Expand All @@ -95,24 +101,20 @@ func getMediaResults(r *http.Request) (Results, error) {
id := GetMD5Hash(url)
// Look to see if we already have the media on disk
medias, err := getAllFilesForId(id)
result := Results{Url: url}
if len(medias) == 0 {
// We don't, so go fetch it
errMessage := ""
id, errMessage, err = downloadMedia(url)
if err != nil {
result.ErrorMessage = errMessage
return result, err
return nil, errMessage, err
}
medias, err = getAllFilesForId(id)
if err != nil {
result.ErrorMessage = err.Error()
return result, err
return nil, "", err
}
}

result.Medias = medias
return result, nil
return medias, "", nil
}

// returns the ID of the file, and error message, and an error
Expand Down
10 changes: 5 additions & 5 deletions templates/media/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ <h1 class="display-4"><a class="text-light" href="/">media roller</a></h1>
<form action="/fetch" method="GET">
<div class="input-group">
<input name="url" type="url" class="form-control" placeholder="URL" aria-label="URL"
aria-describedby="button-submit" value="{{.Url}}" autofocus>
aria-describedby="button-submit" value="{{.url}}" autofocus>
<div class="input-group-append">
<button class="btn btn-primary" type="submit" id="button-submit">Submit</button>
</div>
</div>
</form>
{{ if .Medias }}
{{ if .media }}
<div style="margin-top:20px;">
<h2>Done!</h2>
{{range .Medias}}
{{range .media}}
<a style="color: dodgerblue" href="/download?id={{.Id}}">{{.Name}}</a> <small>{{.HumanSize}}</small>
{{ end }}
</div>
{{ end }}
{{ if .ErrorMessage }}
{{ if .error }}
<h4 style="margin-top:20px;">Error</h4>
<pre style="background-color:white;text-align:left;white-space: pre-wrap;">{{ .ErrorMessage }}</pre>
<pre style="background-color:white;text-align:left;white-space: pre-wrap;">{{ .error }}</pre>
{{ end }}
</div>
</div>
Expand Down

0 comments on commit 1a5f808

Please sign in to comment.