Class: DailyShare::Photo

Inherits:
Sequel::Model
  • Object
show all
Defined in:
lib/dailyshare/models.rb

Instance Method Summary collapse

Instance Method Details

#before_saveObject



7
8
9
# File 'lib/dailyshare/models.rb', line 7

def before_save
  description.strip!
end

#file(size) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/dailyshare/models.rb', line 11

def file(size)
  case size
    when :original
      "#{date_added}-#{member.name}.jpg"
    when :thumb
      "#{date_added}-#{member.name}-thumb.jpg"
    when :big
      "#{date_added}-#{member.name}-big.jpg"
  end
end

#generate_sizesObject



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

def generate_sizes
  img = Magick::Image::read(File.join(CONFIG['photo_dir'],file(:original))).first
  if img
    img.resize_to_fit(240,240).write(File.join(CONFIG['photo_dir'],file(:thumb)))
    img.resize_to_fit(960,680).write(File.join(CONFIG['photo_dir'],file(:big)))
    img.destroy!
    true
  else
    false
  end
end

#parse_exifObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dailyshare/models.rb', line 64

def parse_exif
  img = EXIFR::JPEG.new(file)

  # store fstop/focal length for comparison
  fstop = img.exif[:f_number].to_f
  flen = img.exif[:focal_length].to_f

  {
    # if no decimal is needed, leave it off
    :fstop => (fstop == fstop.to_i ? fstop.to_i : fstop),
    # convert exposure to string
    :exposure => img.exif[:exposure_time].to_s,
    # if no decimal is needed, leave it off
    :focal_length => (flen == flen.to_i ? flen.to_i : flen).to_s+'mm'
  }
end

#save_original(tmpfile) ⇒ Object



26
27
28
29
30
# File 'lib/dailyshare/models.rb', line 26

def save_original(tmpfile)
  image = File.join(CONFIG['photo_dir'],file(:original))
  File.open(image,'wb') {|f| f.write(tmpfile.read) }
  image
end

#save_to_s3Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dailyshare/models.rb', line 50

def save_to_s3
  sizes.each do |file|
    AWS::S3::S3Object.store(
      "/photos/#{File.basename(file)}",
      open(file),
      AUTH['aws']['bucket'],
      {
        :cache_control => 'max-age=315360000',
        :access => 'public_read'
      }
    )
  end
end

#sizesObject



44
45
46
47
48
# File 'lib/dailyshare/models.rb', line 44

def sizes
  [File.join(CONFIG['photo_dir'],file(:original)),
   File.join(CONFIG['photo_dir'],file(:thumb)),
   File.join(CONFIG['photo_dir'],file(:big))]
end

#url(size) ⇒ Object



22
23
24
# File 'lib/dailyshare/models.rb', line 22

def url(size)
  "#{CONFIG['url']['cdn']}/photos/#{file(size)}"
end