Module: StickyFlag::Tags::PNG

Defined in:
lib/stickyflag/tags/png.rb

Class Method Summary collapse

Class Method Details

.clear(file_name) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/stickyflag/tags/png.rb', line 55

def clear(file_name)
  image = ChunkyPNG::Image.from_file(file_name)
  image..delete('X-StickyFlag-Flags')

  outpath = File.tmpnam('.png')
  image.save(outpath)
  FileUtils.mv(outpath, file_name)
end

.extensionsObject



15
16
17
# File 'lib/stickyflag/tags/png.rb', line 15

def extensions
  [ '.png' ]
end

.get(file_name) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/stickyflag/tags/png.rb', line 19

def get(file_name)
  image = ChunkyPNG::Image.from_file(file_name)
  tag_string = image.['X-StickyFlag-Flags']
  return [] if tag_string.nil?
  tag_string.force_encoding('UTF-8') if RUBY_VERSION >= "1.9.0"
  tag_string.split(',').map { |t| t.empty? ? nil : t.strip }.compact
end

.set(file_name, tag) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/stickyflag/tags/png.rb', line 27

def set(file_name, tag)
  tags = get(file_name)
  return if tags.include? tag

  tags << tag

  image = ChunkyPNG::Image.from_file(file_name)
  image.['X-StickyFlag-Flags'] = tags.join(', ')

  outpath = File.tmpnam('.png')
  image.save(outpath)
  FileUtils.mv(outpath, file_name)
end

.unset(file_name, tag) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/stickyflag/tags/png.rb', line 41

def unset(file_name, tag)
  tags = get(file_name)
  return unless tags.include? tag

  tags.delete(tag)

  image = ChunkyPNG::Image.from_file(file_name)
  image.['X-StickyFlag-Flags'] = tags.join(', ')

  outpath = File.tmpnam('.png')
  image.save(outpath)
  FileUtils.mv(outpath, file_name)
end