This commit is contained in:
Tyrel Souza 2014-05-29 14:53:10 -04:00
parent eda5c25af2
commit 77adf17622

View File

@ -3,40 +3,38 @@ require 'tempfile'
class AmazonCopy
def download_video videolisting
file = Tempfile.new ["video_",".mp4"], "#{Rails.root}/tmp"
begin
file.write HTTParty.get(videolisting.get_video_url).parsed_response.force_encoding("utf-8")
file.flush
puts "File #{videolisting.videocode} downloaded."
if not videolisting.on_s3
download_file_and_upload_to_s3 videolisting.get_video_url,
videolisting.videocode,
"mp4",
"bvideos"
s3 = AWS::S3.new
obj = s3.buckets[ENV['VIDEO_BUCKET']].objects["bvideos/#{videolisting.videocode}.mp4"]
obj.write(file)
puts "File #{videolisting.videocode} uploaded."
download_file_and_upload_to_s3 videolisting.get_preview_url,
videolisting.videocode,
"mp4",
"bvideos_preview"
ensure
file.close
file.unlink
videolisting.on_s3 = true
videolisting.save
end
file = Tempfile.new ["video_",".jpg"], "#{Rails.root}/tmp"
begin
file.write HTTParty.get(videolisting.get_preview_url).parsed_response.force_encoding("utf-8")
file.flush
puts "File #{videolisting.videocode} preview downloaded."
s3 = AWS::S3.new
obj = s3.buckets[ENV['VIDEO_BUCKET']].objects["bvideos_previews/#{videolisting.videocode}.jpg"]
obj.write(file)
puts "File #{videolisting.videocode} preview uploaded."
ensure
file.close
file.unlink
end
videolisting.on_s3 = true
videolisting.save
end
def download_file_and_upload_to_s3 url, videocode, extension, prefix
file = Tempfile.new ["video_", ".#{extension}"], "#{Rails.root}/tmp"
begin
file.write HTTParty.get(url).parsed_response.force_encoding("utf-8")
file.flush
s3 = AWS::S3.new
obj = s3.buckets[ENV['VIDEO_BUCKET']].objects["#{prefix}/#{videocode}.mp4"]
obj.write(file)
ensure
file.close
file.unlink
end
end
handle_asynchronously :download_video
end