2 class Picture < Peta::Content
4 belongs_to :original_picture
6 belongs_to :system_picture
8 has_one :resource_picture
10 validates :original_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
11 validates :revision, :presence => true, :numericality => true
12 validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
13 validates :width, :presence => true, :numericality => true, :natural_number => true
14 validates :height, :presence => true, :numericality => true, :natural_number => true
15 validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
16 validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
17 validates :license_id, :presence => true, :numericality => true, :existence => {:both => false}
18 validates :system_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
19 validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
20 validates :license_group_module_name, :presence => true, :length => {:maximum => 50}
26 attr = {:width => rp.width, :height => rp.height, :ext => rp.ext, :filesize => rp.filesize,
27 :original_picture_id => rp.original_picture_id, :license_id => rp.license_id,
28 :system_picture_id => rp.system_picture_id, :artist_id => rp.artist_id,
30 :license_group_module_name => rp.license_group_module_name,
31 :license_group_settings => rp.license_group_settings,
32 :credit_picture_settings => rp.credit_picture_settings,
33 :license_settings => rp.license_settings
35 self.attributes = attr
36 self.revision = self.new_revision #Do not move to attr. new_revision reffernces self.original_picture_id
39 def visible? operators
43 def showable? operators = nil
44 return false unless self.original_picture
45 return true if self.own?(operators)
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 self.license.license_group.caption.to_s + '[' + self.license.caption.to_s + ']'
92 Picture.maximum(:revision, :conditions => ['original_picture_id = ?', self.original_picture_id]).to_i + 1
96 r = self.head.resource_picture
101 Picture.find(:first, :conditions => ['original_picture_id = ?', opid], :order => 'pictures.revision desc')
105 Picture.head(self.original_picture_id)
113 self.ext == 'png' and self.license_extend.gif_convert >= 0
117 self.license_extend.reverse < 0 ? [''] : ['', 'v', 'h', 'vh']
120 def self.find_by_md5 md5
121 r = Picture.find :all, :conditions => ['pictures.md5 = ?', md5], :order => 'pictures.updated_at desc'
124 def self.list_by_md5 md5, opid = nil
125 cond = if opid.blank?
126 ['pictures.md5 = ?', md5]
128 ['pictures.md5 = ? and pictures.original_picture_id <> ?', md5, opid]
130 r = Picture.find :all, :conditions => cond, :order => 'pictures.updated_at desc'
133 def self.exist_by_md5 md5, opid
134 Picture.list_by_md5(md5, opid).empty? ? false : true
137 def self.list_by_original_picture_where original_picture_id
138 ['pictures.original_picture_id = ?', original_picture_id]
141 def self.list_by_original_picture original_picture_id, roles, page = 1, page_size = self.default_page_size
142 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)
146 {:license => {}, :artist => {}}
149 def self.list_json_opt
150 {:include => {:license => {}, :artist => {}} }
156 if res = self.store_picture(imager, self.filename)
158 if gifimager = imager.to_gif
159 res = self.store_picture(gifimager, self.gifname)
169 def store_picture(imager, fn)
172 picdata = d.empty? ? imager.binary : imager.__send__(d)
173 res = PictureIO.picture_io.put(picdata, fn, d)
179 def restore(subdir = nil)
180 PictureIO.picture_io.get self.filename, subdir
183 def self.export(dt = nil)
186 ['artists.author_id is not null and pictures.updated_at >= ?', dt]
188 'artists.author_id is not null'
190 opt.merge!({:conditions => cond})
191 opt.merge!({:include => {:artist => {}}, :order => 'pictures.updated_at desc'})
192 Picture.find(:all, opt)
195 def self.list_as_json_text ary
196 '[' + ary.map {|i| i.to_json_with_picture_data }.join(',') + ']'
200 Base64.encode64(self.restore)
203 def to_json_with_picture_data
204 self.to_json({:methods => :picture_data})
208 imager = PettanImager.load(File.open(Rails.root + 'app/assets/images/error.png', 'rb').read)
209 return false unless imager
214 "#{self.license_group_module_name.tableize}/attributes/credit"
217 def full_credit_template
218 "#{self.license_group_module_name.tableize}/attributes/full_credit"