Class: DragonflyTranslator

Inherits:
Object
  • Object
show all
Defined in:
app/services/dragonfly_translator.rb

Instance Method Summary collapse

Instance Method Details

#process(url, auto_create_images: true, image_datetime: nil) ⇒ Object

Pass a legacy media url, will create image if requested, image datetime is used to affix a different timestamp so they are in chronological order



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
45
46
47
48
49
50
51
52
53
54
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
104
105
106
107
108
# File 'app/services/dragonfly_translator.rb', line 4

def process(url, auto_create_images: true, image_datetime: nil)
  image_datetime ||= Time.current
  job_64 = url.scan(/\/media\/([\w\-]+)\//)&.first&.first
  #Try legacy scan
  job_64 ||= url.scan(/\/media(?:\/.*\.\w{3,4})?\?job=(\w*)/)&.first&.first
  raise "No dragonfly job id detected" unless job_64
  job_info = Dragonfly::Serializer.json_b64_decode(job_64)

  puts job_info
  attachment_uid = nil
  encode = nil
  convert = nil
  size = nil
  width = nil
  height = nil
  crop_x = nil
  crop_y = nil
  crop_w = nil
  crop_h = nil
  percentage = nil
  job_info.each do |ops|
    puts "Operation: #{ops.inspect}"
    case ops[0]
    when 'f'
      attachment_uid = ops[1]
    when 'p'
      case ops[1]
      when 'encode'
        encode = ops[2]
      when 'convert'
        convert = ops[2]
        #parse resize string in convert if present
        #-resize '1400x838.4937238493724!'
        match_data = convert.scan(/-(?:resize|thumbnail) '?(\d+\.?\d+)x(\d+\.?\d+)(.?)'?/)&.first
        if match_data.present?
          width,height,modifier = match_data
          if width && height
            width = width&.to_f&.round
            height = height&.to_f&.round
            size = "#{width&.to_i}x#{height&.to_i}#{modifier}"
          end
        elsif match_data = convert.scan(/-crop ([\.\d]+)x([\.\d]+)\+([\.\d]+)\+([\.\d]+)/)&.first
          crop_w, crop_h, crop_x, crop_y = match_data
        elsif match_data = convert.scan(/-resize '(\d+)%'/)&.first&.first
          percentage = match_data
        end
      when 'thumb'
        size = ops[2]
      end
    end
  end
  puts "Attachment UID : #{attachment_uid}"
  raise "Could not parse attachment_uid" unless attachment_uid
  img = Image.find_by(attachment_uid: attachment_uid)
  unless img
    puts "Could not find image based on attachment_uid, let's create it"
    # location on file system
    #file = File.new("/Volumes/media1/public_assets/production/#{attachment_uid}")

    if auto_create_images
      # Try to imagify it
      file = Dragonfly.app.fetch(attachment_uid).file
      file_blob = Base64.encode64(file.read)
      slug = Addressable::URI.unescape(url.split('/').last.split('.').first).parameterize
      attachment_format = if File.binread(file.path, 3) == 'GIF'
                           'gif'
                        elsif File.binread(file.path, 3, 1) == 'PNG'
                           'png'
                        elsif File.binread(file.path, 4, 6) == 'JFIF'
                           'jpeg'
                        end
      img = Image.new
      img.attachment_format = attachment_format
      img.new_image = file_blob
      img.slug = slug
      img.title = slug.titleize
      img.tags = ['dragonfly-imported']
      img.created_at = image_datetime
      img.updated_at = image_datetime
      img.skip_notify = true
      img.save!
    else
      raise "#{attachment_uid} not found or auto_create_images not set"
    end
  end

  options = {
    encode_format: encode,
    hostname: "img.warmlyyours.com"
  }
  if size
    options[:size] = size
  elsif width || height
    options[:width] = width&.to_f&.round
    options[:height] = height&.to_f&.round
  elsif percentage
    options[:percentage] = percentage
  end
  options[:crop_x] = crop_x&.to_f&.round
  options[:crop_y] = crop_y&.to_f&.round
  options[:crop_h] = crop_h&.to_f&.round
  options[:crop_w] = crop_w&.to_f&.round
  img.image_url(**options)

end