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 :artist_name, :presence => true
21 validates :license_group_classname, :presence => true, :length => {:maximum => 50}
27 attr = {:width => rp.width, :height => rp.height, :ext => rp.ext, :filesize => rp.filesize,
28 :original_picture_id => rp.original_picture_id, :license_id => rp.license_id,
29 :system_picture_id => rp.system_picture_id, :artist_id => rp.artist_id,
30 :md5 => rp.md5, :artist_name => rp.artist_name,
31 :license_group_classname => rp.license_group_classname,
32 :license_group_settings => rp.license_group_settings,
33 :credit_picture_settings => rp.credit_picture_settings,
34 :license_settings => rp.license_settings
36 self.attributes = attr
37 self.revision = self.new_revision #Do not move to attr. new_revision reffernces self.original_picture_id
40 def visible? operators
44 def showable? operators = nil
45 return false unless self.original_picture
46 return true if self.own?(operators)
47 self.enable? and self.head?
51 "#{self.id}.#{self.ext}"
63 '/pictures/' + filename
67 {:src => self.url, :width => self.width, :height => self.height}
71 tw, th = PettanImager.thumbnail_size(self.width, self.height)
72 {:src => self.url, :width => tw, :height => th}
75 def tail_opt_img_tag img
76 {:src => img, :width => self.width, :height => self.height}
79 def tail_tmb_opt_img_tag img
80 tw, th = PettanImager.thumbnail_size(self.width, self.height)
81 {:src => img, :width => tw, :height => th}
85 self.license.license_group.caption.to_s + '[' + self.license.caption.to_s + ']'
93 Picture.maximum(:revision, :conditions => ['original_picture_id = ?', self.original_picture_id]).to_i + 1
97 r = self.head.resource_picture
102 Picture.find(:first, :conditions => ['original_picture_id = ?', opid], :order => 'pictures.revision desc')
106 Picture.head(self.original_picture_id)
114 self.ext == 'png' and self.license_extend.gif_convert >= 0
118 self.license_extend.reverse < 0 ? [''] : ['', 'v', 'h', 'vh']
121 def self.find_by_md5 md5
122 r = Picture.find :all, :conditions => ['pictures.md5 = ?', md5], :order => 'pictures.updated_at desc'
125 def self.list_by_md5 md5, opid = nil
126 cond = if opid.blank?
127 ['pictures.md5 = ?', md5]
129 ['pictures.md5 = ? and pictures.original_picture_id <> ?', md5, opid]
131 r = Picture.find :all, :conditions => cond, :order => 'pictures.updated_at desc'
134 def self.exist_by_md5 md5, opid
135 Picture.list_by_md5(md5, opid).empty? ? false : true
138 def self.list_by_original_picture_where original_picture_id
139 ['pictures.original_picture_id = ?', original_picture_id]
142 def self.list_by_original_picture original_picture_id, roles, page = 1, page_size = self.default_page_size
143 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)
147 {:license => {}, :artist => {}}
150 def self.list_json_opt
151 {:include => {:license => {}, :artist => {}} }
157 if res = self.store_picture(imager, self.filename)
159 if gifimager = imager.to_gif
160 res = self.store_picture(gifimager, self.gifname)
170 def store_picture(imager, fn)
173 picdata = d.empty? ? imager.binary : imager.__send__(d)
174 res = PictureIO.picture_io.put(picdata, fn, d)
180 def restore(subdir = nil)
181 PictureIO.picture_io.get self.filename, subdir
184 def self.export(dt = nil)
187 ['artists.author_id is not null and pictures.updated_at >= ?', dt]
189 'artists.author_id is not null'
191 opt.merge!({:conditions => cond})
192 opt.merge!({:include => {:artist => {}}, :order => 'pictures.updated_at desc'})
193 Picture.find(:all, opt)
196 def self.list_as_json_text ary
197 '[' + ary.map {|i| i.to_json_with_picture_data }.join(',') + ']'
201 Base64.encode64(self.restore)
204 def to_json_with_picture_data
205 self.to_json({:methods => :picture_data})
209 imager = PettanImager.load(File.open(Rails.root + 'app/assets/images/error.png', 'rb').read)
210 return false unless imager
215 "#{self.license_group_classname.tableize}/attributes/credit"
218 def full_credit_template
219 "#{self.license_group_classname.tableize}/attributes/full_credit"