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}"
69 '/resource_pictures/' + (subdir.to_s.empty? ? '' : subdir.to_s + '/' ) + filename
73 tw, th = ResourcePicture.fix_size_both(64, 64, rimg.columns, rimg.rows)
74 ResourcePicture.resize(rimg.to_blob, tw, th).to_blob
78 attr = {:width => op.width, :height => op.height, :ext => op.ext, :filesize => op.filesize,
79 :original_picture_id => op.id, :artist_id => op.artist_id
81 self.attributes = attr
84 def data_to_mgk picture_data
86 mgk = Magick::Image.from_blob(picture_data).shift
88 self.errors.add :base, 'magick failed'
95 d = self.original_picture.restore
109 self.dext == 'png' and self.flag_gif_convert >= 0
112 def self.png_to_gif(data)
115 mgk = Magick::Image.from_blob(data).shift
126 self.copy_data self.original_picture
128 return false unless mgk
129 OriginalPicture.transaction do
130 pc = self.new_picture mgk
132 self.picture_id = pc.id
134 res = self.store_picture_with_gif(mgk)
137 self.errors.add :base, 'picture does not create'
139 raise ActiveRecord::Rollback unless res
144 def store_picture_with_gif(mgk)
145 if res = self.store_picture(mgk)
147 if gifmgk = ResourcePicture.png_to_gif(mgk.to_blob)
148 res = self.store_picture(gifmgk)
150 self.errors.add :base, 'picture data can not conv to gif'
155 self.errors.add :base, 'resource picture io does not work'
160 def store_picture(mgk)
162 tdata = self.flag_thumbnail >= 0 ? thumbnail(mgk) : mgk.to_blob
164 return false unless PictureIO.resource_picture_io.put(tdata, "#{self.id}.#{mgk.format}")
165 PictureIO.resource_picture_io.put(fdata, "#{self.id}.#{mgk.format}", 'full')
168 def restore(subdir = nil)
169 PictureIO.resource_picture_io.get self.filename, subdir
172 def self.default_page_size
176 def self.max_page_size
180 def self.page prm = nil
186 def self.page_size prm = self.default_page_size
188 page_size = self.max_page_size if page_size > self.max_page_size
189 page_size = self.default_page_size if page_size < 1
193 def self.offset cnt, prm = nil
195 offset = cnt - 1 if offset >= cnt
196 offset = cnt - offset.abs if offset < 0
197 offset = 0 if offset < 0
201 def self.list opt = {}, page = 1, page_size = self.default_page_size
202 opt.merge!(self.list_opt) unless opt[:include]
203 opt.merge!({:order => 'updated_at desc', :limit => page_size, :offset => (page -1) * page_size})
204 ResourcePicture.find(:all, opt)
208 {:include => [:license, :artist]}
211 def self.list_json_opt
212 {:include => [:license, :artist]}
215 def self.show rid, opt = {}
216 r = ResourcePicture.find(rid, :include => self.show_include_opt(opt))
217 # raise ActiveRecord::Forbidden unless c.visible?(au)
221 def self.show_include_opt opt = {}
222 res = [:license, :artist]
223 res.push(opt[:include]) if opt[:include]
227 def self.show_json_include_opt
228 {:include => [:license, :artist]}
231 def self.visible_count
232 ResourcePicture.count
236 "#{self.classname.tableize}/credit"
241 @credit_data = JSON.parse(self.credit) unless @credit_data
249 @flags = JSON.parse(self.settings) unless @flags
260 @flag_open = flags["open"] unless @flag_open
265 @flag_commercial = flags["commercial"] unless @flag_commercial
270 @flag_official = flags["official"] unless @flag_official
275 @flag_attribution = flags["attribution"] unless @flag_attribution
280 @flag_derive = flags["derive"] unless @flag_derive
285 @flag_thumbnail = flags["thumbnail"] unless @flag_thumbnail
290 @flag_gif_convert = flags["gif_convert"] unless @flag_gif_convert
295 @flag_reverse = flags["reverse"] unless @flag_reverse
300 @flag_resize = flags["resize"] unless @flag_resize
305 @flag_sync_vh = flags["sync_vh"] unless @flag_sync_vh
310 @flag_overlap = flags["overlap"] unless @flag_overlap