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
33 roles = [roles] unless roles.respond_to?(:each)
34 ar = Picture.get_artist_from_roles roles
35 return false unless ar
36 self.artist_id == ar.id
43 def showable? au = nil
44 return false unless self.original_picture
45 return true if self.own?(au)
46 self.enable? and self.head?
50 "#{self.id}.#{self.ext}"
62 '/pictures/' + filename
66 {:src => self.url, :width => self.width, :height => self.height}
70 tw, th = PettanImager.thumbnail_size(self.width, self.height)
71 {:src => self.url, :width => tw, :height => th}
74 def tail_opt_img_tag img
75 {:src => img, :width => self.width, :height => self.height}
78 def tail_tmb_opt_img_tag img
79 tw, th = PettanImager.thumbnail_size(self.width, self.height)
80 {:src => img, :width => tw, :height => th}
84 Picture.maximum(:revision, :conditions => ['original_picture_id = ?', self.original_picture_id]).to_i + 1
88 r = self.head.resource_picture
93 Picture.find(:first, :conditions => ['original_picture_id = ?', opid], :order => 'pictures.revision desc')
97 Picture.head(self.original_picture_id)
105 self.ext == 'png' and self.flag_gif_convert >= 0
109 flag_reverse < 0 ? [''] : ['', 'v', 'h', 'vh']
112 def self.find_by_md5 md5
113 r = Picture.find :all, :conditions => ['pictures.md5 = ?', md5], :order => 'pictures.updated_at desc'
116 def self.list_by_md5 md5, opid = nil
117 cond = if opid.blank?
118 ['pictures.md5 = ?', md5]
120 ['pictures.md5 = ? and pictures.original_picture_id <> ?', md5, opid]
122 r = Picture.find :all, :conditions => cond, :order => 'pictures.updated_at desc'
125 def self.exist_by_md5 md5, opid
126 Picture.list_by_md5(md5, opid).empty? ? false : true
129 def self.show rid, roles
131 r = Picture.find(rid, opt)
132 raise ActiveRecord::Forbidden unless r.visible?(roles)
139 if res = self.store_picture(imager, self.filename)
141 if gifimager = imager.to_gif
142 res = self.store_picture(gifimager, self.gifname)
152 def store_picture(imager, fn)
155 picdata = d.empty? ? imager.binary : imager.__send__(d)
156 res = PictureIO.picture_io.put(picdata, fn, d)
162 def restore(subdir = nil)
163 PictureIO.picture_io.get self.filename, subdir
166 def self.export(dt = nil)
169 ['artists.author_id is not null and pictures.updated_at >= ?', dt]
171 'artists.author_id is not null'
173 opt.merge!({:conditions => cond})
174 opt.merge!({:include => {:artist => {}}, :order => 'pictures.updated_at desc'})
175 Picture.find(:all, opt)
178 def self.list_as_json_text ary
179 '[' + ary.map {|i| i.to_json_with_picture_data }.join(',') + ']'
183 Base64.encode64(self.restore)
186 def to_json_with_picture_data
187 self.to_json({:methods => :picture_data})
191 imager = PettanImager.load(File.open(Rails.root + 'app/assets/images/error.png', 'rb').read)
192 return false unless imager
197 "#{self.classname.tableize}/attributes/credit"
200 def full_credit_template
201 "#{self.classname.tableize}/attributes/full_credit"
206 @credit_data = JSON.parse(self.credit) unless @credit_data
213 return @flags if @flags
215 @flags = JSON.parse(self.settings)
218 @flags = {} unless @flags
227 @flag_open = (flags["open"] || -1) unless @flag_open
232 @flag_commercial = (flags["commercial"] || -1) unless @flag_commercial
237 @flag_official = (flags["official"] || -1) unless @flag_official
242 @flag_attribution = (flags["attribution"] || -1) unless @flag_attribution
247 @flag_derive = (flags["derive"] || -1) unless @flag_derive
252 @flag_thumbnail = (flags["thumbnail"] || -1) unless @flag_thumbnail
257 @flag_gif_convert = (flags["gif_convert"] || -1) unless @flag_gif_convert
262 @flag_reverse = (flags["reverse"] || -1) unless @flag_reverse
267 @flag_resize = (flags["resize"] || -1) unless @flag_resize
272 @flag_sync_vh = (flags["sync_vh"] || -1) unless @flag_sync_vh
277 @flag_overlap = (flags["overlap"] || -1) unless @flag_overlap