module Paperclip::Storage::Upyun

Public Class Methods

extended(base) click to toggle source
# File lib/paperclip/storage/upyun.rb, line 5
def self.extended base
  begin
    require 'upyun'
  rescue LoadError => e
    e.message << " (You may need to install the upyun gem)"
    raise e
  end unless defined?(::Upyun)

  base.instance_eval do
    @bucket = @options[:bucket]
    @operator = @options[:operator]
    @password = @options[:password]

    raise Paperclip::Upyun::Exceptions::OptionsError '(You should set upyun_host)' unless @host = @options[:upyun_host]
    # @options = @options[:options]
    # @endpoint = @options[:endpoint]
    @options[:interval] = "!"

    @upyun = ::Upyun::Rest.new(@bucket, @operator, @password)
    @options[:path] = @options[:path].gsub(/:url/, @options[:url])
    @options[:url] = ':upyun_public_url'
    @options[:need_delete] = @options[:need_delete] || true

    Paperclip.interpolates(:version) do |attachment, style|
      attachment.version(style)
    end unless Paperclip::Interpolations.respond_to? :version

    Paperclip.interpolates(:upyun_public_url) do |attachment, style|
      attachment.public_url(style)
    end unless Paperclip::Interpolations.respond_to? :upyun_public_url

    Paperclip.interpolates :upyun_host  do |attachment, style|
      attachment.upyun_host(style)
    end

  end

end

Public Instance Methods

exists?(style = default_style) click to toggle source
# File lib/paperclip/storage/upyun.rb, line 44
def exists?(style = default_style)
  resp = @upyun.getinfo(path(style))
  begin
    Paperclip::Upyun::Response.parse(resp)
  rescue => err
    log("UPYUN<ERROR>: #{err}")
  end
end
flush_deletes() click to toggle source
# File lib/paperclip/storage/upyun.rb, line 67
def flush_deletes
  if @options[:need_delete]
    for path in @queued_for_delete do
      delete(path)
    end
  end
  @queued_for_delete = []
end
flush_writes() click to toggle source
# File lib/paperclip/storage/upyun.rb, line 53
def flush_writes
  for style, file in @queued_for_write do
    log("saving #{path(style)}")
    retried = false
    begin
      upload(file, path(style))
    ensure
      file.rewind
    end
  end
  after_flush_writes # allows attachment to clean up temp files
  @queued_for_write = {}
end
public_url(style = default_style) click to toggle source
# File lib/paperclip/storage/upyun.rb, line 76
def public_url(style = default_style)
  url = "#{@options[:upyun_host]}/#{path(style)}"
end
upyun_host(style = default_style) click to toggle source
# File lib/paperclip/storage/upyun.rb, line 87
def upyun_host(style = default_style)
  "#{@options[:upyun_host]}" || raise('upyun_host is nil')
end
version(style = default_style) click to toggle source
# File lib/paperclip/storage/upyun.rb, line 80
def version(style = default_style)
  url = ''
  url += @options[:interval] unless style == default_style
  url += style.to_s unless style == default_style
  url
end

Private Instance Methods

delete(path) click to toggle source
# File lib/paperclip/storage/upyun.rb, line 102
def delete(path)
  res = @upyun.delete(path)
  begin
    Paperclip::Upyun::Response.parse(res)
  rescue => err
    log("UPYUN<ERROR>: #{err}")
  end
end
upload(file, path) click to toggle source
# File lib/paperclip/storage/upyun.rb, line 93
def upload(file, path)
  res = @upyun.put(path, File.new(file.path,"rb"))
  begin
    Paperclip::Upyun::Response.parse(res)
  rescue => err
    log("UPYUN<ERROR>: #{err}")
  end
end