如何检查扩展类型然后保存在正确的位置

时间:2017-11-20 14:55:44

标签: ruby-on-rails mongoid feedjira

我有照片和视频模型

如何检查enclosure_type然后保存在正确的位置?

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

例如:

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"

2 个答案:

答案 0 :(得分:1)

所以这是我在控制器中所做的事情,但让我告诉你我做了什么

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

答案 1 :(得分:0)

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