Class: Thumbor::CryptoURL

Inherits:
Object
  • Object
show all
Defined in:
lib/thumbor/crypto_url.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = false) ⇒ CryptoURL

Returns a new instance of CryptoURL.



10
11
12
# File 'lib/thumbor/crypto_url.rb', line 10

def initialize(key=false)
    @key = key
end

Instance Attribute Details

#computed_keyObject

Returns the value of attribute computed_key.



8
9
10
# File 'lib/thumbor/crypto_url.rb', line 8

def computed_key
  @computed_key
end

Instance Method Details

#calculate_centered_crop(options) ⇒ Object



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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/thumbor/crypto_url.rb', line 56

def calculate_centered_crop(options)
    width = options[:width]
    height = options[:height]
    original_width = options[:original_width]
    original_height = options[:original_height]
    center = options[:center]

    return unless original_width &&
                    original_height &&
                    center &&
                    (width || height)

    unless center.kind_of?(Array) && center.length == 2
        raise 'center must be an array of x,y'
    end

    center_x, center_y = center
    width ||= original_width
    height ||= original_height
    width = width.abs
    height = height.abs
    new_aspect_ratio = width / height.to_f
    original_aspect_ratio = original_width/original_height.to_f

    crop = nil
    # We're going wider, vertical crop
    if new_aspect_ratio > original_aspect_ratio
      # How tall should we be? because new_aspect_ratio is > original_aspect_ratio we can be sure
      # that cropped_height is always less than original_height (original). This is assumed below.
      cropped_height = (original_width / new_aspect_ratio).round
      # Calculate coordinates around center
      top_crop_point = (center_y - (cropped_height * 0.5)).round
      bottom_crop_point = (center_y + (cropped_height * 0.5)).round

      # If we've gone above the top of the image, take all from the bottom
      if top_crop_point < 0
        top_crop_point = 0
        bottom_crop_point = cropped_height
      # If we've gone below the top of the image, take all from the top
      elsif bottom_crop_point > original_height
        top_crop_point = original_height - cropped_height
        bottom_crop_point = original_height
      # Because cropped_height < original_height, top_crop_point and
      # bottom_crop_point will never both be out of bounds
      end

      # Put it together
      crop = [0, top_crop_point, original_width, bottom_crop_point]
    # We're going taller, horizontal crop
    elsif new_aspect_ratio < original_aspect_ratio
      # How wide should we be? because new_aspect_ratio is < original_aspect_ratio we can be sure
      # that cropped_width is always less than original_width (original). This is assumed below.
      cropped_width = (original_height * new_aspect_ratio).round
      # Calculate coordinates around center
      left_crop_point = (center_x - (cropped_width * 0.5)).round
      right_crop_point = (center_x + (cropped_width * 0.5)).round

      # If we've gone beyond the left of the image, take all from the right
      if left_crop_point < 0
        left_crop_point = 0
        right_crop_point = cropped_width
      # If we've gone beyond the right of the image, take all from the left
      elsif right_crop_point > original_width
        left_crop_point = original_width - cropped_width
        right_crop_point = original_width
      # Because cropped_width < original_width, left_crop_point and
      # right_crop_point will never both be out of bounds
      end

      # Put it together
      crop = [left_crop_point, 0, right_crop_point, original_height]
    end

    options[:crop] = crop
end

#calculate_width_and_height(url_parts, options) ⇒ Object



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
53
54
# File 'lib/thumbor/crypto_url.rb', line 22

def calculate_width_and_height(url_parts, options)
    width = options[:width]
    height = options[:height]

    if width and options[:flip]
        width = width * -1
    end
    if height and options[:flop]
        height = height * -1
    end

    if width or height
        width = 0 if not width
        height = 0 if not height
    end

    has_width = width
    has_height = height
    if options[:flip] and not has_width and not has_height
        width = "-0"
        height = '0' if not has_height and not options[:flop]
    end
    if options[:flop] and not has_width and not has_height
        height = "-0"
        width = '0' if not has_width and not options[:flip]
    end

    if width or height
        width = width.to_s
        height = height.to_s
        url_parts.push(width << 'x' << height)
    end
end

#generate(options) ⇒ Object



235
236
237
238
# File 'lib/thumbor/crypto_url.rb', line 235

def generate(options)
    return generate_old(options) if options[:old]
    generate_new(options)
end

#generate_new(options) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/thumbor/crypto_url.rb', line 218

def generate_new(options)
    thumbor_path = ""

    image_options = url_for(options, false)
    thumbor_path << image_options + '/' unless image_options.empty?

    thumbor_path << options[:image]

    if @key
        signature = url_safe_base64(OpenSSL::HMAC.digest('sha1', @key, thumbor_path))
        thumbor_path.insert(0, "/#{signature}/")
    else
        thumbor_path.insert(0, "/unsafe/")
    end
    thumbor_path
end

#generate_old(options) ⇒ Object



208
209
210
211
212
213
214
215
216
# File 'lib/thumbor/crypto_url.rb', line 208

def generate_old(options)
    url = pad(url_for(options))
    cipher = OpenSSL::Cipher::Cipher.new('aes-128-ecb').encrypt
    cipher.key = computed_key
    encrypted = cipher.update(url)
    based = url_safe_base64(encrypted)

    "/#{based}/#{options[:image]}"
end

#pad(s) ⇒ Object



18
19
20
# File 'lib/thumbor/crypto_url.rb', line 18

def pad(s)
    s + ("{" * (16 - s.length % 16))
end

#url_for(options, include_hash = true) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/thumbor/crypto_url.rb', line 132

def url_for(options, include_hash = true)
    if not options[:image]
        raise 'image is a required argument.'
    end

    url_parts = Array.new

    if options[:trim]
        trim_options  = ['trim']
        trim_options << options[:trim] unless options[:trim] == true or options[:trim][0] == true
        url_parts.push(trim_options.join(':'))
    end

    if options[:meta]
        url_parts.push('meta')
    end

    calculate_centered_crop(options)

    crop = options[:crop]
    if crop
        crop_left = crop[0]
        crop_top = crop[1]
        crop_right = crop[2]
        crop_bottom = crop[3]

        if crop_left > 0 or crop_top > 0 or crop_bottom > 0 or crop_right > 0
            url_parts.push(crop_left.to_s << 'x' << crop_top.to_s << ':' << crop_right.to_s << 'x' << crop_bottom.to_s)
        end
    end

    [:fit_in, :adaptive_fit_in, :full_fit_in, :adaptive_full_fit_in].each do |fit|
        if options[fit]
            url_parts.push(fit.to_s.gsub('_','-'))
        end
    end

    if options.include?(:fit_in) or options.include?(:full_fit_in) and not (options.include?(:width) or options.include?(:height))
        raise ArgumentError, 'When using fit-in or full-fit-in, you must specify width and/or height.'
    end

    calculate_width_and_height(url_parts, options)

    if options[:halign] and options[:halign] != :center
        url_parts.push(options[:halign])
    end

    if options[:valign] and options[:valign] != :middle
        url_parts.push(options[:valign])
    end

    if options[:smart]
        url_parts.push('smart')
    end

    if options[:filters] && !options[:filters].empty?
      filter_parts = []
      options[:filters].each do |filter|
        filter_parts.push(filter)
      end

      url_parts.push("filters:#{ filter_parts.join(':') }")
    end

    if include_hash
        image_hash = Digest::MD5.hexdigest(options[:image])
        url_parts.push(image_hash)
    end

    return url_parts.join('/')
end

#url_safe_base64(str) ⇒ Object



204
205
206
# File 'lib/thumbor/crypto_url.rb', line 204

def url_safe_base64(str)
    Base64.encode64(str).gsub('+', '-').gsub('/', '_').gsub!(/[\n]/, '')
end