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 :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, :classname => rp.classname, :credit => rp.credit, :settings => rp.settings
29 self.attributes = attr
30 self.revision = self.new_revision #Do not move to attr. new_revision reffernces self.original_picture_id
33 def visible? operators
37 def showable? operators = nil
38 return false unless self.original_picture
39 return true if self.own?(operators)
40 self.enable? and self.head?
44 "#{self.id}.#{self.ext}"
56 '/pictures/' + filename
60 {:src => self.url, :width => self.width, :height => self.height}
64 tw, th = PettanImager.thumbnail_size(self.width, self.height)
65 {:src => self.url, :width => tw, :height => th}
68 def tail_opt_img_tag img
69 {:src => img, :width => self.width, :height => self.height}
72 def tail_tmb_opt_img_tag img
73 tw, th = PettanImager.thumbnail_size(self.width, self.height)
74 {:src => img, :width => tw, :height => th}
82 Picture.maximum(:revision, :conditions => ['original_picture_id = ?', self.original_picture_id]).to_i + 1
86 r = self.head.resource_picture
91 Picture.find(:first, :conditions => ['original_picture_id = ?', opid], :order => 'pictures.revision desc')
95 Picture.head(self.original_picture_id)
103 self.ext == 'png' and self.flag_gif_convert >= 0
107 flag_reverse < 0 ? [''] : ['', 'v', 'h', 'vh']
110 def self.find_by_md5 md5
111 r = Picture.find :all, :conditions => ['pictures.md5 = ?', md5], :order => 'pictures.updated_at desc'
114 def self.list_by_md5 md5, opid = nil
115 cond = if opid.blank?
116 ['pictures.md5 = ?', md5]
118 ['pictures.md5 = ? and pictures.original_picture_id <> ?', md5, opid]
120 r = Picture.find :all, :conditions => cond, :order => 'pictures.updated_at desc'
123 def self.exist_by_md5 md5, opid
124 Picture.list_by_md5(md5, opid).empty? ? false : true
127 def self.list_by_original_picture_where original_picture_id
128 ['pictures.original_picture_id = ?', original_picture_id]
131 def self.list_by_original_picture original_picture_id, roles, page = 1, page_size = self.default_page_size
132 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)
136 {:license => {}, :artist => {}}
139 def self.list_json_opt
140 {:include => {:license => {}, :artist => {}} }
146 if res = self.store_picture(imager, self.filename)
148 if gifimager = imager.to_gif
149 res = self.store_picture(gifimager, self.gifname)
159 def store_picture(imager, fn)
162 picdata = d.empty? ? imager.binary : imager.__send__(d)
163 res = PictureIO.picture_io.put(picdata, fn, d)
169 def restore(subdir = nil)
170 PictureIO.picture_io.get self.filename, subdir
173 def self.export(dt = nil)
176 ['artists.author_id is not null and pictures.updated_at >= ?', dt]
178 'artists.author_id is not null'
180 opt.merge!({:conditions => cond})
181 opt.merge!({:include => {:artist => {}}, :order => 'pictures.updated_at desc'})
182 Picture.find(:all, opt)
185 def self.list_as_json_text ary
186 '[' + ary.map {|i| i.to_json_with_picture_data }.join(',') + ']'
190 Base64.encode64(self.restore)
193 def to_json_with_picture_data
194 self.to_json({:methods => :picture_data})
198 imager = PettanImager.load(File.open(Rails.root + 'app/assets/images/error.png', 'rb').read)
199 return false unless imager
204 "#{self.classname.tableize}/attributes/credit"
207 def full_credit_template
208 "#{self.classname.tableize}/attributes/full_credit"
213 @credit_data = JSON.parse(self.credit) unless @credit_data
220 return @flags if @flags
222 @flags = JSON.parse(self.settings)
225 @flags = {} unless @flags
234 @flag_open = (flags["open"] || -1) unless @flag_open
239 @flag_commercial = (flags["commercial"] || -1) unless @flag_commercial
244 @flag_official = (flags["official"] || -1) unless @flag_official
249 @flag_attribution = (flags["attribution"] || -1) unless @flag_attribution
254 @flag_derive = (flags["derive"] || -1) unless @flag_derive
259 @flag_thumbnail = (flags["thumbnail"] || -1) unless @flag_thumbnail
264 @flag_gif_convert = (flags["gif_convert"] || -1) unless @flag_gif_convert
269 @flag_reverse = (flags["reverse"] || -1) unless @flag_reverse
274 @flag_resize = (flags["resize"] || -1) unless @flag_resize
279 @flag_sync_vh = (flags["sync_vh"] || -1) unless @flag_sync_vh
284 @flag_overlap = (flags["overlap"] || -1) unless @flag_overlap