0

我有带图片和视频的模型

如何检查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"
4

2 回答 2

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
于 2017-11-20T15:05:58.850 回答
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
于 2017-11-21T11:53:09.040 回答