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 :artist_name, :presence => true
20 validates :license_group_module_name, :presence => true, :length => {:maximum => 50}
21 validates :picture_id, :presence => true, :numericality => true, :existence => {:both => false}
27 attr = {:width => op.width, :height => op.height, :ext => op.ext, :filesize => op.filesize,
28 :original_picture_id => op.id, :artist_id => op.artist_id, :md5 => op.md5
30 self.attributes = attr
33 def visible? operators
35 # content model call to owner checker
36 self.user_visible? operators
40 "#{self.id}.#{self.ext}"
52 '/resource_pictures/' + filename + (subdir.to_s.empty? ? '' : '?subdir=' + subdir.to_s)
56 self.ext == 'png' and self.license_extend.gif_convert >= 0
60 tw, th = ResourcePicture.fix_size_both(Manifest.manifest.magic_numbers['thumbnail_width'], Manifest.manifest.magic_numbers['thumbnail_height'], rimg.columns, rimg.rows)
61 ResourcePicture.resize(rimg.to_blob, tw, th).to_blob
65 tw, th = PettanImager.thumbnail_size(self.width, self.height)
66 {:src => self.url, :width => tw, :height => th}
70 {:src => self.url('full'), :width => self.width, :height => self.height}
74 self.license.license_group.caption.to_s + '[' + self.license.caption.to_s + ']'
82 'resource_pictures.updated_at desc'
90 {:license => {}, :artist => {}, :picture => {} }
93 def self.list_json_opt
94 {:include => {:license => {}, :artist => {}, :picture => {}} }
98 {:include => {:license => {}, :artist => {}, :picture => {}} }
101 def self.show_json_opt
102 {:include => {:license => {}, :artist => {}, :picture => {}} }
105 def new_picture imager
112 self.errors.add :base, Picture.model_name.human + I18n.t('errors.not_create')
117 return false unless imager
119 self.overwrite self.original_picture
120 ResourcePicture.transaction do
121 self.original_picture.published_at = Time.now
122 self.original_picture.stopped_at = nil
123 raise ActiveRecord::Rollback unless self.original_picture.save
124 pc = self.new_picture imager
126 self.picture_id = pc.id
128 res = self.store_picture_with_gif(imager)
132 raise ActiveRecord::Rollback unless res
137 def store_picture_with_gif(imager)
138 if res = self.store_picture(imager, self.filename)
140 if gifimager = imager.to_gif
141 if res = self.store_picture(gifimager, self.gifname)
143 self.errors.add :base, I18n.t('picture_io.error')
146 self.errors.add :base, I18n.t('errors.not_convert')
151 self.errors.add :base, I18n.t('picture_io.error')
156 def store_picture(imager, fn)
158 thumbnail_imager = self.license_extend.thumbnail >= 0 ? imager.to_thumbnail : imager
159 return false unless thumbnail_imager
161 PictureIO.resource_picture_io.put(thumbnail_imager.binary, fn)
162 PictureIO.resource_picture_io.put(imager.binary, fn, 'full')
164 rescue PictureIO::Error
170 def restore(subdir = nil)
171 PictureIO.resource_picture_io.get self.filename, subdir
176 ResourcePicture.transaction do
177 self.original_picture.published_at = nil
178 self.original_picture.stopped_at = Time.now
179 raise ActiveRecord::Rollback unless self.original_picture.save
181 PictureIO.resource_picture_io.delete(self.filename) if PictureIO.resource_picture_io.exist?(self.filename)
182 PictureIO.resource_picture_io.delete(self.filename, 'full') if PictureIO.resource_picture_io.exist?(self.filename, 'full')
183 rescue PictureIO::Error
185 raise ActiveRecord::Rollback
188 raise ActiveRecord::Rollback unless res
193 def self.visible_count
194 ResourcePicture.count
198 Base64.encode64(self.restore 'full')
202 "#{self.license_group_module_name.tableize}/attributes/credit"
205 def full_credit_template
206 "#{self.license_group_module_name.tableize}/attributes/full_credit"