Class: ImageGallery::CompressedStaticGalleryFile

Inherits:
Jekyll::StaticFile
  • Object
show all
Defined in:
lib/gallery/gallery.rb

Constant Summary collapse

BROWSER_SUPPORTED_EXTENSIONS =
['.png', '.jpeg', '.jpg'].freeze
FALLBACK_FORMAT =
'png'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(site, base, source_path, source_name, dest_path, dest_name, &resize_fn) ⇒ CompressedStaticGalleryFile

Returns a new instance of CompressedStaticGalleryFile.



274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/gallery/gallery.rb', line 274

def initialize(site, base, source_path, source_name, dest_path, dest_name, &resize_fn)
  super(site, base, source_path, source_name)
  @dest_path = dest_path
  @dest_name = dest_name
  @resize_fn = resize_fn

  # Apples HEIC format isn't supported by browsers. Using filename isn't an ideal heuristic, but it is probably good
  # enough
  @convert_format = false
  unless BROWSER_SUPPORTED_EXTENSIONS.include?(File.extname(@dest_name).downcase)
    @convert_format = true
    @dest_name = "#{File.basename(@dest_name, '.*')}.#{FALLBACK_FORMAT}"
  end
end

Instance Method Details

#destination(dest) ⇒ Object



289
290
291
# File 'lib/gallery/gallery.rb', line 289

def destination(dest)
  File.join(dest, @dest_path, @dest_name)
end

#relative_destinationObject



293
294
295
# File 'lib/gallery/gallery.rb', line 293

def relative_destination
  File.join(@dest_path, @dest_name)
end

#write(dest) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/gallery/gallery.rb', line 297

def write(dest)
  dest_path = destination(dest)

  return false if File.exist?(dest_path) && (File.stat(path).mtime.to_i == File.stat(dest_path).mtime.to_i)

  FileUtils.mkdir_p(File.dirname(dest_path))
  FileUtils.rm(dest_path) if File.exist?(dest_path)

  image = MiniMagick::Image.open(path)
  image.combine_options do |i|
    i.auto_orient
    i.strip
    @resize_fn.call(i)
  end
  image.format FALLBACK_FORMAT if @convert_format
  image.write(dest_path)

  File.utime(File.atime(dest_path), mtime, dest_path)

  true
end