2 class Picture < Peta::Content
4 belongs_to :original_picture
6 belongs_to :system_picture
8 has_one :resource_picture
9 has_one :resource_picture_picture
11 validates :original_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
12 validates :revision, :presence => true, :numericality => true
13 validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
14 validates :width, :presence => true, :numericality => true, :natural_number => true
15 validates :height, :presence => true, :numericality => true, :natural_number => true
16 validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
17 validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
18 validates :license_id, :presence => true, :numericality => true, :existence => {:both => false}
19 validates :system_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
20 validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
21 validates :license_group_module_name, :presence => true, :length => {:maximum => 50}
23 scope :find_index, -> do
27 scope :find_by_artist, -> (artist_id) do
28 find_index.where(artist_id: artist_id)
35 attr = {:width => rp.width, :height => rp.height, :ext => rp.ext, :filesize => rp.filesize,
36 :original_picture_id => rp.original_picture_id, :license_id => rp.license_id,
37 :system_picture_id => rp.system_picture_id, :artist_id => rp.artist_id,
39 :license_group_module_name => rp.license_group_module_name,
40 :license_group_settings => rp.license_group_settings,
41 :credit_picture_settings => rp.credit_picture_settings,
42 :license_settings => rp.license_settings
44 self.attributes = attr
45 self.revision = self.new_revision #Do not move to attr. new_revision reffernces self.original_picture_id
48 def visible? operators
52 def showable? operators = nil
53 return false unless self.original_picture
54 return true if self.own?(operators)
55 self.enable? and self.head?
59 "#{self.id}.#{self.ext}"
71 '/pictures/' + filename
75 {:src => self.url, :width => self.width, :height => self.height}
79 tw, th = PettanImager.thumbnail_size(self.width, self.height)
80 {:src => self.url, :width => tw, :height => th}
83 def tail_opt_img_tag img
84 {:src => img, :width => self.width, :height => self.height}
87 def tail_tmb_opt_img_tag img
88 tw, th = PettanImager.thumbnail_size(self.width, self.height)
89 {:src => img, :width => tw, :height => th}
93 self.license.license_group.caption.to_s + '[' + self.license.caption.to_s + ']'
101 Picture.where(original_picture_id: self.original_picture_id).maximum(:revision).to_i + 1
105 r = self.head.resource_picture
110 Picture.where(original_picture_id: opid).order('pictures.revision desc').first
114 Picture.head(self.original_picture_id)
122 self.ext == 'png' and self.license_extend.gif_convert >= 0
126 self.license_extend.reverse < 0 ? [''] : ['', 'v', 'h', 'vh']
129 def self.find_by_md5 md5
130 r = Picture.where(md5: md5).order(updated_at: :desc)
133 def self.list_by_md5 md5, opid = nil
134 cond = if opid.blank?
137 ['md5 = :md5 and original_picture_id <> :opid', {md5: md5, opid: opid}]
139 r = Picture.where(cond).order(updated_at: :desc)
142 def self.exist_by_md5 md5, opid
143 Picture.list_by_md5(md5, opid).empty? ? false : true
146 def self.list_by_original_picture_where original_picture_id
147 ['pictures.original_picture_id = ?', original_picture_id]
150 def self.list_by_original_picture original_picture_id, roles, page = 1, page_size = self.default_page_size
151 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)
155 {:license => {}, :artist => {}}
158 def self.list_json_opt
159 {:include => {:license => {}, :artist => {}} }
165 if res = self.store_picture(imager, self.filename)
167 if gifimager = imager.to_gif
168 res = self.store_picture(gifimager, self.gifname)
178 def store_picture(imager, fn)
181 picdata = d.empty? ? imager.binary : imager.__send__(d)
182 res = PictureIO.picture_io.put(picdata, fn, d)
188 def restore(subdir = nil)
189 PictureIO.picture_io.get self.filename, subdir
192 def self.export(dt = nil)
193 pictures = Picture.includes(:artist).where('artists.author_id is not null')
194 pictures = pictures.where(['pictures.updated_at >= ?', dt]) if dt
195 pictures.order('pictures.updated_at desc').references(:artist)
198 def self.list_as_json_text ary
199 '[' + ary.map {|i| i.to_json_with_picture_data }.join(',') + ']'
203 Base64.encode64(self.restore)
206 def to_json_with_picture_data
207 self.to_json({:methods => :picture_data})
212 imager = PettanImager.load(File.open(Rails.root + 'app/assets/images/error.png', 'rb').read)
213 return false unless imager
218 "#{self.license_group_module_name.tableize}/attributes/credit"
221 def full_credit_template
222 "#{self.license_group_module_name.tableize}/attributes/full_credit"