2 class ResourcePicture < Peta::Content
6 belongs_to :system_picture
8 belongs_to :original_picture
10 validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
11 validates :width, :presence => true, :numericality => true, :natural_number => true
12 validates :height, :presence => true, :numericality => true, :natural_number => true
13 validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
14 validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
15 validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
16 validates :license_id, :presence => true, :numericality => true, :existence => {:both => false}
17 validates :system_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
18 validates :original_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
19 validates :license_group_module_name, :presence => true, :length => {:maximum => 50}
20 validates :picture_id, :presence => true, :numericality => true, :existence => {:both => false}
21 validates :license_settings, :boost => {:boost_name => :license}
22 validates :license_group_settings, :boost => {:boost_name => :license_group}
23 validates :credit_picture_settings, :boost => {:boost_name => :credit_picture}
29 attr = {:width => op.width, :height => op.height, :ext => op.ext, :filesize => op.filesize,
30 :original_picture_id => op.id, :artist_id => op.artist_id, :md5 => op.md5,
31 :created_at => Time.now, :updated_at => Time.now
33 self.attributes = attr
36 def visible? operators
38 # content model call to owner checker
39 self.user_visible? operators
43 "#{self.id}.#{self.ext}"
55 '/resource_pictures/' + filename + (subdir.to_s.empty? ? '' : '?subdir=' + subdir.to_s)
59 self.ext == 'png' and self.license_extend.gif_convert >= 0
63 tw, th = ResourcePicture.fix_size_both(Manifest.manifest.magic_numbers['thumbnail_width'], Manifest.manifest.magic_numbers['thumbnail_height'], rimg.columns, rimg.rows)
64 ResourcePicture.resize(rimg.to_blob, tw, th).to_blob
68 tw, th = PettanImager.thumbnail_size(self.width, self.height)
69 {:src => self.url, :width => tw, :height => th}
73 {:src => self.url('full'), :width => self.width, :height => self.height}
77 self.license.license_group.caption.to_s + '[' + self.license.caption.to_s + ']'
85 'resource_pictures.updated_at desc'
93 {:license => {}, :artist => {}, :picture => {} }
96 def self.list_json_opt
97 {:include => {:license => {}, :artist => {}, :picture => {}} }
101 {:include => {:license => {}, :artist => {}, :picture => {}} }
104 def self.show_json_opt
105 {:include => {:license => {}, :artist => {}, :picture => {}} }
108 def new_picture imager
115 self.errors.add :base, Picture.model_name.human + I18n.t('errors.not_create')
120 return false unless imager
122 self.overwrite self.original_picture
123 ResourcePicture.transaction do
124 self.original_picture.published_at = Time.now
125 self.original_picture.stopped_at = nil
126 raise ActiveRecord::Rollback unless self.original_picture.save
127 pc = self.new_picture imager
129 self.picture_id = pc.id
131 res = self.store_picture_with_gif(imager)
135 raise ActiveRecord::Rollback unless res
140 def store_picture_with_gif(imager)
141 if res = self.store_picture(imager, self.filename)
143 if gifimager = imager.to_gif
144 if res = self.store_picture(gifimager, self.gifname)
146 self.errors.add :base, I18n.t('picture_io.error')
149 self.errors.add :base, I18n.t('errors.not_convert')
154 self.errors.add :base, I18n.t('picture_io.error')
159 def store_picture(imager, fn)
161 thumbnail_imager = self.license_extend.thumbnail >= 0 ? imager.to_thumbnail : imager
162 return false unless thumbnail_imager
164 PictureIO.resource_picture_io.put(thumbnail_imager.binary, fn)
165 PictureIO.resource_picture_io.put(imager.binary, fn, 'full')
167 rescue PictureIO::Error
173 def restore(subdir = nil)
174 PictureIO.resource_picture_io.get self.filename, subdir
179 ResourcePicture.transaction do
180 self.original_picture.published_at = nil
181 self.original_picture.stopped_at = Time.now
182 raise ActiveRecord::Rollback unless self.original_picture.save
184 PictureIO.resource_picture_io.delete(self.filename) if PictureIO.resource_picture_io.exist?(self.filename)
185 PictureIO.resource_picture_io.delete(self.filename, 'full') if PictureIO.resource_picture_io.exist?(self.filename, 'full')
186 rescue PictureIO::Error
188 raise ActiveRecord::Rollback
191 raise ActiveRecord::Rollback unless res
196 def self.visible_count
197 ResourcePicture.count
201 Base64.encode64(self.restore 'full')
205 "#{self.license_group_module_name.tableize}/attributes/credit"
208 def full_credit_template
209 "#{self.license_group_module_name.tableize}/attributes/full_credit"