Class: CloudflareStreamApi
- Inherits:
-
Object
- Object
- CloudflareStreamApi
- Includes:
- Singleton
- Defined in:
- app/services/cloudflare_stream_api.rb
Overview
Service object: cloudflare stream api.
Constant Summary collapse
- ACCOUNT_ID =
Account id.
'79b7f58cf035093b5ad11747df30369a'- BASE_URL =
URL for base.
'https://api.cloudflare.com/client/v4'- STREAM_BASE_URL =
URL for stream base.
"#{BASE_URL}/accounts/#{ACCOUNT_ID}/stream".freeze
Instance Method Summary collapse
-
#copy_from_url(url, meta: {}) ⇒ Hash
Upload a video from a URL to Cloudflare Stream (URL copy).
-
#delete_captions(cloudflare_uid, language) ⇒ Object
Delete captions for a video and language.
-
#delete_mp4_download(cloudflare_uid, download_type) ⇒ Object
Delete specific type of video download for a video.
-
#delete_video(cloudflare_uid) ⇒ Object
Delete a video from Cloudflare Stream.
-
#enable_mp4_download(cloudflare_uid, download_type) ⇒ Object
Enable specific type of video download for a video.
-
#enable_mp4_downloads(cloudflare_uid) ⇒ Object
Enable video downloads for a video (legacy method - creates default download).
-
#get_downloads(cloudflare_uid) ⇒ Object
Get downloads information for a video.
-
#get_poster_url(cloudflare_uid) ⇒ Object
Get poster/thumbnail URL for a video.
-
#get_video(cloudflare_uid) ⇒ Object
Get video details from Cloudflare Stream.
-
#get_vtt_captions(cloudflare_uid, language) ⇒ Object
Get VTT captions content.
-
#initialize ⇒ CloudflareStreamApi
constructor
A new instance of CloudflareStreamApi.
-
#initiate_tus_upload(upload_length:, upload_metadata:) ⇒ Hash
Initiate a TUS resumable upload via the Cloudflare Stream direct-user endpoint.
-
#list_captions(cloudflare_uid) ⇒ Object
List captions for a video.
-
#upload_captions(cloudflare_uid, language, vtt_file_path) ⇒ Object
Upload VTT captions to a video.
Constructor Details
#initialize ⇒ CloudflareStreamApi
Returns a new instance of CloudflareStreamApi.
14 15 16 |
# File 'app/services/cloudflare_stream_api.rb', line 14 def initialize @token = CF_STREAM_AND_IMAGE_TOKEN end |
Instance Method Details
#copy_from_url(url, meta: {}) ⇒ Hash
Upload a video from a URL to Cloudflare Stream (URL copy).
Cloudflare will fetch and process the video asynchronously.
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'app/services/cloudflare_stream_api.rb', line 186 def copy_from_url(url, meta: {}) payload = { url: url, meta: }.compact response = make_request(:post, "#{STREAM_BASE_URL}/copy", payload.to_json) if response&.success? body = JSON.parse(response.body) result = body['result'] { success: true, uid: result['uid'], data: result } else error = response ? response.body : 'no response' { success: false, error: error } end rescue StandardError => e Rails.logger.error "CloudflareStreamApi#copy_from_url failed: #{e.}" { success: false, error: e. } end |
#delete_captions(cloudflare_uid, language) ⇒ Object
Delete captions for a video and language
130 131 132 133 134 135 136 137 138 |
# File 'app/services/cloudflare_stream_api.rb', line 130 def delete_captions(cloudflare_uid, language) endpoint = "#{STREAM_BASE_URL}/#{cloudflare_uid}/captions/#{language}" response = make_request(:delete, endpoint) response&.success? rescue StandardError => e Rails.logger.error "Failed to delete captions for video #{cloudflare_uid}: #{e.}" false end |
#delete_mp4_download(cloudflare_uid, download_type) ⇒ Object
Delete specific type of video download for a video
55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/services/cloudflare_stream_api.rb', line 55 def delete_mp4_download(cloudflare_uid, download_type) raise ArgumentError, "Invalid download type: #{download_type}" unless %w[default audio].include?(download_type) endpoint = "#{STREAM_BASE_URL}/#{cloudflare_uid}/downloads/#{download_type}" response = make_request(:delete, endpoint) response&.success? rescue StandardError => e Rails.logger.error "Failed to delete #{download_type} download for video #{cloudflare_uid}: #{e.}" false end |
#delete_video(cloudflare_uid) ⇒ Object
Delete a video from Cloudflare Stream
204 205 206 207 208 209 210 211 212 |
# File 'app/services/cloudflare_stream_api.rb', line 204 def delete_video(cloudflare_uid) endpoint = "#{STREAM_BASE_URL}/#{cloudflare_uid}" response = make_request(:delete, endpoint) response&.success? rescue StandardError => e Rails.logger.error "Failed to delete Cloudflare video #{cloudflare_uid}: #{e.}" false end |
#enable_mp4_download(cloudflare_uid, download_type) ⇒ Object
Enable specific type of video download for a video
40 41 42 43 44 45 46 47 48 49 50 |
# File 'app/services/cloudflare_stream_api.rb', line 40 def enable_mp4_download(cloudflare_uid, download_type) raise ArgumentError, "Invalid download type: #{download_type}" unless %w[default audio].include?(download_type) endpoint = "#{STREAM_BASE_URL}/#{cloudflare_uid}/downloads/#{download_type}" response = make_request(:post, endpoint) response&.success? rescue StandardError => e Rails.logger.error "Failed to enable #{download_type} download for video #{cloudflare_uid}: #{e.}" false end |
#enable_mp4_downloads(cloudflare_uid) ⇒ Object
Enable video downloads for a video (legacy method - creates default download)
33 34 35 |
# File 'app/services/cloudflare_stream_api.rb', line 33 def enable_mp4_downloads(cloudflare_uid) enable_mp4_download(cloudflare_uid, 'default') end |
#get_downloads(cloudflare_uid) ⇒ Object
Get downloads information for a video
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'app/services/cloudflare_stream_api.rb', line 68 def get_downloads(cloudflare_uid) endpoint = "#{STREAM_BASE_URL}/#{cloudflare_uid}/downloads" response = make_request(:get, endpoint) return nil unless response&.success? data = JSON.parse(response.body) data['success'] ? data['result'] : nil rescue StandardError => e Rails.logger.error "Failed to get downloads for video #{cloudflare_uid}: #{e.}" nil end |
#get_poster_url(cloudflare_uid) ⇒ Object
Get poster/thumbnail URL for a video
82 83 84 |
# File 'app/services/cloudflare_stream_api.rb', line 82 def get_poster_url(cloudflare_uid) "#{CF_STREAM_URL}/#{cloudflare_uid}/thumbnails/thumbnail.jpg" end |
#get_video(cloudflare_uid) ⇒ Object
Get video details from Cloudflare Stream
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'app/services/cloudflare_stream_api.rb', line 19 def get_video(cloudflare_uid) endpoint = "#{STREAM_BASE_URL}/#{cloudflare_uid}" response = make_request(:get, endpoint) return nil unless response&.success? data = JSON.parse(response.body) data['success'] ? data['result'] : nil rescue StandardError => e Rails.logger.error "Failed to get Cloudflare video #{cloudflare_uid}: #{e.}" nil end |
#get_vtt_captions(cloudflare_uid, language) ⇒ Object
Get VTT captions content
141 142 143 144 145 146 147 148 149 |
# File 'app/services/cloudflare_stream_api.rb', line 141 def get_vtt_captions(cloudflare_uid, language) endpoint = "#{STREAM_BASE_URL}/#{cloudflare_uid}/captions/#{language}/vtt" response = make_request(:get, endpoint) response&.success? ? response.body : nil rescue StandardError => e Rails.logger.error "Failed to get VTT captions for video #{cloudflare_uid}: #{e.}" nil end |
#initiate_tus_upload(upload_length:, upload_metadata:) ⇒ Hash
Initiate a TUS resumable upload via the Cloudflare Stream direct-user endpoint.
Returns the Location header URL that the client should resume uploading to.
Called by the controller's +cf_upload+ action so that no Cloudflare API
details leak into application controllers.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'app/services/cloudflare_stream_api.rb', line 160 def initiate_tus_upload(upload_length:, upload_metadata:) conn = Faraday.new(url: "#{STREAM_BASE_URL}?direct_user=true") do |f| f.headers['Authorization'] = "bearer #{@token}" f.headers['Tus-Resumable'] = '1.0.0' f.headers['Upload-Length'] = upload_length.to_s f.headers['Upload-Metadata'] = .to_s f.headers['Content-Type'] = 'application/json' f.adapter Faraday.default_adapter end response = conn.post if response.success? { success: true, location: response.headers['Location'] } else { success: false, error: response.body } end rescue Faraday::Error => e Rails.logger.error "CloudflareStreamApi#initiate_tus_upload failed: #{e.}" { success: false, error: e. } end |
#list_captions(cloudflare_uid) ⇒ Object
List captions for a video
116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'app/services/cloudflare_stream_api.rb', line 116 def list_captions(cloudflare_uid) endpoint = "#{STREAM_BASE_URL}/#{cloudflare_uid}/captions" response = make_request(:get, endpoint) return [] unless response&.success? data = JSON.parse(response.body) data['success'] ? data['result'] : [] rescue StandardError => e Rails.logger.error "Failed to list captions for video #{cloudflare_uid}: #{e.}" [] end |
#upload_captions(cloudflare_uid, language, vtt_file_path) ⇒ Object
Upload VTT captions to a video
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'app/services/cloudflare_stream_api.rb', line 87 def upload_captions(cloudflare_uid, language, vtt_file_path) endpoint = "#{STREAM_BASE_URL}/#{cloudflare_uid}/captions/#{language}" # Create multipart form data using proper multipart handling conn = Faraday.new(url: endpoint) do |faraday| faraday.headers['Authorization'] = "Bearer #{@token}" faraday.request :multipart faraday.request :url_encoded faraday.adapter Faraday.default_adapter end payload = { file: Faraday::UploadIO.new(vtt_file_path, 'text/vtt') } response = conn.put do |req| req.body = payload end return nil unless response&.success? data = JSON.parse(response.body) data['success'] ? data['result'] : nil rescue StandardError => e Rails.logger.error "Failed to upload captions for video #{cloudflare_uid}: #{e.}" nil end |