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



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

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

#createdObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/archiver/record.rb', line 62

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

#exif_dataObject



85
86
87
# File 'lib/archiver/record.rb', line 85

def exif_data
  @exif_data ||= exif_detect
end

#extensionObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/archiver/record.rb', line 38

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

  if mime_type.match /^video\/quicktime$/i
    return '.mov'
  end

  File.extname(path).downcase
end

#filename(counter = 0) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/archiver/record.rb', line 17

def filename(counter = 0)
  if created
    append = if counter > 0
      sprintf('-%03d', counter)
    else
      nil
    end
    
    [
      created.strftime('%Y%m%d-%H%M%S'),
      append,
      extension
    ].compact.join
  else
    [
      sprintf('%05d', counter),
      extension
    ].compact.join
  end
end

#image?Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
99
# File 'lib/archiver/record.rb', line 93

def image?
  if mime_type.match /^image\/.*/i
    true
  else
    false
  end
end

#mime_typeObject



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

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

#move(directory, counter = 0) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/archiver/record.rb', line 117

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

  unless File.directory? File.dirname(destination)
    FileUtils.mkdir_p File.dirname(destination)
  end

  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)


89
90
91
# File 'lib/archiver/record.rb', line 89

def process?
  image? or video?
end

#same?(other) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
112
113
114
115
# File 'lib/archiver/record.rb', line 109

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

#segmentObject



136
137
138
# File 'lib/archiver/record.rb', line 136

def segment
  created ? created.strftime('%Y') : '0000'
end

#video?Boolean

Returns:

  • (Boolean)


101
102
103
104
105
106
107
# File 'lib/archiver/record.rb', line 101

def video?
  if mime_type.match /^video\/.*/i
    true
  else
    false
  end
end