refactor
This commit is contained in:
parent
eda5c25af2
commit
77adf17622
@ -3,40 +3,38 @@ require 'tempfile'
|
|||||||
|
|
||||||
class AmazonCopy
|
class AmazonCopy
|
||||||
def download_video videolisting
|
def download_video videolisting
|
||||||
file = Tempfile.new ["video_",".mp4"], "#{Rails.root}/tmp"
|
if not videolisting.on_s3
|
||||||
begin
|
download_file_and_upload_to_s3 videolisting.get_video_url,
|
||||||
file.write HTTParty.get(videolisting.get_video_url).parsed_response.force_encoding("utf-8")
|
videolisting.videocode,
|
||||||
file.flush
|
"mp4",
|
||||||
puts "File #{videolisting.videocode} downloaded."
|
"bvideos"
|
||||||
|
|
||||||
s3 = AWS::S3.new
|
download_file_and_upload_to_s3 videolisting.get_preview_url,
|
||||||
obj = s3.buckets[ENV['VIDEO_BUCKET']].objects["bvideos/#{videolisting.videocode}.mp4"]
|
videolisting.videocode,
|
||||||
obj.write(file)
|
"mp4",
|
||||||
puts "File #{videolisting.videocode} uploaded."
|
"bvideos_preview"
|
||||||
|
|
||||||
ensure
|
|
||||||
file.close
|
|
||||||
file.unlink
|
|
||||||
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.on_s3 = true
|
||||||
videolisting.save
|
videolisting.save
|
||||||
|
end
|
||||||
|
|
||||||
end
|
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
|
handle_asynchronously :download_video
|
||||||
end
|
end
|
Loading…
Reference in New Issue
Block a user