Commit 2d6beb84 authored by Rafael Reggiani Manzo's avatar Rafael Reggiani Manzo

Merge branch 'thumbs' into 'master'

Thumbs

Add thumbnails generation for videos.
This is done by indicating to the zencoder API we want a thumbnail for the content being encoded. Its dimensions will be the same as the content's.

See merge request !23
parents 16916151 3e4af691
Pipeline #3685120 passed with stage
in 9 minutes and 12 seconds
require 'open-uri'
class ContentsController < ApplicationController
before_action :set_content, only: [:show]
protect_from_forgery with: :null_session, only: :zencoder_callback
......@@ -38,7 +36,8 @@ class ContentsController < ApplicationController
@encoded_content.height = params[:outputs].first[:height]
@encoded_content.duration = params[:outputs].first[:duration_in_ms]
@encoded_content.file_size = params[:outputs].first[:file_size_in_bytes]
@encoded_content.video = open(params[:outputs].first[:url])
@encoded_content.video = URI.parse(params[:outputs].first[:url])
@encoded_content.thumbnail = URI.parse(params[:outputs].first[:thumbnails].first[:images].first[:url])
@encoded_content.save
end
......
......@@ -3,10 +3,15 @@ class EncodedContent < ApplicationRecord
storage: :s3,
s3_credentials: AWSSettings.to_h,
s3_region: AWSSettings.aws_region
validates_attachment_content_type :video, content_type: /\Avideo\/.*\Z/
has_attached_file :thumbnail,
storage: :s3,
s3_credentials: AWSSettings.to_h,
s3_region: AWSSettings.aws_region
validates_attachment_content_type :video, content_type: /\Avideo\/.*\Z/
validates_attachment_content_type :thumbnail, content_type: /\Aimage/
validates :content_id, :job_id, presence: true
belongs_to :content
end
......@@ -4,6 +4,7 @@ default_outputs: &default_outputs
size: '848x480'
public: true
quality: 4
thumbnails: {}
default_http_options: &default_http_options
http_default_options:
......
class AddThumbnailToEncodedContent < ActiveRecord::Migration[5.0]
def self.up
change_table :encoded_contents do |t|
t.attachment :thumbnail
end
end
def self.down
remove_attachment :encoded_contents, :thumbnail
end
end
......@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160701135238) do
ActiveRecord::Schema.define(version: 20160706141505) do
create_table "contents", force: :cascade do |t|
t.string "title", null: false
......@@ -50,8 +50,8 @@ ActiveRecord::Schema.define(version: 20160701135238) do
end
create_table "encoded_contents", force: :cascade do |t|
t.integer "content_id", null: false
t.integer "job_id", null: false
t.integer "content_id", null: false
t.integer "job_id", null: false
t.integer "output_id"
t.integer "file_size"
t.integer "width"
......@@ -62,8 +62,12 @@ ActiveRecord::Schema.define(version: 20160701135238) do
t.string "video_content_type"
t.integer "video_file_size"
t.datetime "video_updated_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "thumbnail_file_name"
t.string "thumbnail_content_type"
t.integer "thumbnail_file_size"
t.datetime "thumbnail_updated_at"
end
create_table "profiles", force: :cascade do |t|
......
......@@ -86,16 +86,19 @@ RSpec.describe ContentsController, type: :controller do
describe 'POST #zencoder_callback' do
let!(:job_id) { '1' }
let!(:thumb_output) { [ images: [ url: 'http://test.com' ] ] }
let!(:output) { { id: '2', state: 'finished', width: '640', height: '320', duration_in_ms: '5000',
file_size_in_bytes: '1024', url: 'http://test.com' } }
file_size_in_bytes: '1024', url: 'http://test.com', thumbnails: thumb_output } }
let!(:outputs) { [output] }
let!(:video_file_mock) { instance_double('video_file') }
let!(:thumb_file_mock) { instance_double('thumb_file') }
context 'when the content is found' do
let(:encoded_video) { build(:encoded_video) }
before do
expect(EncodedContent).to receive(:find_by).with(job_id: job_id).and_return(encoded_video)
expect(subject).to receive(:open).with(outputs.first[:url]).and_return(video_file_mock)
expect(URI).to receive(:parse).with(outputs.first[:url]).and_return(video_file_mock)
expect(URI).to receive(:parse).with(thumb_output.first[:images].first[:url]).and_return(thumb_file_mock)
expect(encoded_video).to receive(:save).and_return(true)
expect(encoded_video).to receive(:output_id=).with(output[:id])
expect(encoded_video).to receive(:state=).with(output[:state])
......@@ -104,6 +107,7 @@ RSpec.describe ContentsController, type: :controller do
expect(encoded_video).to receive(:duration=).with(output[:duration_in_ms])
expect(encoded_video).to receive(:file_size=).with(output[:file_size_in_bytes])
expect(encoded_video).to receive(:video=).with(video_file_mock)
expect(encoded_video).to receive(:thumbnail=).with(thumb_file_mock)
post :zencoder_callback, params: { job: { id: job_id}, outputs: outputs }
end
......
......@@ -6,8 +6,19 @@ RSpec.describe EncodedContent, type: :model do
it { is_expected.to validate_presence_of :job_id }
end
describe 'paperclip attachments' do
it { is_expected.to have_attached_file(:video) }
it { is_expected.to validate_attachment_content_type(:video).
allowing('video/mp4', 'video/x-flv', 'video/MP2T', 'video/3gpp', 'video/quicktime', 'video/x-msvideo', 'video/x-ms-wmv').
rejecting('text/plain', 'text/xml', 'image/png', 'image/gif') }
it { is_expected.to have_attached_file(:thumbnail) }
it { is_expected.to validate_attachment_content_type(:thumbnail).
allowing('image/png', 'image/gif', 'image/jpg').
rejecting('text/plain', 'text/xml', 'video/mp4', 'video/x-flv') }
end
describe 'relations' do
it { should belong_to(:content) }
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment