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 :artist_id, :presence => true, :numericality => true, :existence => true
14 validates :license_id, :presence => true, :numericality => true, :existence => true
15 validates :original_picture_id, :presence => true, :numericality => true, :existence => true
16 validates :artist_name, :presence => true
17 validates :classname, :presence => true, :length => {:maximum => 50}
18 validates :picture_id, :presence => true, :numericality => true, :existence => true
20 before_destroy :destroy_with_file
23 PictureIO.resource_picture_io.delete self.filename
24 PictureIO.resource_picture_io.class.subdirs.each do |d|
26 PictureIO.resource_picture_io.delete(self.filename, d) if PictureIO.resource_picture_io.exist?(self.filename, d)
30 def self.resize(data, dw, dh)
31 Magick::Image.from_blob(data).shift.resize(dw, dh)
34 #サイズの調整(limw,limhに必ず収まるように合わせる)
35 def self.fix_size_both(limw, limh, w, h)
61 "#{self.id}.#{self.dext}"
73 '/resource_pictures/' + (subdir.to_s.empty? ? '' : subdir.to_s + '/' ) + filename
77 tw, th = ResourcePicture.fix_size_both(MagicNumber['thumbnail_width'], MagicNumber['thumbnail_height'], rimg.columns, rimg.rows)
78 ResourcePicture.resize(rimg.to_blob, tw, th).to_blob
82 tw, th = ResourcePicture.fix_size_both(MagicNumber['thumbnail_width'], MagicNumber['thumbnail_height'], self.width, self.height)
83 {:src => self.url, :width => tw, :height => th}
87 attr = {:width => op.width, :height => op.height, :ext => op.ext, :filesize => op.filesize,
88 :original_picture_id => op.id, :artist_id => op.artist_id, :md5 => op.md5
90 self.attributes = attr
93 def data_to_mgk picture_data
95 mgk = Magick::Image.from_blob(picture_data).shift
97 self.errors.add :base, 'magick failed'
104 d = self.original_picture.restore
105 return false unless d
118 self.dext == 'png' and self.flag_gif_convert >= 0
121 def self.png_to_gif(data)
124 mgk = Magick::Image.from_blob(data).shift
135 self.copy_data self.original_picture
137 return false unless mgk
138 OriginalPicture.transaction do
139 pc = self.new_picture mgk
141 self.picture_id = pc.id
143 res = self.store_picture_with_gif(mgk)
146 self.errors.add :base, 'picture does not create'
148 raise ActiveRecord::Rollback unless res
153 def store_picture_with_gif(mgk)
154 if res = self.store_picture(mgk, self.filename)
156 if gifmgk = ResourcePicture.png_to_gif(mgk.to_blob)
157 res = self.store_picture(gifmgk, self.gifname)
159 self.errors.add :base, 'picture data can not conv to gif'
164 self.errors.add :base, 'resource picture io does not work'
169 def store_picture(mgk, fn)
171 tdata = self.flag_thumbnail >= 0 ? thumbnail(mgk) : mgk.to_blob
173 return false unless PictureIO.resource_picture_io.put(tdata, fn)
174 PictureIO.resource_picture_io.put(fdata, fn, 'full')
177 def restore(subdir = nil)
178 PictureIO.resource_picture_io.get self.filename, subdir
181 def self.default_page_size
185 def self.max_page_size
189 def self.page prm = nil
195 def self.page_size prm = self.default_page_size
197 page_size = self.max_page_size if page_size > self.max_page_size
198 page_size = self.default_page_size if page_size < 1
202 def self.offset cnt, prm = nil
204 offset = cnt - 1 if offset >= cnt
205 offset = cnt - offset.abs if offset < 0
206 offset = 0 if offset < 0
210 def self.list opt = {}, page = 1, page_size = self.default_page_size
211 opt.merge!(self.list_opt) unless opt[:include]
212 opt.merge!({:order => 'updated_at desc', :limit => page_size, :offset => (page -1) * page_size})
213 ResourcePicture.find(:all, opt)
217 {:include => [:license, :artist]}
220 def self.list_json_opt
221 {:include => [:license, :artist]}
224 def self.show rid, opt = {}
225 r = ResourcePicture.find(rid, :include => self.show_include_opt(opt))
226 # raise ActiveRecord::Forbidden unless c.visible?(au)
230 def self.show_include_opt opt = {}
231 res = [:license, :artist]
232 res.push(opt[:include]) if opt[:include]
236 def self.show_json_include_opt
237 {:include => [:license, :artist]}
240 def self.visible_count
241 ResourcePicture.count
245 "#{self.classname.tableize}/attributes/credit"
250 @credit_data = JSON.parse(self.credit) unless @credit_data
258 @flags = JSON.parse(self.settings) unless @flags
269 @flag_open = flags["open"] unless @flag_open
274 @flag_commercial = flags["commercial"] unless @flag_commercial
279 @flag_official = flags["official"] unless @flag_official
284 @flag_attribution = flags["attribution"] unless @flag_attribution
289 @flag_derive = flags["derive"] unless @flag_derive
294 @flag_thumbnail = flags["thumbnail"] unless @flag_thumbnail
299 @flag_gif_convert = flags["gif_convert"] unless @flag_gif_convert
304 @flag_reverse = flags["reverse"] unless @flag_reverse
309 @flag_resize = flags["resize"] unless @flag_resize
314 @flag_sync_vh = flags["sync_vh"] unless @flag_sync_vh
319 @flag_overlap = flags["overlap"] unless @flag_overlap