Tag: 回形针

从Ruby on Rails 3.2.6中的Paperclip生成的文件中删除问号

我正在使用Paperclip-FFMEG将video文件上传到我的开发环境(当我的项目投入生产时,最终会上传到本地服务器)。 上传video时,默认情况下,文件名如下: /system/modelnames/paperclipnames/…/mynewfile.mp4?xxxxxxxxxx 我相信问号后面的10位数字是一个时间戳。 但是,我将用来播放video的播放器不希望在文件附件后有任何内容 – 因此我想在将URL传递给播放器之前删除问号和后面的时间戳。 我试着使用以下Ruby(我认为)strip函数: temp_variable = model.paperclipattribute.url(:blah).strip(‘?’)[0] 但是,Rails会抛出一个错误: wrong number of arguments(1 for 0) 我认为我做错了吗? 还有其他方法吗? 我不想完全关闭时间戳,因为在这种情况下我只需要这样做。 谢谢!

安全显示上传了paperclip gem的图像

默认情况下:paperclip gem存储公共目录中的所有附件。 出于安全原因,我不想将附件存储在公共目录中,因此我将它们保存在应用程序根目录的uploads目录中: class Post < ActiveRecord::Base belongs_to :user has_attached_file :some_image, path: ":rails_root/uploads/:attachment/:id/:style/:filename" do_not_validate_attachment_file_type :some_image end 我没有指定url选项,因为我不想为每个图像附件添加url。 如果指定了url:那么具有该url的任何人都可以访问该图像。 这不安全。 在user#show页面中:我想实际显示图像。 如果我使用所有回形针默认值,那么我可以这样做,因为图像将在公共目录中,图像将有一个URL: Some image: 看来如果我将图像附件保存在公共目录之外并且没有指定URL(再次:执行此操作以保护图像),则图像将不会在视图中呈现。 我知道如何通过pundit gem在整个应用程序中进行授权。 但是,当该图像可公开访问时,授权图像是无用的。 因此,我尝试通过删除URL并将图像保存在公共目录之外来使图像无法公开访问,但随后图像不再在视图中呈现。 更新 :我的问题实际上归结为:我的图像保存在我的rails应用程序中。 使用回形针:有没有办法在视图中安全地显示图像? 换句话说:设置它使图像没有自己独立的URL(因为任何有该URL的人都可以访问该图像,使该图像不安全)。 以下是安全显示图像的一些示例: 示例#1:仅在用户登录时显示图像。(图像没有自己独立的URL,因此没有其他方法可以访问图像) 示例#2:仅在用户与此图像关联时显示图像。 (图像没有自己独立的url,因此没有其他方法可以访问图像) 附加更新 :我知道我可以安全地发送一个send_file文件,允许用户下载图像,但这不是我想要做的。 我不希望用户下载图像。 相反:我希望用户能够在网页上实际看到图像。 我想在页面上安全地显示/渲染图像。 使用send_file (据我所知)允许用户下载文件。 它不会在页面上呈现图像。

Paperclip:与邮箱gem集成

我正在使用mailboxer gem,我不知道如何使用Paperclip(Message Class)。 将Paperclip与User类一起使用是: class User < ActiveRecord::Base has_attached_file :picture end 如何将has_attached_file添加到Message类(模型中没有message.rb)? 谢谢。

使用回形针在heroku种子图像

当我运行heroku run rake db:seed我得到 Command :: identify -format ‘%wx%h,%[exif:orientation]’ ‘/tmp/image20130219-2-1gk1yip.png[0]’ Command :: composite -gravity Center /app/public/media/watermark.png “/tmp/image20130219-2-1gk1yip.png[0]” -resize “1×1<" "/tmp/image20130219-2-1gk1yip.png20130219-2-1ng5f6c[0]" Command :: file -b –mime '/tmp/image20130219-2-1gk1yip.png20130219-2-1ng5f6c' Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/image20130219-2-1gk1yip20130219-2-t3caqg.png20130219-2-1ng5f6c[0]' Command :: convert "/tmp/image20130219-2-1gk1yip20130219-2-t3caqg.png20130219-2-1ng5f6c[0]" -resize "260×190" "/tmp/image20130219-2-1gk1yip20130219-2-t3caqg.png20130219-2-1mz0u16[0]" Command :: file -b –mime '/tmp/image20130219-2-1gk1yip20130219-2-t3caqg.png20130219-2-1mz0u16' [paperclip] Saving attachments. 我得到了我的Stuff和有效的db记录,但是在公共文件夹中没有重新创建图像..如果我通过视图forms添加图像一切都很好。 我的种子 Stuff.create! title: ‘title’, description: ‘desc’, […]

压缩存储在S3上的所有Paperclip附件

Paperclip是Rails的一个很棒的上传插件。 将上传存储在本地文件系统或Amazon S3上似乎运行良好。 我只假设本地主机上存储文件,但此应用程序需要使用S3,因为它将托管在Heroku上。 如何通过单个压缩下载从S3获取所有上传/附件? 从本地文件系统获取zip 文件似乎很简单。 它让S3中的文件让我感到困惑。 我认为这可能与rubyzip处理URL引用的文件的方式有关。 我尝试了各种方法但似乎无法避免错误。 format.zip { registrations_with_attachments = Registration.find_by_sql(‘SELECT * FROM registrations WHERE abstract_file_name NOT LIKE “”‘) headers[‘Cache-Control’] = ‘no-cache’ tmp_filename = “#{RAILS_ROOT}/tmp/tmp_zip_” << Time.now.to_f.to_s < No such file or directory – http://s3.amazonaws.com/bucket/original/abstract.txt # Should note that these files in S3 bucket are publicly accessible. No ACL. # works […]

在seeds.rb中使用Paperclip

我们说我的seeds.rb文件中有以下条目: Image.create(:id => 52, :asset_file_name => “somefile.jpg”, :asset_file_size => 101668, :asset_content_type => “image/jpeg”, :product_id => 52) 如果我播种它,它会尝试处理指定的图像,我收到此错误: No such file or directory – {file path} etc… 我的图像备份,所以我真的不需要创建它们; 但我需要记录。 我不能在我的模型中评论paperclip指令; 然后它起作用; 但我想可能还有另一种解决方案。 是否还有其他模式可以实现它? 或者告诉回形针不要处理图像?

Paperclip和Amazon S3如何做路径?

使用Amazon S3时如何使用回形针创建路径? 我的目录上的目录是: /image/:id/:filename 我的模特: has_attached_file :image, :storage => :s3, :bucket => ‘mybucket’, :s3_credentials => { :access_key_id => ENV[‘S3_KEY’], :secret_access_key => ENV[‘S3_SECRET’] }

回形针:从带有扩展名的url上传

我想通过paperclip在S3存储上传url中的图片。 我合作: Ruby 1.9.3 Rails 3.2.6 paperclip 3.1.3 aws-sdk 1.3.9 我有我的图片模型: class Asset has_attached_file :asset, :styles => {:thumb => “60×60>”}, :storage => :s3, :s3_credentials => “#{Rails.root}/config/s3.yml”, :path => “/pictures/:id/:style.:extension” validates_attachment_content_type :asset, :content_type => [‘image/gif’, ‘image/jpeg’, ‘image/png’, ‘image/x-ms-bmp’] end 所以基本上我这是从URL下载我的文件: picture = Asset.new(asset: open(“http://sofzh.miximages.com/ruby-on-rails/my_picture.jpg”)) picture.save 但是它使用错误的file_name保存我的文件,并且它没有设置文件的扩展名: # p.asset.url => http://s3.amazonaws.com/my_assets_path/pictures/5/original. 如您所见,没有扩展名。 我找到了解决它的方法,但我确信我可以有更好的方法。 这个解决方案是在我的计算机上复制文件然后我在S3上发送它,如下所示: filename = “#{Rails.root}/tmp/my_picture.jpg” […]

Ruby on Rails – Paperclip和动态参数

我正在使用Paperclip为Ruby on Rails编写一些图像上传代码,我有一个可行的解决方案,但它非常hacky所以我真的很感激如何更好地实现它的建议。 我有一个’资产’类,其中包含有关上传图像的信息,包括Paperclip附件,以及封装尺寸信息的’Generator’类。 每个“项目”都有多个资产和发电机; 所有资产应根据每台发电机规定的尺寸resize; 因此,每个项目都有一定的规模,其所有资产都应具备。 发电机型号: class Generator < ActiveRecord::Base attr_accessible :height, :width belongs_to :project def sym "#{self.width}x#{self.height}".to_sym end end 资产模型: class Asset lambda { |a| a.instance.styles } belongs_to :project # this is utterly horrendous def styles s = {} if @generators == nil @generators = self.project.generators end @generators.each do |g| s[g.sym] = […]

回形针:样式取决于型号(has_many多态图像)

我已将模型设置为使用多态Image模型。 这工作正常,但我想知道是否可以更改每个模型的:styles设置。 使用STI(Model <Image)找到了一些例子但是这对我来说不是一个选项,因为我使用的是has_many关系。 艺术 has_many :images, :as => :imageable 图片 belongs_to :imageable, :polymorphic => true has_attached_file :file, :styles => { :thumb => “150×150>”, :normal => “492×600>”} #Change this setting depending on model UPDATE 我尝试在Proc方法中启动调试器。 仅填充与附加文件相关的字段: run’irb(Image):006:0> a.instance => # 这是ImageController #create的对象 ImageController#create @image => # 我正在使用paperclip(2.3.5)和Rails 3.0.1。 无论我做什么,a.instance对象都是只填充了与附件相关的字段的图像。 有任何想法吗? UPDATE2 在Paperclip论坛上阅读了很多内容后,我认为在保存实例之前无法访问该实例。 你只能看到Paperclip的东西,就是这样。 我通过使用前置filter从图像控制器预设图像来解决这个问题 – […]