Module: GitMedia::FilterSmudge

Defined in:
lib/git-media/filter-smudge.rb

Class Method Summary collapse

Class Method Details

.run!Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/git-media/filter-smudge.rb', line 4

def self.run!
  media_buffer = GitMedia.get_media_buffer
  can_download = false # TODO: read this from config and implement
  
  # read checksum size
  sha = STDIN.readpartial(64).lines.first.chomp # read no more than 64 bytes
  if STDIN.eof? && sha.length == 40 && sha.match(/^[0-9a-fA-F]+$/) != nil
    # this is a media file
    media_file = File.join(media_buffer, sha.chomp)
    if File.exists?(media_file)
      STDERR.puts('recovering media : ' + sha)
      File.open(media_file, 'r') do |f|
        while data = f.read(4096) do
          print data
        end
      end
    else
      # TODO: download file if not in the media buffer area
      if !can_download
        STDERR.puts('media missing, saving placeholder : ' + sha)
        puts sha
      end
    end
  else
    # if it is not a 40 character long hash, just output
    STDERR.puts('Unknown git-media file format')
    print sha
    while data = STDIN.read(4096)
      print data
    end
  end
end