Sha256: 201aad2400670fc5c85901e15d828fb8b772c4b1b1ad7cfc8dd063af65c16d5a

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

class FileUploader < CarrierWave::Uploader::Base
    include CarrierWave::MiniMagick
    storage :file
    after :remove, :delete_empty_upstream_dirs

    version :normalized, :if => :image? do
      process :resize_to_fit => [600,600]
    end

    def timestamp
      time=if model
        model.created_at || Time.now
      else
        Time.now
      end
      time.strftime("%Y%m")
    end

    # With slash in first place it will be absolute url, otherwise relative to Rails.root+"/public"
    def store_dir
      if model
        "upload/#{model.class.to_s.underscore}/#{timestamp}/#{model.id}"
      else
        "upload/misc"
      end
    end

    def delete_empty_upstream_dirs
      path = ::File.expand_path(store_dir, root)
      Dir.delete(path) # fails if path not empty dir
      
     # path = ::File.expand_path(base_store_dir, root)
     # Dir.delete(path) # fails if path not empty dir
    rescue SystemCallError
      true # nothing, the dir is not empty
    end

    def image?(file)
      File.extname(file.filename.to_s).match(/jpg|JPG|jpeg|JPEG|png|PNG|gif|GIF/)
    end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lolita-file-upload-0.2.9 app/uploaders/file_uploader.rb