Class: Alchemy::ReorientPictures

Inherits:
Object
  • Object
show all
Extended by:
Shell
Defined in:
lib/alchemy/tasks/reorient_pictures.rb

Overview

Bakes the EXIF orientation into legacy picture masters.

Masters uploaded before auto orientation landed still carry an EXIF orientation tag. Clients honor it for the original image, but not once it is converted to WebP, which renders such variants rotated in Chrome and Firefox.

Constant Summary collapse

UPRIGHT_ORIENTATIONS =

Also covers "" and "Undefined", i.e. no orientation tag at all.

["", "Undefined", "TopLeft"].freeze

Constants included from Shell

Shell::COLORS

Class Method Summary collapse

Methods included from Shell

add_todo, desc, display_todos, log, silence!, silenced?, todo, todos, verbose!

Class Method Details

.call(dry_run: false, picture_ids: nil) ⇒ Object



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
# File 'lib/alchemy/tasks/reorient_pictures.rb', line 18

def call(dry_run: false, picture_ids: nil)
  unless Alchemy.storage_adapter.dragonfly?
    log "Auto orientation only applies to the Dragonfly storage adapter", :skip
    return []
  end

  log "Dry run - no changes will be made", :message if dry_run

  affected = []
  skipped = 0
  # Dragonfly and ActiveJob log every shell command and enqueued job, which
  # floods the output. Silence them for the duration of the run.
  with_quiet_logging do
    # "." upright, "f" needs reorienting (report), "o" reoriented,
    # "s" master file missing.
    scope(picture_ids).find_each do |picture|
      next unless picture.has_convertible_format?

      if UPRIGHT_ORIENTATIONS.include?(orientation_of(picture))
        print_progress "."
        next
      end

      affected << picture.id
      reorient!(picture) unless dry_run
      print_progress(dry_run ? "f" : "o")
    rescue *Array(Alchemy.storage_adapter.rescuable_errors)
      affected.delete(picture.id)
      skipped += 1
      print_progress "s"
    end
  end
  finish_progress

  log "#{dry_run ? "Found" : "Reoriented"} #{affected.size} picture(s) that need reorienting"
  log "Skipped #{skipped} picture(s) whose master file is missing", :error if skipped.positive?
  if dry_run && affected.any?
    log "Reorient them with: rake alchemy:pictures:reorient PICTURE_IDS=#{affected.join(",")}"
  end
  affected
end

.report(picture_ids: nil) ⇒ Object



60
61
62
# File 'lib/alchemy/tasks/reorient_pictures.rb', line 60

def report(picture_ids: nil)
  call(dry_run: true, picture_ids:)
end