【问题标题】:Rails and Paperclip: Handling uploads for multiple file types (Video and Images)Rails 和 Paperclip:处理多种文件类型(视频和图像)的上传
【发布时间】:2015-04-30 23:05:39
【问题描述】:

我正在学习 rails,过去两天我一直在试图弄清楚如何使用回形针通过同一模型上传视频和图像。我觉得我已经用尽了我的资源来寻找答案,但没有找到任何似乎可以帮助我将正确的样式应用于正确的文件类型的东西。

这是我的 Image 模型(注意:我从 Image 模型和 avatar 属性开始。我正在扩展它的用途,所以请不要对它的命名方式感到困惑)

class Image < ActiveRecord::Base
  belongs_to :imageable, polymorphic: true

  has_attached_file :avatar, 
    if: :is_image_type?, styles: {:large => "750x750>", :medium => "300x300#", :thumb => "100x100#" }, :default_url => "no_image.png",
    if: :is_video_type?, :styles =>  {
        :medium => { :geometry => "640x480", :format => 'flv' },
        :thumb => { :geometry => "100x100#", :format => 'jpg', :time => 10 }
      }, :processors => [:transcoder]

  validates_attachment_content_type :avatar,
    :content_type => ['image/png', 'image/jpeg', 'image/jpg', 'image/gif', "video/mp4", "video/m4v", "video/mpeg"]

  def is_image_type? 
    content_type = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif']
    # /\Aimage\/.*\Z/
  end 

  def is_video_type? 
    content_type = ["video/mp4", "video/m4v", "video/mpeg"]
  end

end

基本上我正在尝试弄清楚如何让样式适用于其适当的文件类型。如果是视频,我希望它被设置为视频,如果是图像,则作为图像。

内容验证工作正常,一旦工作正常,我将对其进行更改以包含所有图像格式和所有视频格式。

目标 总而言之,我的目标是能够拥有一个文件上传字段,用户可以在其中上传多个文件和多种文件类型,包括图像、视频、音频、pdf 和其他文件类型。

然后我将使用“content_type”字段来提取我想在网站不同区域显示的文件类型。

如果这种仅使用一个模型的方法似乎不合适,请指出更好的方法。

我正在使用“paperclip-av-transcoder” gem 来处理视频。

再次,我正在学习 Rails,所以如果有不清楚的地方,我很乐意澄清和修改。

非常感谢。

【问题讨论】:

    标签: ruby-on-rails paperclip


    【解决方案1】:

    所以我终于找到了问题所在。我的代码(基本上)是正确的,不过我会在这里传递最终代码。

    问题在于它没有正确处理,因为我的机器上没有安装编码器。

    因此,对于任何想要上传视频并且像我这样的新手,您需要安装 FFmpeg 之类的工具来处理上传视频时的处理。

    我在本教程中使用自制软件,非常简单:https://trac.ffmpeg.org/wiki/CompilationGuide/MacOSX

    这是我的代码的最终版本:

     class Image < ActiveRecord::Base
      belongs_to :imageable, polymorphic: true
    
    
    
      # Apply styling appropriate for this file
      has_attached_file :avatar, styles: lambda { |a| a.instance.check_file_type}, :default_url => "no_image.png"
      validates_attachment_content_type :avatar, :content_type => /.*/
    
    
      # The path of the image that will be shown while the file is loading
      def processing_image_path(image_name)
        "/assets/" + Rails.application.assets.find_asset(image_name).digest_path
      end  
    
    
      process_in_background :avatar, processing_image_url: lambda { |a| a.instance.processing_image_path("dad.jpg")}
    
    
      # IMPORTANT! The ffmpeg library has to added to the server machine. 
      # It can be uploaded from the official website https://www.ffmpeg.org/
      def check_file_type
        if is_image_type?
          {:large => "750x750>", :medium => "300x300#", :thumb => "100x100#" }
        elsif is_video_type?
          {
              :medium => { :geometry => "300x300#", :format => 'jpg'},
              :video => {:geometry => "640x360#", :format => 'mp4', :processors => [:transcoder]}
          }
        elsif is_audio_type?
          {
            :audio => {
              :format => "mp3", :processors => [:transcoder]
            }
          }
         # avatar_file_name = self.basename(:avatar_file_name, self.extname(:avatar_file_name))
        else
          {}
        end
      end
    
    
    
      # Method returns true if file's content type contains word 'image', overwise false
      def is_image_type?
        avatar_content_type =~ %r(image)
      end
    
      # Method returns true if file's content type contains word 'video', overwise false
      def is_video_type?
        avatar_content_type =~ %r(video)
      end
    
      # Method returns true if file's content type contains word 'audio', overwise false
      def is_audio_type?
        avatar_content_type =~ /\Aaudio\/.*\Z/
      end
    
    end
    

    我仍在添加不同的数据类型,并在我完成后特别允许这些数据类型进行验证,但这应该会向您展示我所做的并且您可以在此基础上进行构建。

    希望这对其他人有帮助,而不仅仅是我!

    【讨论】:

    • 杰克,我正在尝试这个,但假设我没有预先验证数据,因为整个系统是内部的。我想使用活动管理员直接上传文件而不验证内容类型 - 图像/音频/视频并将 s3 url 保存在数据库中。您建议使用什么以及任何有用的链接?
    猜你喜欢
    • 2017-09-03
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 2015-03-10
    • 2016-07-30
    • 2023-03-16
    • 1970-01-01
    • 2020-12-16
    相关资源
    最近更新 更多