2 class Picture < Peta::Content
4 belongs_to :original_picture
7 has_one :resource_picture
9 validates :original_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
10 validates :revision, :presence => true, :numericality => true
11 validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
12 validates :width, :presence => true, :numericality => true, :natural_number => true
13 validates :height, :presence => true, :numericality => true, :natural_number => true
14 validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
15 validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
16 validates :license_id, :presence => true, :numericality => true, :existence => {:both => false}
17 validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
18 validates :artist_name, :presence => true
19 validates :license_group_classname, :presence => true, :length => {:maximum => 50}
25 attr = {:width => rp.width, :height => rp.height, :ext => rp.ext, :filesize => rp.filesize,
26 :original_picture_id => rp.original_picture_id, :license_id => rp.license_id, :artist_id => rp.artist_id,
27 :md5 => rp.md5, :artist_name => rp.artist_name,
28 :license_group_classname => rp.license_group_classname,
29 :license_group_settings => rp.license_group_settings,
30 :credit_picture_settings => rp.credit_picture_settings,
31 :license_settings => rp.license_settings
33 self.attributes = attr
34 self.revision = self.new_revision #Do not move to attr. new_revision reffernces self.original_picture_id
37 def visible? operators
41 def showable? operators = nil
42 return false unless self.original_picture
43 return true if self.own?(operators)
44 self.enable? and self.head?
48 "#{self.id}.#{self.ext}"
60 '/pictures/' + filename
64 {:src => self.url, :width => self.width, :height => self.height}
68 tw, th = PettanImager.thumbnail_size(self.width, self.height)
69 {:src => self.url, :width => tw, :height => th}
72 def tail_opt_img_tag img
73 {:src => img, :width => self.width, :height => self.height}
76 def tail_tmb_opt_img_tag img
77 tw, th = PettanImager.thumbnail_size(self.width, self.height)
78 {:src => img, :width => tw, :height => th}
86 Picture.maximum(:revision, :conditions => ['original_picture_id = ?', self.original_picture_id]).to_i + 1
90 r = self.head.resource_picture
95 Picture.find(:first, :conditions => ['original_picture_id = ?', opid], :order => 'pictures.revision desc')
99 Picture.head(self.original_picture_id)
107 self.ext == 'png' and self.license_extend.gif_convert >= 0
111 self.license_extend.reverse < 0 ? [''] : ['', 'v', 'h', 'vh']
114 def self.find_by_md5 md5
115 r = Picture.find :all, :conditions => ['pictures.md5 = ?', md5], :order => 'pictures.updated_at desc'
118 def self.list_by_md5 md5, opid = nil
119 cond = if opid.blank?
120 ['pictures.md5 = ?', md5]
122 ['pictures.md5 = ? and pictures.original_picture_id <> ?', md5, opid]
124 r = Picture.find :all, :conditions => cond, :order => 'pictures.updated_at desc'
127 def self.exist_by_md5 md5, opid
128 Picture.list_by_md5(md5, opid).empty? ? false : true
131 def self.list_by_original_picture_where original_picture_id
132 ['pictures.original_picture_id = ?', original_picture_id]
135 def self.list_by_original_picture original_picture_id, roles, page = 1, page_size = self.default_page_size
136 self.where(self.list_by_original_picture_where(original_picture_id)).includes(self.list_opt).order('pictures.revision desc').offset((page -1) * page_size).limit(page_size)
140 {:license => {}, :artist => {}}
143 def self.list_json_opt
144 {:include => {:license => {}, :artist => {}} }
150 if res = self.store_picture(imager, self.filename)
152 if gifimager = imager.to_gif
153 res = self.store_picture(gifimager, self.gifname)
163 def store_picture(imager, fn)
166 picdata = d.empty? ? imager.binary : imager.__send__(d)
167 res = PictureIO.picture_io.put(picdata, fn, d)
173 def restore(subdir = nil)
174 PictureIO.picture_io.get self.filename, subdir
177 def self.export(dt = nil)
180 ['artists.author_id is not null and pictures.updated_at >= ?', dt]
182 'artists.author_id is not null'
184 opt.merge!({:conditions => cond})
185 opt.merge!({:include => {:artist => {}}, :order => 'pictures.updated_at desc'})
186 Picture.find(:all, opt)
189 def self.list_as_json_text ary
190 '[' + ary.map {|i| i.to_json_with_picture_data }.join(',') + ']'
194 Base64.encode64(self.restore)
197 def to_json_with_picture_data
198 self.to_json({:methods => :picture_data})
202 imager = PettanImager.load(File.open(Rails.root + 'app/assets/images/error.png', 'rb').read)
203 return false unless imager
208 "#{self.license_group_classname.tableize}/attributes/credit"
211 def full_credit_template
212 "#{self.license_group_classname.tableize}/attributes/full_credit"