Class: YouTube::UploadService

Inherits:
Object
  • Object
show all
Defined in:
app/services/youtube/upload_service.rb

Overview

Uploads a Video to YouTube from Cloudflare Stream or local Dragonfly attachment.

Downloads the MP4 to a temp file, then uploads via the YouTube Data API.
Handles both Cloudflare-hosted and locally-hosted videos.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account: nil, client: nil) ⇒ UploadService

Returns a new instance of UploadService.



15
16
17
18
# File 'app/services/youtube/upload_service.rb', line 15

def initialize(account: nil, client: nil)
  @client = client || YouTube::ApiClient.new(account: )
  @logger = Rails.logger
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



13
14
15
# File 'app/services/youtube/upload_service.rb', line 13

def client
  @client
end

#loggerObject (readonly)

Returns the value of attribute logger.



13
14
15
# File 'app/services/youtube/upload_service.rb', line 13

def logger
  @logger
end

Instance Method Details

#upload(video, privacy_status: 'private') ⇒ Video

Upload a video to YouTube.

Parameters:

  • video (Video)

    the local Video record

  • privacy_status (String) (defaults to: 'private')

    'public', 'unlisted', or 'private'

Returns:

  • (Video)

    the updated video with youtube_id set



24
25
26
27
28
29
30
31
32
33
# File 'app/services/youtube/upload_service.rb', line 24

def upload(video, privacy_status: 'private')
  validate_uploadable!(video)

  video.update_columns(youtube_upload_status: 'uploading')

  download_and_upload(video, privacy_status)
rescue StandardError
  video.update_columns(youtube_upload_status: 'failed')
  raise
end