Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New endpoint for enabling video download of Cloudfare videos #411

Merged
merged 5 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions routes/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,64 @@ func (fes *APIServer) GetVideoStatus(ww http.ResponseWriter, req *http.Request)
}
}

type EnableVideoDownloadResponse struct {
Default map[string]interface{}
}

// Cloudflare does NOT allow uploaded videos to be downloaded (just streamed)
// Cloudflare allows adding download support on a per-video basis
// EnableVideoDownload enables download support for an already uploaded video
// See Cloudflare documentation here: https://developers.cloudflare.com/stream/viewing-videos/download-videos/
func (fes *APIServer) EnableVideoDownload(ww http.ResponseWriter, req *http.Request) {
if fes.Config.CloudflareStreamToken == "" || fes.Config.CloudflareAccountId == "" {
_AddBadRequestError(ww, fmt.Sprintf("EnableVideoDownload: This node is not configured to support video uploads"))
return
}
vars := mux.Vars(req)
videoId, videoIdExists := vars["videoId"]
if !videoIdExists {
_AddBadRequestError(ww, fmt.Sprintf("EnableVideoDownload: Missing videoId"))
return
}
url := fmt.Sprintf("https://api.cloudflare.com/client/v4/accounts/%v/stream/%v/downloads", fes.Config.CloudflareAccountId, videoId)
client := &http.Client{}

// This is a POST request because:
// - If video downloading is not enabled for the video, the POST request will enable it and return the video URL
// - If video downloading is already enabled for the video, the POST request will just return the video URL
request, err := http.NewRequest("POST", url, nil)
request.Header.Add("Authorization", fmt.Sprintf("Bearer %v", fes.Config.CloudflareStreamToken))
request.Header.Add("Content-Type", "application/json")
resp, err := client.Do(request)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf(
"EnableVideoDownload: error performing POST request: %v", err))
return
}
if resp.StatusCode != 200 {
_AddBadRequestError(ww, fmt.Sprintf("EnableVideoDownload: POST request did not return 200 status code but instead a status code of %v", resp.StatusCode))
return
}
cfVideoDetailsResponse := &CFVideoDetailsResponse{}
if err = json.NewDecoder(resp.Body).Decode(&cfVideoDetailsResponse); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("EnableVideoDownload: failed decoding body: %v", err))
return
}
if err = resp.Body.Close(); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("EnableVideoDownload: failed closing body: %v", err))
return
}
defaultVideo, _ := cfVideoDetailsResponse.Result["default"]

res := &EnableVideoDownloadResponse{
Default: defaultVideo.(map[string]interface{}),
}
if err = json.NewEncoder(ww).Encode(res); err != nil {
_AddInternalServerError(ww, fmt.Sprintf("EnableVideoDownload: Problem serializing object to JSON: %v", err))
return
}
}

type CFVideoOEmbedResponse struct {
Height uint64 `json:"height"`
Width uint64 `json:"width"`
Expand Down
18 changes: 13 additions & 5 deletions routes/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,12 @@ const (
RoutePathGetAcceptedBidHistory = "/api/v0/accepted-bid-history"

// media.go
RoutePathUploadImage = "/api/v0/upload-image"
RoutePathGetFullTikTokURL = "/api/v0/get-full-tiktok-url"
RoutePathUploadVideo = "/api/v0/upload-video"
RoutePathGetVideoStatus = "/api/v0/get-video-status"
RoutePathGetVideoDimensions = "/api/v0/get-video-dimensions"
RoutePathUploadImage = "/api/v0/upload-image"
RoutePathGetFullTikTokURL = "/api/v0/get-full-tiktok-url"
RoutePathUploadVideo = "/api/v0/upload-video"
RoutePathGetVideoStatus = "/api/v0/get-video-status"
RoutePathGetVideoDimensions = "/api/v0/get-video-dimensions"
RoutePathEnableVideoDownload = "/api/v0/enable-video-download"

// message.go
RoutePathSendMessageStateless = "/api/v0/send-message-stateless"
Expand Down Expand Up @@ -1852,6 +1853,12 @@ func (fes *APIServer) NewRouter() *muxtrace.Router {
fes.GetVideoStatus,
PublicAccess,
},
{
"EnableVideoDownload",
[]string{"POST", "OPTIONS"},
RoutePathEnableVideoDownload,
fes.EnableVideoDownload,
},
{
"GetVideoDimensions",
[]string{"GET"},
Expand Down Expand Up @@ -2129,6 +2136,7 @@ func Logger(inner http.Handler, name string) http.Handler {
var publicRoutes = map[string]interface{}{
RoutePathGetJumioStatusForPublicKey: nil,
RoutePathUploadVideo: nil,
RoutePathEnableVideoDownload: nil,
RoutePathGetReferralInfoForReferralHash: nil,
RoutePathGetReferralInfoForUser: nil,
RoutePathGetVerifiedUsernames: nil,
Expand Down