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 pc = self.new_picture imager
167 self.picture_id = pc.id
169 res = self.store_picture_with_gif(imager)
173 raise ActiveRecord::Rollback unless res
178 def store_picture_with_gif(imager)
179 if res = self.store_picture(imager, self.filename)
181 if gifimager = imager.to_gif
182 if res = self.store_picture(gifimager, self.gifname)
184 self.errors.add :base, I18n.t('picture_io.error')
187 self.errors.add :base, I18n.t('errors.not_convert')
192 self.errors.add :base, I18n.t('picture_io.error')
197 def store_picture(imager, fn)
199 thumbnail_imager = self.flag_thumbnail >= 0 ? imager.to_thumbnail : imager
200 return false unless thumbnail_imager
202 PictureIO.resource_picture_io.put(thumbnail_imager.binary, fn)
203 PictureIO.resource_picture_io.put(imager.binary, fn, 'full')
205 rescue PictureIO::Error
211 def restore(subdir = nil)
212 PictureIO.resource_picture_io.get self.filename, subdir
217 ResourcePicture.transaction do
219 PictureIO.resource_picture_io.delete(self.filename) if PictureIO.resource_picture_io.exist?(self.filename)
220 PictureIO.resource_picture_io.delete(self.filename, 'full') if PictureIO.resource_picture_io.exist?(self.filename, 'full')
221 rescue PictureIO::Error
223 raise ActiveRecord::Rollback
226 raise ActiveRecord::Rollback unless res
231 def self.visible_count
232 ResourcePicture.count
236 Base64.encode64(self.restore 'full')
240 "#{self.classname.tableize}/attributes/credit"
243 def full_credit_template
244 "#{self.classname.tableize}/attributes/full_credit"
249 @credit_data = JSON.parse(self.credit) unless @credit_data
257 @flags = JSON.parse(self.settings) unless @flags
268 @flag_open = flags["open"] unless @flag_open
273 @flag_commercial = flags["commercial"] unless @flag_commercial
278 @flag_official = flags["official"] unless @flag_official
283 @flag_attribution = flags["attribution"] unless @flag_attribution
288 @flag_derive = flags["derive"] unless @flag_derive
293 @flag_thumbnail = flags["thumbnail"] unless @flag_thumbnail
298 @flag_gif_convert = flags["gif_convert"] unless @flag_gif_convert
303 @flag_reverse = flags["reverse"] unless @flag_reverse
308 @flag_resize = flags["resize"] unless @flag_resize
313 @flag_sync_vh = flags["sync_vh"] unless @flag_sync_vh
318 @flag_overlap = flags["overlap"] unless @flag_overlap