Advertisement
Guest User

video_transcoding_worker.rb

a guest
Sep 25th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.82 KB | None | 0 0
  1. # This worker's purpose is to get a transcoded / minified version of a video
  2. # from the Filestack API for display on discussion pages
  3. # See Filestack API docs: https://www.filestack.com/docs/api/processing/#audio-and-video
  4.  
  5. module FilestackUploadWorkers
  6.    
  7.   class VideoTranscodingWorker
  8.     include Sidekiq::Worker
  9.     sidekiq_options unique: :until_timeout, unique_job_expiration: 60 * 60 * 24
  10.  
  11.     attr_reader :resource_handle,:attachment_id,:attachment_class,:opts
  12.  
  13.     def perform(attachment_id, attachment_class, resource_handle)
  14.       puts "VideoTranscodingWorker.perform: attachment_id, attachment_class, resource_handle #{attachment_id}, #{attachment_class}, #{resource_handle}"
  15.       @attachment_id = attachment_id
  16.       @attachment_class = attachment_class
  17.       @resource_handle = resource_handle
  18.  
  19.       # Poll to see if the video transcoding is completed & update the record; otherwise reschedule
  20.       poll_for_complete || reschedule_worker
  21.     end  
  22.    
  23.     # Find the discussion attachment by ID
  24.     def discussion_attachment
  25.       @discussion_attachment ||= attachment_class.find(attachment_id)
  26.     end
  27.  
  28.  
  29.     # Poll to Filestack transcoding API and return JSON response
  30.     # TODO: Test me - post resource for transcoding
  31.     def post_to_transcode
  32.       puts "VideoTranscodingWorker.post_to_transcode"
  33.       max_width, max_height = [600, 400]
  34.       base_url = 'https://cdn.filestackcontent.com'
  35.       conversion = "video_convert=width:#{max_width},height:#{max_height}"
  36.       uri = URI.parse([base_url, conversion, resource_handle].join('/'))
  37.       req = Net::HTTP::Get.new(uri)
  38.       res = Net::HTTP.start(uri.hostname, uri.port) {|http| http.request(req) }
  39.       res.body
  40.     end
  41.  
  42.     # Is the returned JSON "data" field empty?
  43.     def completed?(response)
  44.       puts "VideoTranscodingWorker.completed?"
  45.       JSON.parse(response)["data"].empty? ? false : true
  46.     end
  47.  
  48.     # Reschedule the worker to poll again in 1 minute
  49.     def reschedule_worker
  50.       puts "VideoTranscodingWorker.reschedule_worker"
  51.       VideoTranscodingWorker.perform_in(1.minute, attachment_class, attachment_id)
  52.     end
  53.  
  54.     # Test the transcoding is complete. If so, save the new compressed_version_url.
  55.     # Return false if returned JSON data is still empty
  56.     def poll_for_complete
  57.       puts "VideoTranscodingWorker.poll_for_complete"
  58.       res = post_to_transcode
  59.       puts "completed?(res):"
  60.       if completed?(res)
  61.         puts "TRUE"
  62.         update_compressed_url(res)
  63.         return true
  64.       else
  65.         puts "FALSE"
  66.         return false
  67.       end
  68.     end
  69.    
  70.     # Update the attachment record with the new compressed_version_url
  71.     def update_compressed_url(response)
  72.       puts "VideoTranscodingWorker.update_compressed_url"
  73.       new_url = response['data']['url']
  74.       discussion_attachment.update_attribute(compressed_version_url: new_url)
  75.     end
  76.  
  77.   end
  78.  
  79. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement