Module: JekyllIS::Images::Image::Cache

Extended by:
Cache
Included in:
Cache, SEO, Transform
Defined in:
lib/jekyll-is/images/image/cache.rb

Instance Method Summary collapse

Instance Method Details

#cached_file(context, prefix, digest, suffix) {|target| ... } ⇒ Array<String, String, String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns full path, inner path, target url.

Parameters:

Yields:

  • File creation

Yield Parameters:

  • target (String)

    Path for resulting file

Yield Returns:

  • (void)

    ignored

Returns:

  • (Array<String, String, String>)

    full path, inner path, target url



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/jekyll-is/images/image/cache.rb', line 59

def cached_file context, prefix, digest, suffix
  splitted = split_digest context, digest
  path = "#{ context.cache_path }/#{ prefix }/#{ splitted }#{ suffix }"
  url = "/#{ context.target_path_prefix }/#{ splitted }#{ suffix }"
  full = context.site.in_source_dir path
  unless File.file?(full)
    FileUtils.mkdir_p File.dirname(full)
    yield full if block_given?
  end
  if File.file?(full)
    [ full, path, url ]
  else
    [  nil,  nil, nil ]
  end
end

#clean_unused_files(context) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/jekyll-is/images/image/cache.rb', line 125

def clean_unused_files context
  cache_path = context.site.in_source_dir context.cache_path
  cache_files = Dir[ "#{ cache_path }/**/*" ].select { File.file? it }
  static_files = context.site.static_files.map(&:path).map { it.start_with?('/') ? it : context.site.in_source_dir(it) }
  orphan_files = cache_files - static_files
  File.delete(*orphan_files)
  Jekyll::logger.info 'jekyll-is-images', "Cleanup: #{ orphan_files.size } files deleted."
  Dir[ "#{ cache_path }/**/*" ].select { File.directory? it }.reverse_each do |dir|
    Dir.rmdir dir if (Dir.entries(dir) - %w[. ..]).empty?
  end
end

#magick_image(context, digest, suffix) {|target| ... } ⇒ MiniMagick::Image

Parameters:

Yields:

  • Image generation code.

Yield Parameters:

  • target (String)

    Path for result (full).

Yield Returns:

  • (void)

    ignored

Returns:

  • (MiniMagick::Image)


82
83
84
85
86
# File 'lib/jekyll-is/images/image/cache.rb', line 82

def magick_image context, digest, suffix, &block
  full, path, _ = cached_file(context, TMP, digest, suffix, &block)
  raise "File not found: #{ path.inspect }" unless full && File.file?(full)
  MiniMagick::Image::open full
end

#restore_static_files(context) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/jekyll-is/images/image/cache.rb', line 110

def restore_static_files context
  cache_path = context.site.in_source_dir context.cache_path
  generated_path = "#{ cache_path }/#{ GEN }"
  if File.directory?(generated_path)
    cache_files = Dir[ '**/*', base: generated_path ].select { File.file? "#{ generated_path }/#{ it }" }
    cache_files.each do |file|
      url = "/#{ context.target_path_prefix }/#{ file }"
      context.site.static_files << IS::StaticFile::new(context.site, '/', url, source: "#{ context.cache_path }/#{ GEN }/#{ file }")
    end
  end
end

#source_digest(context, source, **params) ⇒ String

Parameters:

Returns:

  • (String)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/jekyll-is/images/image/cache.rb', line 92

def source_digest context, source, **params
  sha256 = Digest::SHA256::new
  sha256.update source if source
  unless source.nil? || source.empty? || source.start_with?('http://') || source.start_with?('https://')
    full = context.site.in_source_dir source
    File::open full, 'rb' do |file|
      while chunk = file.read(65536)
        sha256.update chunk
      end
    end
  end
  sha256.update JSON.generate(params, sort_keys: true)
  sha256.hexdigest.tr('ad', 'ot')          # Чтобы нигде не случилось участка пути /ad/, который режут баннерорезки...
end

#static_info(context, digest, format) {|target| ... } ⇒ JekyllIS::Images::Image::Info

Parameters:

Yields:

  • Image generation code.

Yield Parameters:

  • target (String)

    Path for result (full).

Yield Returns:

  • (void)

    ignored

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jekyll-is/images/image/cache.rb', line 29

def static_info context, digest, format, &block
  full, path, url = cached_file(context, GEN, digest, '.' + format, &block)
  raise "File not found: #{ path.inspect }" unless full && File.file?(full)
  second = if path.start_with?('/')
    path[1..]
  else
    "/#{ path }"
  end
  static = context.site.static_files.find { it.relative_path == path || it.relative_path == second || it.path == path || it.path == second }
  unless static
    static = IS::StaticFile::new context.site, '/', url, source: path
    context.site.static_files << static
  end
  props = jekyll_cache.getset digest do
    image = MiniMagick::Image::open full
    result = { width: image.width&.to_i, height: image.height&.to_i }
    image&.destroy!
    result
  end
  JekyllIS::Images::Image::Info[static.url, props[:width], (props[:width] && props[:height] ? props[:width].to_r / props[:height].to_r : nil)]
end