Class: MiddlemanSimpleThumbnailer::ImageStore

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman-simple-thumbnailer/image_store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeImageStore

Returns a new instance of ImageStore.



7
8
9
# File 'lib/middleman-simple-thumbnailer/image_store.rb', line 7

def initialize
  @tmp_path =  Dir::Tmpname.create('thumbnail', nil) {}
end

Instance Attribute Details

#tmp_pathObject (readonly)

Returns the value of attribute tmp_path.



5
6
7
# File 'lib/middleman-simple-thumbnailer/image_store.rb', line 5

def tmp_path
  @tmp_path
end

Instance Method Details

#deleteObject



37
38
39
# File 'lib/middleman-simple-thumbnailer/image_store.rb', line 37

def delete
  File.delete(@tmp_path) if File.exist?(@tmp_path)
end

#eachObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/middleman-simple-thumbnailer/image_store.rb', line 26

def each
  return unless File.exist?(@tmp_path)
  File.open(@tmp_path, "r") do |f|
    f.flock(File::LOCK_SH)
    resized_images = f.size > 0 ? Marshal.load(f) : {}
    resized_images.values.each do |store_entry|
      yield *store_entry
    end
  end
end

#store(img_path, resize_to) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/middleman-simple-thumbnailer/image_store.rb', line 11

def store(img_path, resize_to)
  File.open(@tmp_path, File::RDWR|File::CREAT, 0644) do |f|
    f.flock(File::LOCK_EX)
    resized_images = f.size > 0 ? Marshal.load(f) : {}
    file_key = "#{img_path}.#{resize_to}"
    if ! resized_images.has_key?(file_key)
      resized_images[file_key] = [img_path, resize_to]
      f.rewind
      Marshal.dump(resized_images,f)
      f.flush
      f.truncate(f.pos)
    end
  end
end