Module: Glib::DynamicImagesHelper

Defined in:
app/helpers/glib/dynamic_images_helper.rb

Defined Under Namespace

Classes: EncryptionService

Instance Method Summary collapse

Instance Method Details

#glib_dynamic_image_url(blob_key, width: 100, height: 100, fit: 'clip', https: true, host: 'imageserver-demo.herokuapp.com', port: nil, bucket: Rails.application.config.try(:aws_s3_bucket), signature_key: nil) ⇒ Object

NOTE: The bucket should probably be set as a parameter for json_libs



4
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
43
44
# File 'app/helpers/glib/dynamic_images_helper.rb', line 4

def glib_dynamic_image_url(blob_key,
    width: 100,
    height: 100,
    fit: 'clip',
    https: true,
    host: 'imageserver-demo.herokuapp.com',
    port: nil,
    bucket: Rails.application.config.try(:aws_s3_bucket),
    signature_key: nil)
  return unless blob_key.present?

  full_params_hash = {
    bucket_name: bucket,
    blob_key: blob_key,
    w: width,
    h: height,
    fit: fit,
    # expires: 1.hours.from_now.to_i
    expires: 1.month.from_now.end_of_month.to_i
  }

  request_params_hash = full_params_hash.except(:bucket_name, :blob_key)

  if (private_key = signature_key)
    encryption_service = EncryptionService.new(bucket, private_key)
    # This produces different signature every time.
    # TODO: Use digest instead, because we really only need to verify. We don't need to be able to decrypt.
    signature = encryption_service.encrypt(full_params_hash)
    request_params_hash = request_params_hash.merge(signature: signature)
  end

  request_builder = https ? URI::HTTPS : URI::HTTP
  uri = request_builder.build(
    host: host,
    port: port,
    path: "/image/#{bucket}/#{blob_key}",
    query: request_params_hash.to_param
  )

  uri.to_s
end