Class: Archiver::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/archiver/record.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Record

Returns a new instance of Record.



13
14
15
# File 'lib/archiver/record.rb', line 13

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



11
12
13
# File 'lib/archiver/record.rb', line 11

def path
  @path
end

Instance Method Details

#checksumObject



59
60
61
# File 'lib/archiver/record.rb', line 59

def checksum
  @checksum ||= Digest::MD5.hexdigest(File.read(path))
end

#createdObject



51
52
53
54
55
56
57
# File 'lib/archiver/record.rb', line 51

def created
  if exif_data and exif_data.date_time_original
    exif_data.date_time_original
  else
    File.mtime(path)
  end
end

#exif_dataObject



70
71
72
# File 'lib/archiver/record.rb', line 70

def exif_data
  @exif_data ||= exif_detect
end

#extensionObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/archiver/record.rb', line 31

def extension
  if mime_type.match /^image\/jpeg$/i
    return '.jpg'
  end

  if mime_type.match /^image\/gif$/i
    return '.gif'
  end

  if mime_type.match /^image\/png$/i
    return '.png'
  end

  if mime_type.match /^video\/3gpp$/i
    return '.mp4'
  end

  File.extname(path).downcase
end

#filename(counter = 0) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/archiver/record.rb', line 17

def filename(counter = 0)
  append = if counter > 0
    sprintf('-%03d', counter)
  else
    nil
  end

  [
    created.strftime('%Y%m%d-%H%M%S'),
    append,
    extension
  ].compact.join
end

#image?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/archiver/record.rb', line 78

def image?
  !!(mime_type.match /^image\/.*/i)
end

#mime_typeObject



63
64
65
66
67
68
# File 'lib/archiver/record.rb', line 63

def mime_type
  @mime_type ||= mime_detect
    .split(';')
    .first
    .strip
end

#move(directory, counter = 0) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/archiver/record.rb', line 94

def move(directory, counter = 0)
  destination = File.join(directory, filename(counter))

  if File.exists? destination
    if same? destination
      FileUtils.rm_rf path
    else
      move(directory, counter + 1)
    end
  else
    FileUtils.cp path, destination
    FileUtils.rm_rf path
  end
end

#process?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/archiver/record.rb', line 74

def process?
  image? or video?
end

#same?(other) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
# File 'lib/archiver/record.rb', line 86

def same?(other)
  if other.is_a? Record
    checksum == other.checksum
  else
    checksum == Digest::MD5.hexdigest(File.read(other))
  end
end

#video?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/archiver/record.rb', line 82

def video?
  !!(mime_type.match /^video\/.*/i)
end