2 class Picture < ActiveRecord::Base
3 belongs_to :original_picture
6 has_one :resource_picture
8 validates :original_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
9 validates :revision, :presence => true, :numericality => true
10 validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
11 validates :width, :presence => true, :numericality => true, :natural_number => true
12 validates :height, :presence => true, :numericality => true, :natural_number => true
13 validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
14 validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
15 validates :license_id, :presence => true, :numericality => true, :existence => {:both => false}
16 validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
17 validates :artist_name, :presence => true
18 validates :classname, :presence => true, :length => {:maximum => 50}
24 attr = {:width => rp.width, :height => rp.height, :ext => rp.ext, :filesize => rp.filesize,
25 :original_picture_id => rp.original_picture_id, :license_id => rp.license_id, :artist_id => rp.artist_id,
26 :md5 => rp.md5, :artist_name => rp.artist_name, :classname => rp.classname, :credit => rp.credit, :settings => rp.settings
28 self.attributes = attr
29 self.revision = self.new_revision #Do not move to attr. new_revision reffernces self.original_picture_id
34 self.artist_id == ar.artist.id
35 elsif ar.is_a?(Artist)
36 self.artist_id == ar.id
46 def showable? au = nil
47 return false unless self.original_picture
48 return true if self.own?(au)
49 self.enable? and self.head?
53 "#{self.id}.#{self.ext}"
65 '/pictures/' + filename
69 {:src => self.url, :width => self.width, :height => self.height}
73 tw, th = PettanImager.thumbnail_size(self.width, self.height)
74 {:src => self.url, :width => tw, :height => th}
77 def tail_opt_img_tag img
78 {:src => img, :width => self.width, :height => self.height}
81 def tail_tmb_opt_img_tag img
82 tw, th = PettanImager.thumbnail_size(self.width, self.height)
83 {:src => img, :width => tw, :height => th}
87 Picture.maximum(:revision, :conditions => ['original_picture_id = ?', self.original_picture_id]).to_i + 1
91 r = self.head.resource_picture
96 Picture.find(:first, :conditions => ['original_picture_id = ?', self.original_picture_id], :order => 'pictures.revision desc')
104 self.ext == 'png' and self.flag_gif_convert >= 0
108 flag_reverse < 0 ? [''] : ['', 'v', 'h', 'vh']
111 def self.find_by_md5 md5
112 r = Picture.find :all, :conditions => ['pictures.md5 = ?', md5], :order => 'pictures.updated_at desc'
115 def self.list_by_md5 md5, opid = nil
116 cond = if opid.blank?
117 ['pictures.md5 = ?', md5]
119 ['pictures.md5 = ? and pictures.original_picture_id <> ?', md5, opid]
121 r = Picture.find :all, :conditions => cond, :order => 'pictures.updated_at desc'
124 def self.exist_by_md5 md5, opid
125 Picture.list_by_md5(md5, opid).empty? ? false : true
128 def self.show rid, au
130 r = Picture.find(rid, opt)
131 raise ActiveRecord::Forbidden unless r.visible?(au)
138 if res = self.store_picture(imager, self.filename)
140 if gifimager = imager.to_gif
141 res = self.store_picture(gifimager, self.gifname)
151 def store_picture(imager, fn)
154 picdata = d.empty? ? imager.binary : imager.__send__(d)
155 res = PictureIO.picture_io.put(picdata, fn, d)
161 def restore(subdir = nil)
162 PictureIO.picture_io.get self.filename, subdir
166 imager = PettanImager.load(File.open(Rails.root + 'app/assets/images/error.png', 'rb').read)
167 return false unless imager
172 "#{self.classname.tableize}/attributes/credit"
175 def full_credit_template
176 "#{self.classname.tableize}/attributes/full_credit"
181 @credit_data = JSON.parse(self.credit) unless @credit_data
188 return @flags if @flags
190 @flags = JSON.parse(self.settings)
193 @flags = {} unless @flags
202 @flag_open = (flags["open"] || -1) unless @flag_open
207 @flag_commercial = (flags["commercial"] || -1) unless @flag_commercial
212 @flag_official = (flags["official"] || -1) unless @flag_official
217 @flag_attribution = (flags["attribution"] || -1) unless @flag_attribution
222 @flag_derive = (flags["derive"] || -1) unless @flag_derive
227 @flag_thumbnail = (flags["thumbnail"] || -1) unless @flag_thumbnail
232 @flag_gif_convert = (flags["gif_convert"] || -1) unless @flag_gif_convert
237 @flag_reverse = (flags["reverse"] || -1) unless @flag_reverse
242 @flag_resize = (flags["resize"] || -1) unless @flag_resize
247 @flag_sync_vh = (flags["sync_vh"] || -1) unless @flag_sync_vh
252 @flag_overlap = (flags["overlap"] || -1) unless @flag_overlap