2 class ResourcePicture < Peta::Content
7 belongs_to :original_picture
9 validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
10 validates :width, :presence => true, :numericality => true, :natural_number => true
11 validates :height, :presence => true, :numericality => true, :natural_number => true
12 validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
13 validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
14 validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
15 validates :license_id, :presence => true, :numericality => true, :existence => {:both => false}
16 validates :original_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
17 validates :artist_name, :presence => true
18 validates :classname, :presence => true, :length => {:maximum => 50}
19 validates :picture_id, :presence => true, :numericality => true, :existence => {:both => false}
25 attr = {:width => op.width, :height => op.height, :ext => op.ext, :filesize => op.filesize,
26 :original_picture_id => op.id, :artist_id => op.artist_id, :md5 => op.md5
28 self.attributes = attr
31 def visible? operators
32 return false unless super
37 "#{self.id}.#{self.ext}"
49 '/resource_pictures/' + (subdir.to_s.empty? ? '' : subdir.to_s + '/' ) + filename
53 self.ext == 'png' and self.flag_gif_convert >= 0
57 tw, th = ResourcePicture.fix_size_both(Manifest.manifest.magic_numbers['thumbnail_width'], Manifest.manifest.magic_numbers['thumbnail_height'], rimg.columns, rimg.rows)
58 ResourcePicture.resize(rimg.to_blob, tw, th).to_blob
62 tw, th = PettanImager.thumbnail_size(self.width, self.height)
63 {:src => self.url, :width => tw, :height => th}
67 {:src => self.url('full'), :width => self.width, :height => self.height}
75 'resource_pictures.updated_at desc'
83 {:license => {}, :artist => {}, :picture => {} }
86 def self.list_json_opt
87 {:include => {:license => {}, :artist => {}, :picture => {}} }
91 {:include => {:license => {}, :artist => {}, :picture => {}} }
94 def self.show_json_opt
95 {:include => {:license => {}, :artist => {}, :picture => {}} }
98 def new_picture imager
104 self.errors.add :base, Picture.model_name.human + I18n.t('errors.not_create')
109 return false unless imager
111 self.overwrite self.original_picture
112 ResourcePicture.transaction do
113 self.original_picture.published_at = Time.now
114 self.original_picture.stopped_at = nil
115 raise ActiveRecord::Rollback unless self.original_picture.save
116 pc = self.new_picture imager
118 self.picture_id = pc.id
120 res = self.store_picture_with_gif(imager)
124 raise ActiveRecord::Rollback unless res
129 def store_picture_with_gif(imager)
130 if res = self.store_picture(imager, self.filename)
132 if gifimager = imager.to_gif
133 if res = self.store_picture(gifimager, self.gifname)
135 self.errors.add :base, I18n.t('picture_io.error')
138 self.errors.add :base, I18n.t('errors.not_convert')
143 self.errors.add :base, I18n.t('picture_io.error')
148 def store_picture(imager, fn)
150 thumbnail_imager = self.flag_thumbnail >= 0 ? imager.to_thumbnail : imager
151 return false unless thumbnail_imager
153 PictureIO.resource_picture_io.put(thumbnail_imager.binary, fn)
154 PictureIO.resource_picture_io.put(imager.binary, fn, 'full')
156 rescue PictureIO::Error
162 def restore(subdir = nil)
163 PictureIO.resource_picture_io.get self.filename, subdir
168 ResourcePicture.transaction do
169 self.original_picture.published_at = nil
170 self.original_picture.stopped_at = Time.now
171 raise ActiveRecord::Rollback unless self.original_picture.save
173 PictureIO.resource_picture_io.delete(self.filename) if PictureIO.resource_picture_io.exist?(self.filename)
174 PictureIO.resource_picture_io.delete(self.filename, 'full') if PictureIO.resource_picture_io.exist?(self.filename, 'full')
175 rescue PictureIO::Error
177 raise ActiveRecord::Rollback
180 raise ActiveRecord::Rollback unless res
185 def self.visible_count
186 ResourcePicture.count
190 Base64.encode64(self.restore 'full')
194 "#{self.classname.tableize}/attributes/credit"
197 def full_credit_template
198 "#{self.classname.tableize}/attributes/full_credit"
203 @credit_data = JSON.parse(self.credit) unless @credit_data
211 @flags = JSON.parse(self.settings) unless @flags
222 @flag_open = flags["open"] unless @flag_open
227 @flag_commercial = flags["commercial"] unless @flag_commercial
232 @flag_official = flags["official"] unless @flag_official
237 @flag_attribution = flags["attribution"] unless @flag_attribution
242 @flag_derive = flags["derive"] unless @flag_derive
247 @flag_thumbnail = flags["thumbnail"] unless @flag_thumbnail
252 @flag_gif_convert = flags["gif_convert"] unless @flag_gif_convert
257 @flag_reverse = flags["reverse"] unless @flag_reverse
262 @flag_resize = flags["resize"] unless @flag_resize
267 @flag_sync_vh = flags["sync_vh"] unless @flag_sync_vh
272 @flag_overlap = flags["overlap"] unless @flag_overlap