Fix version display bug
This commit is contained in:
parent
5fd2d648dd
commit
1a5f808f27
2 changed files with 29 additions and 27 deletions
|
|
@ -32,12 +32,6 @@ type Media struct {
|
||||||
HumanSize string
|
HumanSize string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Results struct {
|
|
||||||
Medias []Media
|
|
||||||
Url string
|
|
||||||
ErrorMessage string
|
|
||||||
}
|
|
||||||
|
|
||||||
var fetchIndexTmpl = template.Must(template.ParseFiles("templates/media/index.html"))
|
var fetchIndexTmpl = template.Must(template.ParseFiles("templates/media/index.html"))
|
||||||
|
|
||||||
// Where the media files are saved. Always has a trailing slash
|
// Where the media files are saved. Always has a trailing slash
|
||||||
|
|
@ -55,37 +49,49 @@ func Index(w http.ResponseWriter, _ *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func FetchMedia(w http.ResponseWriter, r *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 {
|
if err != nil {
|
||||||
_ = fetchIndexTmpl.Execute(w, response)
|
_ = fetchIndexTmpl.Execute(w, data)
|
||||||
return
|
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)
|
log.Error().Msgf("Error rendering template: %v", err)
|
||||||
http.Error(w, "Internal error", http.StatusInternalServerError)
|
http.Error(w, "Internal error", http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func FetchMediaApi(w http.ResponseWriter, r *http.Request) {
|
func FetchMediaApi(w http.ResponseWriter, r *http.Request) {
|
||||||
response, err := getMediaResults(r)
|
url := getUrl(r)
|
||||||
|
medias, _, err := getMediaResults(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if len(response.Medias) == 0 {
|
if len(medias) == 0 {
|
||||||
http.Error(w, "Media not found", http.StatusBadRequest)
|
http.Error(w, "Media not found", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// just take the first one
|
// just take the first one
|
||||||
streamFileToClientById(w, r, response.Medias[0].Id)
|
streamFileToClientById(w, r, medias[0].Id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getMediaResults(r *http.Request) (Results, error) {
|
func getUrl(r *http.Request) string {
|
||||||
url := r.URL.Query().Get("url")
|
return strings.TrimSpace(r.URL.Query().Get("url"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func getMediaResults(url string) ([]Media, string, error) {
|
||||||
if url == "" {
|
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.
|
// NOTE: This system is for a simple use case, meant to run at home. This is not a great design for a robust system.
|
||||||
|
|
@ -95,24 +101,20 @@ func getMediaResults(r *http.Request) (Results, error) {
|
||||||
id := GetMD5Hash(url)
|
id := GetMD5Hash(url)
|
||||||
// Look to see if we already have the media on disk
|
// Look to see if we already have the media on disk
|
||||||
medias, err := getAllFilesForId(id)
|
medias, err := getAllFilesForId(id)
|
||||||
result := Results{Url: url}
|
|
||||||
if len(medias) == 0 {
|
if len(medias) == 0 {
|
||||||
// We don't, so go fetch it
|
// We don't, so go fetch it
|
||||||
errMessage := ""
|
errMessage := ""
|
||||||
id, errMessage, err = downloadMedia(url)
|
id, errMessage, err = downloadMedia(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
result.ErrorMessage = errMessage
|
return nil, errMessage, err
|
||||||
return result, err
|
|
||||||
}
|
}
|
||||||
medias, err = getAllFilesForId(id)
|
medias, err = getAllFilesForId(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
result.ErrorMessage = err.Error()
|
return nil, "", err
|
||||||
return result, err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
result.Medias = medias
|
return medias, "", nil
|
||||||
return result, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns the ID of the file, and error message, and an error
|
// returns the ID of the file, and error message, and an error
|
||||||
|
|
|
||||||
|
|
@ -18,23 +18,23 @@
|
||||||
<form action="/fetch" method="GET">
|
<form action="/fetch" method="GET">
|
||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
<input name="url" type="url" class="form-control" placeholder="URL" aria-label="URL"
|
<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">
|
<div class="input-group-append">
|
||||||
<button class="btn btn-primary" type="submit" id="button-submit">Submit</button>
|
<button class="btn btn-primary" type="submit" id="button-submit">Submit</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
{{ if .Medias }}
|
{{ if .media }}
|
||||||
<div style="margin-top:20px;">
|
<div style="margin-top:20px;">
|
||||||
<h2>Done!</h2>
|
<h2>Done!</h2>
|
||||||
{{range .Medias}}
|
{{range .media}}
|
||||||
<a style="color: dodgerblue" href="/download?id={{.Id}}">{{.Name}}</a> <small>{{.HumanSize}}</small>
|
<a style="color: dodgerblue" href="/download?id={{.Id}}">{{.Name}}</a> <small>{{.HumanSize}}</small>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</div>
|
</div>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ if .ErrorMessage }}
|
{{ if .error }}
|
||||||
<h4 style="margin-top:20px;">Error</h4>
|
<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 }}
|
{{ end }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue