Module: Paperclip::Storage::Upyun

Defined in:
lib/paperclip/storage/upyun.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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

Instance Method Details

#exists?(style = default_style) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
# 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_deletesObject



67
68
69
70
71
72
73
74
# 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_writesObject



53
54
55
56
57
58
59
60
61
62
63
64
65
# 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) ⇒ Object



76
77
78
# 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) ⇒ Object



87
88
89
# 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) ⇒ Object



80
81
82
83
84
85
# 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