Anton Ipatov
Anton Ipatov

Reputation: 167

How to check the extension type then save in the proper location

I have model with pictures and videos

How to check the enclosure_type then save in the proper location?

feed_entry.rb

class FeedEntry
  include Mongoid::Document

  field :enclosure_url, type: String
  field :enclosure_type, type: String
  field :video, type: String
  mount_uploader :picture, PictureUploader
end

For example:

feed = Feedjira::Feed.fetch_and_parse 'http://example.com/feed.xml'

feed.entries.first.enclosure_url # => "http://example.com/d260eb5c"
feed.entries.first.enclosure_type # => "image/jpeg"

feed.entries.last.enclosure_url # => 'https://youtu.be/AnXR-MKhg64'
feed.entries.last.enclosure_type # => "video/x-ms-asf"

Upvotes: 0

Views: 38

Answers (2)

Anton Ipatov
Anton Ipatov

Reputation: 167

before_save  :save_uri

def media_type
    if self.enclosure_type =~ /image\//
      self.remote_picture_url = self.enclosure_url
    elsif self.enclosure_type =~ /video\//
      self.video = self.enclosure_url
    else
      self.video = self.enclosure_url
    end
  end

Upvotes: 0

MZaragoza
MZaragoza

Reputation: 10111

So this is something that I am doing in the controller but let me show you what I have done

class FilesController < ApplicationController

  def create 
    ...
    feed_entry.save
    if params[:file].content_type == 'image/jpeg'
      feed_entry.save_image
      # save file at images location
    elsif params[:file].content_type == 'video/x-ms-asf'
      feed_entry.save_video
      # save file at video location
    else
      # Dont know how to process file
    end
    ...
  end
end

Upvotes: 1

Related Questions