Class: Photo

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/models/photo.rb

Defined Under Namespace

Classes: EXIFData

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.guid(path, date) ⇒ Object



9
10
11
12
13
14
# File 'lib/models/photo.rb', line 9

def self.guid(path,date)
  Digest::MD5.hexdigest([
    path,
    date
  ].join(''))
end

.name_from_path(path) ⇒ Object



113
114
115
# File 'lib/models/photo.rb', line 113

def self.name_from_path(path)
  path.split('/').pop.gsub(/\.[a-zA-Z]{1,4}/,'').gsub(/^[\d]{1,3}\-/,'')
end

Instance Method Details

#full_pathObject



16
17
18
# File 'lib/models/photo.rb', line 16

def full_path
  File.join(PhotoFolder.source_directory_location,path)
end

#meta_file_pathObject



52
53
54
# File 'lib/models/photo.rb', line 52

def meta_file_path
  File.join(full_path.gsub(/\.(jpg|png)$/,'.json'))
end

#set_default_exif_dataObject



74
75
76
77
78
# File 'lib/models/photo.rb', line 74

def set_default_exif_data
  self.height = 0
  self.width = 0
  @tags = []
end

#set_exif_dataObject



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

def set_exif_data
  if full_path.match(/\.(png|jpg)$/)
    if PhotoFolder.mini_exiftool_available?
      set_exif_data_with_mini_exiftool
    else
      set_exif_data_without_mini_exiftool
    end
  else
    set_default_exif_data
  end
end

#set_exif_data_with_mini_exiftoolObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/models/photo.rb', line 80

def set_exif_data_with_mini_exiftool
  exif = EXIFData.new(MiniExiftool.new(full_path),logger)
  [:focal_length,:iso,:shutter_speed,:f_stop,:camera,:caption,:location,:height,:width].each do |key|
    self.send(key.to_s + '=',exif[key] || nil)
  end
  #date,title,lens and tags are special cases
  if exif[:date].is_a? Time
    self.date = exif[:date].to_i
  else
    date_match = (exif[:date] || '').match(/([\d]+):([\d]+):([\d]+) ([\d]+):([\d]+):([\d]+)/)
    if date_match
      self.date = (Time.mktime(date_match[1],date_match[2],date_match[3],date_match[4],date_match[5],date_match[6])).to_i
    end
  end
  self.name = exif[:title] || Photo.name_from_path(full_path)
  self.lens = exif[:lens] ? exif[:lens] : nil
  @tags = exif[:tags] || []
end

#set_exif_data_without_mini_exiftoolObject



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/models/photo.rb', line 99

def set_exif_data_without_mini_exiftool
  self.name = Photo.name_from_path(full_path)
  if(full_path.match(/\.png$/))
    width, height = File.read(full_path)[0x10..0x18].unpack('NN')
  else
    jpeg_meta_info = JPEGMetaInfo.new(full_path)
    width = jpeg_meta_info.width
    height = jpeg_meta_info.height
  end
  self.width = width
  self.height = height
  @tags = []
end

#set_guidObject



29
30
31
# File 'lib/models/photo.rb', line 29

def set_guid
  self.guid = Photo.guid(path,date)
end

#set_last_modifiedObject



48
49
50
# File 'lib/models/photo.rb', line 48

def set_last_modified
  self.last_modified = File.mtime(full_path)
end

#set_metaObject



56
57
58
59
60
# File 'lib/models/photo.rb', line 56

def set_meta
   = File.exists?(meta_file_path) ? File.read(meta_file_path) : nil
  update_attribute :meta, 
  
end

#set_nsfwObject



44
45
46
# File 'lib/models/photo.rb', line 44

def set_nsfw
  self.nsfw = @tags.collect(&:downcase).include?('nsfw') ? 1 : 0
end

#set_tagsObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/models/photo.rb', line 33

def set_tags
  #@tags was set by set_exif_data, which is always called first
  self.photo_taggings.destroy_all
  @tags.each do |str|
    self.photo_taggings.create({
      :photo_tag_id => (PhotoTag.find_by_tag(str) || PhotoTag.create(:tag => str)).id,
      :photo_id => self.id
    })
  end
end

#update_infoObject



20
21
22
23
24
25
26
27
# File 'lib/models/photo.rb', line 20

def update_info
  set_exif_data
  set_last_modified
  set_guid
  set_nsfw
  save
  set_tags
end