Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails和回形针:处理多种文件类型(视频和图像)的上传_Ruby On Rails_Paperclip - Fatal编程技术网

Ruby on rails Rails和回形针:处理多种文件类型(视频和图像)的上传

Ruby on rails Rails和回形针:处理多种文件类型(视频和图像)的上传,ruby-on-rails,paperclip,Ruby On Rails,Paperclip,我正在学习rails,在过去的两天里,我一直在想如何使用回形针通过同一型号上传视频和图像。我觉得我已经用尽了我的资源来寻找答案,但是还没有找到任何东西可以帮助我将正确的样式应用到正确的文件类型 这是我的图像模型(注意:我从一个图像模型和一个头像属性开始。我正在扩展它的用途,所以请不要混淆它的名称) 类映像“750x750>”,:medium=>“300x300”,:thumb=>“100x100”},:default_url=>“no_image.png”, 如果::是视频类型?,:样式=>{

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

这是我的图像模型(注意:我从一个图像模型和一个头像属性开始。我正在扩展它的用途,所以请不要混淆它的名称)

类映像“750x750>”,:medium=>“300x300”,:thumb=>“100x100”},:default_url=>“no_image.png”,
如果::是视频类型?,:样式=>{
:medium=>{:geometry=>“640x480”,:format=>“flv”},
:thumb=>{:geometry=>“100x100#”,:format=>'jpg',:time=>10}
},:处理器=>[:转码器]
验证\u附件\u内容\u类型:化身,
:content_type=>['image/png','image/jpeg','image/jpg','image/gif','video/mp4','video/m4v','video/mpeg']
def是图像类型?
content_type=['image/png'、'image/jpeg'、'image/jpg'、'image/gif']
#/\Aimage\/.*\Z/
结束
def是视频类型?
内容类型=[“视频/mp4”、“视频/m4v”、“视频/mpeg”]
结束
结束
本质上,我试图找出如何使样式适合其适当的文件类型。如果它是一个视频,我希望它被设计成一个视频,如果它是一个图像,作为一个图像

内容验证工作正常,一旦工作正常,我将修改它以包括所有图像格式和所有视频格式

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

然后,我将使用“content_type”字段提取我希望在站点不同区域显示的文件类型

如果使用一种模型的方法似乎不合适,请说明更好的方法

我用“回形针av转码器”宝石来处理视频

同样,我正在学习rails,所以如果有什么不清楚的地方,我很乐意澄清和修改


非常感谢。

所以我终于解决了这个问题。我的代码(基本上)是正确的,不过我将在这里通过最后的代码

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

所以,对于任何想上传视频的人来说,像我这样的新手,你需要安装像FFmpeg这样的东西来处理上传的视频

我在本教程中使用了自制软件,非常简单:

以下是我的代码的最终版本:

 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
类映像“no_image.png”
验证\附件\内容\类型:化身,:内容\类型=>/*/
#加载文件时将显示的图像路径
def处理图像路径(图像名称)
“/assets/”+Rails.application.assets.find\u asset(图像\u名称)。摘要\u路径
结束
进程_in_background:avatar,处理_image _url:lambda{| a | a.instance.processing _image _path(“dad.jpg”)}
#重要!必须将ffmpeg库添加到服务器计算机。
#可以从官方网站上传https://www.ffmpeg.org/
def检查文件类型
如果是图像类型?
{:大=>“750x750>”,:中=>“300x300”,:拇指=>“100x100”}
elsif是视频类型吗?
{
:medium=>{:geometry=>“300x300#”,:format=>'jpg'},
:video=>{:geometry=>“640x360”;:format=>'mp4',:processors=>[:transcoder]}
}
elsif是音频类型吗?
{
:音频=>{
:format=>“mp3”,:处理器=>[:转码器]
}
}
#头像文件名=self.basename(:头像文件名,self.extname(:头像文件名))
其他的
{}
结束
结束
#如果文件的内容类型包含单词“image”,则方法返回true,否则返回false
def是图像类型?
化身内容类型=~%r(图像)
结束
#如果文件的内容类型包含单词“video”,则方法返回true,否则返回false
def是视频类型?
化身内容类型=~%r(视频)
结束
#如果文件的内容类型包含单词“audio”,则方法返回true,反之则返回false
def是音频类型?
化身内容类型=~/\Aaudio\/.*\Z/
结束
结束
我仍然在添加不同的数据类型,并将在我完成验证后特别允许这些数据类型进行验证,但这将向您展示我所做的工作,您可以在此基础上进行构建


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

杰克,我正在尝试,但是假设我没有预先验证数据,因为整个系统都是内部的。我想直接上传一个文件使用主动管理,而不是验证内容类型-图像/音频/视频,并保存在数据库中的S3URL。你建议使用什么以及任何有用的链接?
 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