2010-12-03 76 views
0

我目前有一個工作照片上傳,使用回形針和aws-s3寶石創建照片圖像。加載器還可以動態添加照片上傳字段,以便一次提交多個文件即可立即上傳。我想要做的是可以選擇上傳一個zip文件,期望該文件包含照片,並通過創建縮略圖,中等大小以及單張照片文件上傳所經過的原始圖像的相同過程來運行該文件。我的模型和控制器是,如果在生產花式視圖模板存儲在本地,如果在開發或S3上的照片,只有一點點很直接:使用回形針和亞馬遜S3的zip文件包含照片

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 *

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 

視圖的有趣的部分是在這裏:

* _fo rm.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_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 

歡迎所有的想法和貢獻!謝謝!

回答

2

我會使用回調來提取存檔文件(zip,tar等),並讓圖像文件繼續處理/保存。使用delayed_job在上傳後處理歸檔,以增加用戶體驗並減輕服務器的負載。

我不確定在ruby中有任何存檔實用程序包裝,但可以使用系統調用來使用tar或類似的東西解壓縮存檔,然後循環解壓縮文件來處理和存儲圖像並丟棄非圖像文件。

您甚至可以使用rake任務和cron作業定期解壓,循環並從解壓縮的存檔創建照片。

+0

感謝您的答案喬恩。我已經將應用部署在heroku上,並且不願意爲DJ流程付費,而cron只能每天使用一次。我將嘗試爲zip(歸檔)文件創建一個單獨的s3存儲桶並使用回調進行後期處理。 – 2010-12-05 09:11:34