-
Notifications
You must be signed in to change notification settings - Fork 0
/
ytdownload.go
37 lines (30 loc) · 1007 Bytes
/
ytdownload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package handlers
import (
"context"
"github.com/lmika/broadtail/models"
"github.com/lmika/broadtail/services/videodownload"
"net/http"
"github.com/lmika/broadtail/middleware/errhandler"
"github.com/lmika/broadtail/services/jobsmanager"
)
type youTubeDownloadHandlers struct {
videoDownloadService *videodownload.Service
jobsManager *jobsmanager.JobsManager
}
func (ytdl *youTubeDownloadHandlers) CreateDownloadJob() http.Handler {
return errhandler.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) (err error) {
youtubeId := r.FormValue("video_ref")
if youtubeId == "" {
return errhandler.Errorf(http.StatusBadRequest, "missing YouTube ID")
}
videoRef, err := models.ParseVideoRef(youtubeId)
if err != nil {
return errhandler.Wrap(err, http.StatusBadRequest)
}
if err := ytdl.videoDownloadService.QueueForDownload(ctx, videoRef, nil); err != nil {
return err
}
http.Redirect(w, r, "/", http.StatusSeeOther)
return nil
})
}