Module: PhotoCook::Resize::Assemble

Defined in:
lib/photo-cook/resize/assemble.rb

Class Method Summary collapse

Class Method Details

.assemble_public_path(root) ⇒ Object

Path to public directory

Arguments:

root => /application

Returns /application/public

NOTE: This method performs no validation



93
94
95
# File 'lib/photo-cook/resize/assemble.rb', line 93

def assemble_public_path(root)
  File.join(root, PhotoCook.public_dir)
end

.assemble_resize_uri(source_uri, width, height, mode) ⇒ Object

Returns URI which points to PhotoCook::Resize::Middleware

Arguments:

source_uri  => /uploads/photos/1/car.png
width       => :auto
height      => 640
mode        => fit

Returns /uploads/photos/1/resized/width=auto&height=640&mode=fit/car.png

NOTE: This method performs no validation NOTE: This method is very hot



20
21
22
# File 'lib/photo-cook/resize/assemble.rb', line 20

def assemble_resize_uri(source_uri, width, height, mode)
  source_uri.split('/').insert(-2, PhotoCook::Resize.cache_dir, Command.assemble(width, height, mode)).join('/')
end

.assemble_source_path_from_normal_uri(root, normal_uri) ⇒ Object



65
66
67
# File 'lib/photo-cook/resize/assemble.rb', line 65

def assemble_source_path_from_normal_uri(root, normal_uri)
  File.join(assemble_public_path(root), normal_uri)
end

.assemble_source_path_from_resize_uri(root, resize_uri) ⇒ Object

Path where source photo is stored

Arguments:

root       => /application
resize_uri => /uploads/photos/1/resized/width=auto&height=640&mode=fit/car.png

Returns /application/public/uploads/photos/1/car.png

NOTE: This method performs no validation



61
62
63
# File 'lib/photo-cook/resize/assemble.rb', line 61

def assemble_source_path_from_resize_uri(root, resize_uri)
  assemble_source_path_from_normal_uri(root, disassemble_resize_uri(resize_uri))
end

.assemble_store_path(root, source_path, assembled_command) ⇒ Object

Path where resized photo is stored

Arguments:

root              => /application
source_path       => /application/public/uploads/photos/1/car.png
assembled_command => width=auto&height=640&mode=fit

Returns /application/public/resized/uploads/photos/1/width=auto&height=640&mode=fit/car.png

NOTE: This method performs no validation



79
80
81
82
83
# File 'lib/photo-cook/resize/assemble.rb', line 79

def assemble_store_path(root, source_path, assembled_command)
  public         = assemble_public_path(root)
  photo_location = dirname_or_blank(source_path.split(public).last)
  File.join(public, PhotoCook::Resize.cache_dir, photo_location, assembled_command, File.basename(source_path))
end

.disassemble_resize_uri(resize_uri) ⇒ Object

Strips resize command from URI. Inverse of assemble_resize_uri

Arguments:

resize_uri => /uploads/photos/1/resized/width=auto&height=640&mode=fit/car.png

Returns /uploads/photos/1/car.png

NOTE: This method performs no validation



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/photo-cook/resize/assemble.rb', line 32

def disassemble_resize_uri(resize_uri)
  # Take URI:
  # /uploads/photos/1/resized/width=auto&height=640&mode=fit/car.png
  #
  # Split by separator:
  # ["", "uploads", "photos", "1", "resized", "width=auto&height=640&mode=fit", "car.png"]
  #
  sections = resize_uri.split('/')

  # Delete PhotoCook directory:
  # ["", "uploads", "photos", "1", "width=auto&height=640&mode=fit", "car.png"]
  sections.delete_at(-3)

  # Delete command string:
  # ["", "uploads", "photos", "1", "car.png"]
  sections.delete_at(-2)

  sections.join('/')
end

.resize_uri?(uri) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
103
104
105
106
107
# File 'lib/photo-cook/resize/assemble.rb', line 97

def resize_uri?(uri)
  sections = uri.split('/')

  # Check if PhotoCook cache directory exists:
  #   sections[-3] => resized
  sections[-3] == PhotoCook::Resize.cache_dir &&

  # Check if valid resize command exists:
  #   sections[-2] => width=auto&height=640&mode=fit
  matches_regex?(sections[-2], Command.regex)
end