简体   繁体   English

使用Paperclip和Amazon S3获取包含照片的zip文件

[英]Using Paperclip and Amazon S3 for zip file containing photos

I currently have a working photo uploader that creates Photo images using paperclip and aws-s3 gems. 我目前有一个可以正常工作的照片上传程序,可以使用回形针和aws-s3宝石创建照片图像。 The loader can also dynamically add Photo upload fields so multiple files can be uploaded at once on a single submit. 加载程序还可以动态添加照片上传字段,以便在单个提交时一次上传多个文件。 What I'd like to do is have the option of uploading a zip file with the expectation that the file contains Photos and have it run through my same process of creating thumbnails, medium size, and original images that the single photo file upload goes through. 我想做的是可以选择上传一个zip文件,期望文件包含照片,并让它通过我创建缩略图,中等大小和单张照片文件上传的原始图像的相同过程运行。 My model and controller is pretty straight forward with storing photos locally if on development, or on s3 if production, with just a little bit on fanciness with the view template: 我的模型和控制器非常直接,如果在开发时在本地存储照片,或者在生产时在s3上存储照片,只需稍微看一下视图模板:

photo.rb photo.rb

class Photo < ActiveRecord::Base
  belongs_to :album
  if AppConfig['s3']
    has_attached_file :data, 
      :styles => { 
        :thumb => "100x100>",
        :medium => "500x500>" 
      },
      :storage => :s3, 
      :default_style => :original,
      :bucket => AppConfig['s3']['bucket_name'],
      :s3_credentials => { :access_key_id => AppConfig['s3']['access_id'], :secret_access_key => AppConfig['s3']['secret_key'] },
      :s3_headers => { 'Cache-Control' => 'max-age=315576000', 'Expires' => 10.years.from_now.httpdate },
      :path => "/:class/:id/:style/:filename"
  else
    has_attached_file :data,
      :styles => { 
        :thumb => "100x100>",
        :medium => "500x500>"
      },
      :storage => :filesystem, 
      :default_style => :original
  end
end

*photos_controller.rb* * photos_controller.rb *

class Admin::PhotosController < Admin::AdminController
  def index
    @photos = Photo.all
  end

  def show
    @photo = Photo.find(params[:id])
  end

  def new
    @photo = Photo.new
  end

  def create
    @photo = Photo.new(params[:photo])
    if @photo.save
      flash[:notice] = "Successfully created photo."
      redirect_to [:admin, @photo]
    else
      render :action => 'new'
    end
  end

  def edit
    @photo = Photo.find(params[:id])
  end

  def update
    @photo = Photo.find(params[:id])
    album = @photo.album
    if @photo.update_attributes(params[:photo])
      flash[:notice] = "Successfully updated photo."
      redirect_to [:admin, @photo]
    else
      redirect_to edit_admin_album_url(album)
    end
  end

  def destroy
    @photo = Photo.find(params[:id])
    album = @photo.album
    @photo.destroy
    flash[:notice] = "Successfully destroyed photo."
    redirect_to edit_admin_album_url(album)
  end

end

The interesting parts of the view are here: 视图的有趣部分在这里:

*_form.html.haml* * _form.html.haml *

  #photos
    - if @album.new_record?
      = render :partial => 'photo', :locals => { :form => f, :photo => @album.photos.build }
    - else
      - for photo in @album.photos
        .photo
          = link_to(image_tag(photo.data(:thumb)), photo.data(:medium), :class => 'photo_link')
          - f.fields_for @album.photos do |photo_field|
            / Viewable?
            / = photo_field.check_box :viewable
          %br
          = link_to "Delete", [:admin, photo], :confirm => 'Are you sure?', :method => :delete
      .float_clear
  = add_object_link("New Photo", f, @album.photos.build, "photo", "#photos")
  .row
    = submit_tag "Save", :disable_with => "Uploading please wait..."
  .float_clear

*_photo.html.haml* * _photo.html.haml *

.photo_form
  %p
  - form.fields_for :photos, photo, :child_index => (photo.new_record? ? "index_to_replace_with_js" : nil) do |photo_form| 
    = photo_form.file_field :data
    = link_to_function "delete", "remove_field($(this), ('.photo_form'))" 
    %br

Welcome all ideas or contributions! 欢迎所有想法或贡献! Thanks! 谢谢!

I would use a callback to pull out the archive files (zip, tar etc) and let the image files go on through to be processed/saved. 我会使用回调来提取存档文件(zip,tar等)并让图像文件继续处理/保存。 Use delayed_job to process the archives after upload to increase the user experience and ease the load on your server. 上传后使用delayed_job处理存档,以增加用户体验并减轻服务器负载。

I'm not sure of any archive utility wrappers in ruby but you could use system calls to unzip archives using tar or something similar, then loop through the unzipped files to process and store the images and discard non-image files. 我不确定ruby中的任何归档实用程序包装器,但您可以使用系统调用使用tar或类似的东西解压缩归档,然后循环解压缩文件以处理和存储图像并丢弃非图像文件。

You could even use a rake task and cron job to periodically unzip, loop through and create Photos from the untarred archives. 您甚至可以使用rake任务和cron作业来定期解压缩,循环并从untarred归档创建Photos。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM