2 class ResourcePicture < ActiveRecord::Base
6 has_many :panel_pictures
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
32 return false unless ar.is_a?(Artist)
33 ar.id == self.artist_id
38 return false if MagicNumber['run_mode'] == 1
39 elsif au.is_a?(Author)
48 "#{self.id}.#{self.ext}"
60 '/resource_pictures/' + (subdir.to_s.empty? ? '' : subdir.to_s + '/' ) + filename
64 self.ext == 'png' and self.flag_gif_convert >= 0
68 tw, th = ResourcePicture.fix_size_both(MagicNumber['thumbnail_width'], MagicNumber['thumbnail_height'], rimg.columns, rimg.rows)
69 ResourcePicture.resize(rimg.to_blob, tw, th).to_blob
73 tw, th = PettanImager.thumbnail_size(self.width, self.height)
74 {:src => self.url, :width => tw, :height => th}
78 {:src => self.url('full'), :width => self.width, :height => self.height}
81 def self.default_page_size
85 def self.max_page_size
89 def self.page prm = nil
95 def self.page_size prm = self.default_page_size
97 page_size = self.max_page_size if page_size > self.max_page_size
98 page_size = self.default_page_size if page_size < 1
102 def self.list page = 1, page_size = self.default_page_size
104 opt.merge!(self.list_opt)
105 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
106 opt.merge!({:order => 'updated_at desc'})
107 ResourcePicture.find(:all, opt)
111 {:include => {:license => {}, :artist => {}, :picture => {}} }
114 def self.list_json_opt
115 {:include => {:license => {}, :artist => {}, :picture => {}} }
118 def self.mylist ar, page = 1, page_size = Author.default_resource_picture_page_size
120 opt.merge!(ResourcePicture.list_opt)
121 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
122 opt.merge!({:conditions => ['resource_pictures.artist_id = ?', ar.id], :order => 'resource_pictures.updated_at desc'})
123 ResourcePicture.find(:all, opt)
126 def self.show rid, au
128 opt.merge!(self.show_opt)
129 r = ResourcePicture.find(rid, opt)
130 raise ActiveRecord::Forbidden unless r.visible?(au)
135 {:include => {:license => {}, :artist => {}, :picture => {}} }
138 def self.show_json_opt
139 {:include => {:license => {}, :artist => {}, :picture => {}} }
142 def self.edit rid, ar
144 opt.merge!(self.show_opt)
145 r = ResourcePicture.find(rid, opt)
146 raise ActiveRecord::Forbidden unless r.own?(ar)
150 def new_picture imager
156 self.errors.add :base, Picture.model_name.human + I18n.t('errors.not_create')
161 return false unless imager
163 self.overwrite self.original_picture
164 ResourcePicture.transaction do
165 self.original_picture.published_at = Time.now
166 self.original_picture.stopped_at = nil
167 raise ActiveRecord::Rollback unless self.original_picture.save
168 pc = self.new_picture imager
170 self.picture_id = pc.id
172 res = self.store_picture_with_gif(imager)
176 raise ActiveRecord::Rollback unless res
181 def store_picture_with_gif(imager)
182 if res = self.store_picture(imager, self.filename)
184 if gifimager = imager.to_gif
185 if res = self.store_picture(gifimager, self.gifname)
187 self.errors.add :base, I18n.t('picture_io.error')
190 self.errors.add :base, I18n.t('errors.not_convert')
195 self.errors.add :base, I18n.t('picture_io.error')
200 def store_picture(imager, fn)
202 thumbnail_imager = self.flag_thumbnail >= 0 ? imager.to_thumbnail : imager
203 return false unless thumbnail_imager
205 PictureIO.resource_picture_io.put(thumbnail_imager.binary, fn)
206 PictureIO.resource_picture_io.put(imager.binary, fn, 'full')
208 rescue PictureIO::Error
214 def restore(subdir = nil)
215 PictureIO.resource_picture_io.get self.filename, subdir
220 ResourcePicture.transaction do
221 self.original_picture.published_at = nil
222 self.original_picture.stopped_at = Time.now
223 raise ActiveRecord::Rollback unless self.original_picture.save
225 PictureIO.resource_picture_io.delete(self.filename) if PictureIO.resource_picture_io.exist?(self.filename)
226 PictureIO.resource_picture_io.delete(self.filename, 'full') if PictureIO.resource_picture_io.exist?(self.filename, 'full')
227 rescue PictureIO::Error
229 raise ActiveRecord::Rollback
232 raise ActiveRecord::Rollback unless res
237 def self.visible_count
238 ResourcePicture.count
242 Base64.encode64(self.restore 'full')
246 "#{self.classname.tableize}/attributes/credit"
249 def full_credit_template
250 "#{self.classname.tableize}/attributes/full_credit"
255 @credit_data = JSON.parse(self.credit) unless @credit_data
263 @flags = JSON.parse(self.settings) unless @flags
274 @flag_open = flags["open"] unless @flag_open
279 @flag_commercial = flags["commercial"] unless @flag_commercial
284 @flag_official = flags["official"] unless @flag_official
289 @flag_attribution = flags["attribution"] unless @flag_attribution
294 @flag_derive = flags["derive"] unless @flag_derive
299 @flag_thumbnail = flags["thumbnail"] unless @flag_thumbnail
304 @flag_gif_convert = flags["gif_convert"] unless @flag_gif_convert
309 @flag_reverse = flags["reverse"] unless @flag_reverse
314 @flag_resize = flags["resize"] unless @flag_resize
319 @flag_sync_vh = flags["sync_vh"] unless @flag_sync_vh
324 @flag_overlap = flags["overlap"] unless @flag_overlap