0
votes

I have been having a lot of trouble trying to figure out how to upload thumbnails using the YT Gem. Does anyone know how to help? This is the original tutorial that I was following but it doesn't talk about custom thumbnails. [https://www.sitepoint.com/youtube-api-version-3-rails/][1] I can upload videos just not custom thumbnails.

Thanks very much for any help..

Video Upload Model

class VideoUpload < ActiveType::Object
  attribute :file, :varchar
  attribute :title, :varchar
  attribute :description, :text
  attribute :upload_thumbnail, :varchar

  validates :file, presence: true
  validates :title, presence: true
  validates :upload_thumbnail, presence: true

  def upload!(user)
    account = Yt::Account.new access_token: user.token
    account.upload_video self.file, title: self.title, description: self.description, upload_thumbnail: self.upload_thumbnail
  end
end

VideoUploadController

def create
  @video_upload = VideoUpload.new(
    title: params[:video_upload][:title],
    description: params[:video_upload][:description], 
    upload_thumbnail: params[:video_upload][:upload_thumbnail],
    file: params[:video_upload][:file].try(:tempfile).try(:to_path)
  )

  if @video_upload.save
    uploaded_video = @video_upload.upload!(current_user)

    if uploaded_video.failed?
      flash[:error] = 'There was an error while uploading your video...'
    else
      Video.create({
        link: "https://www.youtube.com/watch?v=#{uploaded_video.id}",
        uid: uploaded_video.id,
        title: uploaded_video.title,
        description: uploaded_video.description
      })

      flash[:success] = 'Your video has been uploaded!'
    end

    redirect_to root_url
  else
    render :new
  end
end

View

<%= form_for @video_upload do |f| %>
  <%= render 'shared/errors', object: @video_upload %>

  <div class="form-group">
    <%= f.label :file %>
    <%= f.file_field :file, class: 'form-control', required: true %>
  </div>

  <div class="form-group">
    <%= f.label :upload_thumbnail %>
    <%= f.file_field :upload_thumbnail, class: 'form-control', required: true %>
  </div>

  <div class="form-group">
    <%= f.label :title %>
    <%= f.text_field :title, class: 'form-control', required: true %>
  </div>

  <div class="form-group">
    <%= f.label :description %>
    <%= f.text_area :description, class: 'form-control', cols: 3 %>
  </div>

  <%= f.submit 'Upload', class: 'btn btn-primary' %>
<% end %>

Server Response

Processing by VideoUploadsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"wFf3EbDhQjJwttjnE+nbQxinH508coHBctC+hi4M4sNAyhe6fisDhGWscVDxppS9NJ3/fcjCGQKiNFy1thAXOA==", "video_upload"=>{"file"=>#, @original_filename="IMG_0673.MOV", @content_type="video/quicktime", @headers="Content-Disposition: form-data; name=\"video_upload[file]\"; filename=\"IMG_0673.MOV\"\r\nContent-Type: video/quicktime\r\n">, "upload_thumbnail"=>#, @original_filename="protest_3.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"video_upload[upload_thumbnail]\"; filename=\"protest_3.png\"\r\nContent-Type: image/png\r\n">, "title"=>"o iuiou goiug oiug piu gpiugpiu ", "description"=>"yg ouyg ouyg piu gpugpiu piuhpiuhpiuh [ io hio h; ['o ih[oi [oih [' oih "}, "commit"=>"Upload"}

UPDATED CONTROLLER:

def create
  @video_upload = VideoUpload.new(video_upload_params)
      [:file].try(:tempfile).try(:to_path))
    if @video_upload.save
        video = video_upload_params[:file].try(:tempfile).try(:to_path)
        account = Yt::Account.new access_token: current_user.token
        uploaded_video = account.upload_video(video, video_upload_params)
      if uploaded_video.failed?
        render :new
        flash[:error] = 'There was an error while uploading your video...'
      else
        Video.create({link: "https://www.youtube.com/watch?v=#{uploaded_video.id}",
        uid: uploaded_video.id, title: uploaded_video.title, description: uploaded_video.description})
        flash[:success] = 'Your video has been uploaded!'
      end
      redirect_to videos_path
    else
      render :new
    end
  end
private
    def video_upload_params
       params.require(:video_upload).permit(:file, :title, :description, :upload_thumbnail)
    end

Processing by VideoUploadsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"V1qozopsKc8Cb9/99ex4GME2O6Z5MFDaPQhiILA5fLfXx0hlRKZoeRd1dkoXozfm7QzbRo2AyBnt7IATKCWJTA==", "video_upload"=>{"file"=>#, @original_filename="IMG_0673.MOV", @content_type="video/quicktime", @headers="Content-Disposition: form-data; name=\"video_upload[file]\"; filename=\"IMG_0673.MOV\"\r\nContent-Type: video/quicktime\r\n">, "upload_thumbnail"=>#, @original_filename="protest_3.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"video_upload[upload_thumbnail]\"; filename=\"protest_3.png\"\r\nContent-Type: image/png\r\n">, "title"=>";lekhjg wktrj hoibwj 5tor[fij [boij r[oifj [goj [oi j", "description"=>"woer jgowiejr [ihjg w[oei fgoi joifj g[okj fskdjfa;lkj f;klajs d;lfkdaj sd;lkfj a;lskdjf lakjs df;lkaj sdk e;lkrj ;gkj4 roij toi2j 45oirj toij 45oijrtij 54ij =tioj = j5=oi ejwrgoij wroiejh gpoiej rtpoihj ep5iojt rpoibj5 t"}, "commit"=>"Upload"}

1
do you get an error message in your server log? - Fabrizio Bertoglio
Hi. Thanks for the response. I do not appear to get an error. I appended my server response to my question though. - Matthew
How do you pass the custom thumbnail with your parameters? - Fabrizio Bertoglio
Sorry but I'm relatively new to rails. I thought I was doing that with the "upload!" method in my model. I guess I am missing something? - Matthew

1 Answers

0
votes
  1. GET request

    GET "videouploads/new", "videouploads#new"
    
  2. Server renders the videouploads/new.html.erb and the form field @videoupload.upload_thumbnail

    <div class="form-group">
      <%= f.label :upload_thumbnail %>
      <%= f.file_field :upload_thumbnail, class: 'form-control', required: true %>
    </div>
    
  3. User fills the form and submits

  4. You controller with strong params allows those parameters to be read. This are the parameters, you need to focus on "upload_thumbnail". This is where you pass the values for you video thubnail.. It includes different fields like "upload_thumbnail",@original_filename, etc..

    {"utf8"=>"✓", 
     "authenticity_token"=>"somecode", 
     "video_upload"=>{
          ..a sequence of fields then.., 
          "upload_thumbnail"=>#, 
          @original_filename="protest_3.png", 
          @content_type="image/png", 
          @headers="Content-Disposition: form-data; 
          name=\"video_upload[upload_thumbnail]\"; 
          filename=\"protest_3.png\"\r\nContent-Type: image/png\r\n">, 
          "title"=>"o iuiou goiug oiug piu gpiugpiu ", 
           "description"=>"yg ouyg ouyg piu gpugpiu piuhpiuhpiuh [ io hio h; ['o ih[oi [oih [' oih "}, 
           "commit"=>"Upload"
          }
    
  5. Create an instance of Video Upload with the params passed from the router in step 4, the params will be those above:

    @video_upload = VideoUpload.new(params)
    

The problem may be that you are not using the whole parameters params[:video_upload], but just

upload_thumbnail: params[:video_upload][:upload_thumbnail]

Which as you can see from above it will equal:

"upload_thumbnail"=>#, 

You still have many other field for the thumbnail, for example

@original_filename="protest_3.png", 
@content_type="image/png", 
@headers="Content-Disposition: form-data; 
name=\"video_upload[upload_thumbnail]\"; 
filename=\"protest_3.png\"\r\nContent-Type: image/png\r\n">, 
"title"=>"o iuiou goiug oiug piu gpiugpiu ", 
"description"=>"yg ouyg ouyg piu gpugpiu piuhpiuhpiuh [ io hio h; ['o ih[oi [oih [' oih "}, 
"commit"=>"Upload"

So maybe would be a good idea to create the object with all the parameters

@video_upload = VideoUpload.new(your_strong_params)

you need to use rails strong params

http://weblog.rubyonrails.org/2012/3/21/strong-parameters/

  1. Then Save the instance video_upload

    @video_upload.save
    

If this does not solve the problem, you need to stop with the debugger or print to the server with the put ruby syntax the following fields:

puts @video_upload.errors.full_messages

Or check those out with the debug and tell us if you have any error message, as probably you are not saving it.. or maybe