2 class ResourcePicture < Peta::Content
5 belongs_to :license_group
7 belongs_to :system_picture
9 belongs_to :original_picture
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 :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
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 :original_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
20 validates :license_group_module_name, :presence => true, :length => {:maximum => 50}
21 validates :picture_id, :presence => true, :numericality => true, :existence => {:both => false}
22 validates :license_settings, :boost => {:boost_name => :license}
23 validates :license_group_settings, :boost => {:boost_name => :license_group}
24 validates :credit_picture_settings, :boost => {:boost_name => :credit_picture}
26 def self.pickup_item_name
30 def self.pickup_column_name
31 self.pickup_item_name + '_id'
36 self.attributes[self.pickup_column_name]
43 attr = {:width => op.width, :height => op.height, :ext => op.ext, :filesize => op.filesize,
44 :original_picture_id => op.id, :artist_id => op.artist_id, :md5 => op.md5,
45 :created_at => Time.now, :updated_at => Time.now
47 self.attributes = attr
50 def visible? operators
52 # content model call to owner checker
53 self.user_visible? operators
57 "#{self.id}.#{self.ext}"
69 '/resource_pictures/' + filename + (subdir.to_s.empty? ? '' : '?subdir=' + subdir.to_s)
73 self.ext == 'png' and self.license_extend.gif_convert >= 0
77 tw, th = ResourcePicture.fix_size_both(Manifest.manifest.magic_numbers['thumbnail_width'], Manifest.manifest.magic_numbers['thumbnail_height'], rimg.columns, rimg.rows)
78 ResourcePicture.resize(rimg.to_blob, tw, th).to_blob
82 tw, th = PettanImager.thumbnail_size(self.width, self.height)
83 {:src => self.url, :width => tw, :height => th}
87 {:src => self.url('full'), :width => self.width, :height => self.height}
91 self.license.license_group.caption.to_s + '[' + self.license.caption.to_s + ']'
99 'resource_pictures.updated_at desc'
107 {:license => {}, :artist => {}, :picture => {} }
110 def self.list_json_opt
111 {:include => {:license => {}, :artist => {}, :picture => {}} }
115 {:include => {:license => {}, :artist => {}, :picture => {}} }
118 def self.show_json_opt
119 {:include => {:license => {}, :artist => {}, :picture => {}} }
122 def new_picture imager
129 self.errors.add :base, Picture.model_name.human + I18n.t('errors.not_create')
134 return false unless imager
136 self.overwrite self.original_picture
137 ResourcePicture.transaction do
138 self.original_picture.published_at = Time.now
139 self.original_picture.stopped_at = nil
140 raise ActiveRecord::Rollback unless self.original_picture.save
141 pc = self.new_picture imager
143 self.picture_id = pc.id
145 res = self.store_picture_with_gif(imager)
149 raise ActiveRecord::Rollback unless res
154 def store_picture_with_gif(imager)
155 if res = self.store_picture(imager, self.filename)
157 if gifimager = imager.to_gif
158 if res = self.store_picture(gifimager, self.gifname)
160 self.errors.add :base, I18n.t('picture_io.error')
163 self.errors.add :base, I18n.t('errors.not_convert')
168 self.errors.add :base, I18n.t('picture_io.error')
173 def store_picture(imager, fn)
175 thumbnail_imager = self.license_extend.thumbnail >= 0 ? imager.to_thumbnail : imager
176 return false unless thumbnail_imager
178 PictureIO.resource_picture_io.put(thumbnail_imager.binary, fn)
179 PictureIO.resource_picture_io.put(imager.binary, fn, 'full')
181 rescue PictureIO::Error
187 def restore(subdir = nil)
188 PictureIO.resource_picture_io.get self.filename, subdir
193 ResourcePicture.transaction do
194 self.original_picture.published_at = nil
195 self.original_picture.stopped_at = Time.now
196 raise ActiveRecord::Rollback unless self.original_picture.save
198 PictureIO.resource_picture_io.delete(self.filename) if PictureIO.resource_picture_io.exist?(self.filename)
199 PictureIO.resource_picture_io.delete(self.filename, 'full') if PictureIO.resource_picture_io.exist?(self.filename, 'full')
200 rescue PictureIO::Error
202 raise ActiveRecord::Rollback
205 raise ActiveRecord::Rollback unless res
210 def self.visible_count
211 ResourcePicture.count
215 Base64.encode64(self.restore 'full')
219 "#{self.license_group_module_name.tableize}/attributes/credit"
222 def full_credit_template
223 "#{self.license_group_module_name.tableize}/attributes/full_credit"