Class: Pushfile::Upload

Inherits:
Object
  • Object
show all
Includes:
Amazon, Data, Rackspace, Resize, Util
Defined in:
lib/pushfile/upload.rb

Constant Summary collapse

IMAGE_REGEX =

Image regexp

/^.+(\.(jpg|jpeg|png|gif|bmp))$/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Amazon

#amazon, #amazon_cdn, #amazon_container

Methods included from Rackspace

#rackspace, #rackspace_cdn, #rackspace_container

Methods included from Util

#filename

Methods included from Resize

#resize!, #thumbnail!

Methods included from Data

#ajax_upload, #file_upload, #setup_data, #url_upload

Constructor Details

#initialize(o = {}) ⇒ Upload

Returns a new instance of Upload.



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
43
44
45
46
47
48
49
50
51
52
# File 'lib/pushfile/upload.rb', line 15

def initialize(o = {})
  # Convert option keys to symbols
  o = o.symbolize_keys

  # Set provider
  @provider = o[:provider] || Pushfile.provider || 'amazon'

  # Extract config
  config = (o.delete(:config).to_sym rescue nil) || :default

  # Default options
  o = {
    :cdn => send("#{@provider}_cdn"),
    :container => send("#{@provider}_container"),
    :max => Pushfile.settings[:upload_limit],
    :snet => Pushfile.mode == 'production',
    :provider => @provider
  }.merge(o)

  # Merge image config
  o = Pushfile.settings[:images][config].merge(o) rescue o

  # Storing options
  @options = o
  @type = o[:type]
  @cdn = o[:cdn]
  @container = o[:container]
  @snet = o[:snet]
  @max = o[:max]
  @width = o[:width]
  @height = o[:height]
  begin
    @service = send(o[:provider])
  rescue
    @status = {:error => 'upload_cant_connect_to_cdn'}
  end
  @data = setup_data
end

Instance Attribute Details

#cdnObject

Returns the value of attribute cdn.



13
14
15
# File 'lib/pushfile/upload.rb', line 13

def cdn
  @cdn
end

#containerObject

Returns the value of attribute container.



13
14
15
# File 'lib/pushfile/upload.rb', line 13

def container
  @container
end

#dataObject

Returns the value of attribute data.



13
14
15
# File 'lib/pushfile/upload.rb', line 13

def data
  @data
end

#fileObject

Returns the value of attribute file.



13
14
15
# File 'lib/pushfile/upload.rb', line 13

def file
  @file
end

#heightObject

Returns the value of attribute height.



13
14
15
# File 'lib/pushfile/upload.rb', line 13

def height
  @height
end

#maxObject

Returns the value of attribute max.



13
14
15
# File 'lib/pushfile/upload.rb', line 13

def max
  @max
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/pushfile/upload.rb', line 13

def name
  @name
end

#providerObject

Returns the value of attribute provider.



13
14
15
# File 'lib/pushfile/upload.rb', line 13

def provider
  @provider
end

#serviceObject

Returns the value of attribute service.



13
14
15
# File 'lib/pushfile/upload.rb', line 13

def service
  @service
end

#statusObject

Returns the value of attribute status.



13
14
15
# File 'lib/pushfile/upload.rb', line 13

def status
  @status
end

#thumbObject

Returns the value of attribute thumb.



13
14
15
# File 'lib/pushfile/upload.rb', line 13

def thumb
  @thumb
end

#timestampObject

Returns the value of attribute timestamp.



13
14
15
# File 'lib/pushfile/upload.rb', line 13

def timestamp
  @timestamp
end

#typeObject

Returns the value of attribute type.



13
14
15
# File 'lib/pushfile/upload.rb', line 13

def type
  @type
end

#widthObject

Returns the value of attribute width.



13
14
15
# File 'lib/pushfile/upload.rb', line 13

def width
  @width
end

Instance Method Details

#createObject

Create upload



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/pushfile/upload.rb', line 55

def create
  @file = @data.is_a?(String) ? File.open(@data) : @data[:tempfile]

  # Return error if no file
  return (@status = {:error => 'upload_file_not_found'}) unless @file

  @name = filename(@data.is_a?(String) ? @data : @data[:filename])

  # Check if it's more than max or return error
  return (@status = {:error => 'upload_file_size_is_too_big'}) if @max and @file.size > @max

  # Resize file and create thumbnail for image
  if @name =~ IMAGE_REGEX
    resize! if @width or @height
    thumbnail!
  end

  # Store timestamp and type
  @timestamp = @data[:timestamp].nil? ? Time.now.to_i.to_s : @data[:timestamp].to_s
  @type = @data[:type]

  begin
    # Upload file
    @service.directories.new(:key => @container).files.create(
      :content_type => @type,
      :key => (@timestamp ? "#{@timestamp}_#{@name}" : @name),
      :body => @file
    )

    # Upload thumbnail
    if @thumb
      @service.directories.new(:key => @container).files.create(
        :content_type => @type,
        :key => "#{@timestamp}_#{@thumb}",
        :body => File.open("/tmp/#{@thumb}")
      )
      # Delete thumb file
      File.delete("/tmp/#{@thumb}")
    end
  rescue => x
    # Can't connect, report exception.
    @status = {:error => 'upload_cant_connect_to_cdn'}

  else
    thumb_url = @thumb ? "#{@cdn}/#{@timestamp}_#{@thumb}" : nil
    @status = {:url => "#{@cdn}/#{@timestamp}_#{@name}", :thumb_url => thumb_url, :size => @file.size, :mimetype => @type}
    @status
  end
end

#destroy(key) ⇒ Object

Destroy



106
107
108
109
110
111
112
# File 'lib/pushfile/upload.rb', line 106

def destroy(key)
  begin
    @service.directories.new(:key => @container).files.new(:key => key).destroy
  rescue
    @status = {:error => 'upload_cant_connect_to_cdn'}
  end
end