1 class ResourcePicture < ActiveRecord::Base
5 has_many :panel_pictures
6 belongs_to :original_picture
8 validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
\r
9 validates :width, :presence => true, :numericality => true, :natural_number => true
10 validates :height, :presence => true, :numericality => true, :natural_number => true
11 validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
12 validates :artist_id, :presence => true, :numericality => true, :existence => true
13 validates :license_id, :presence => true, :numericality => true, :existence => true
14 validates :original_picture_id, :presence => true, :numericality => true, :existence => true
15 validates :artist_name, :presence => true
16 validates :picture_id, :presence => true, :numericality => true, :existence => true
18 before_destroy :destroy_with_file
21 PictureIO.resource_picture_io.delete self.filename
22 PictureIO.resource_picture_io.class.subdirs.each do |d|
24 PictureIO.resource_picture_io.delete(self.filename, d) if PictureIO.resource_picture_io.exist?(self.filename, d)
28 def self.resize(data, dw, dh)
29 Magick::Image.from_blob(data).shift.resize(dw, dh)
32 #サイズの調整(limw,limhに必ず収まるように合わせる)
33 def self.fix_size_both(limw, limh, w, h)
59 "#{self.id}.#{self.dext}"
67 '/resource_pictures/' + (subdir.to_s.empty? ? '' : subdir.to_s + '/' ) + filename
71 tw, th = ResourcePicture.fix_size_both(64, 64, rimg.columns, rimg.rows)
72 ResourcePicture.resize(rimg.to_blob, tw, th).to_blob
76 attr = {:width => op.width, :height => op.height, :ext => op.ext, :filesize => op.filesize,
77 :original_picture_id => op.id, :artist_id => op.artist_id
79 self.attributes = attr
82 def data_to_mgk picture_data
84 mgk = Magick::Image.from_blob(picture_data).shift
86 self.errors.add :base, 'magick failed'
93 d = self.original_picture.restore
107 self.dext == 'png' and self.flag_gif_convert >= 0
110 def self.png_to_gif(data)
113 mgk = Magick::Image.from_blob(data).shift
124 self.copy_data self.original_picture
126 return false unless mgk
127 OriginalPicture.transaction do
128 pc = self.new_picture mgk
130 self.picture_id = pc.id
132 res = self.store_picture_with_gif(mgk)
135 self.errors.add :base, 'picture does not create'
137 raise ActiveRecord::Rollback unless res
142 def store_picture_with_gif(mgk)
143 if res = self.store_picture(mgk)
145 if gifmgk = ResourcePicture.png_to_gif(mgk.to_blob)
146 res = self.store_picture(gifmgk)
148 self.errors.add :base, 'picture data can not conv to gif'
153 self.errors.add :base, 'resource picture io does not work'
158 def store_picture(mgk)
160 tdata = self.flag_thumbnail >= 0 ? thumbnail(mgk) : mgk.to_blob
162 return false unless PictureIO.resource_picture_io.put(tdata, "#{self.id}.#{mgk.format}")
163 PictureIO.resource_picture_io.put(fdata, "#{self.id}.#{mgk.format}", 'full')
166 def restore(subdir = nil)
167 PictureIO.resource_picture_io.get self.filename, subdir
170 def self.default_page_size
174 def self.max_page_size
178 def self.page prm = nil
184 def self.page_size prm = self.default_page_size
186 page_size = self.max_page_size if page_size > self.max_page_size
187 page_size = self.default_page_size if page_size < 1
191 def self.offset cnt, prm = nil
193 offset = cnt - 1 if offset >= cnt
194 offset = cnt - offset.abs if offset < 0
195 offset = 0 if offset < 0
199 def self.list opt = {}, page = 1, page_size = self.default_page_size
200 opt.merge!(self.list_opt) unless opt[:include]
201 opt.merge!({:order => 'updated_at desc', :limit => page_size, :offset => (page -1) * page_size})
202 ResourcePicture.find(:all, opt)
206 {:include => [:license, :artist]}
209 def self.list_json_opt
210 {:include => [:license, :artist]}
213 def self.show rid, opt = {}
214 r = ResourcePicture.find(rid, :include => self.show_include_opt(opt))
215 # raise ActiveRecord::Forbidden unless c.visible?(au)
219 def self.show_include_opt opt = {}
220 res = [:license, :artist]
221 res.push(opt[:include]) if opt[:include]
225 def self.show_json_include_opt
226 {:include => [:license, :artist]}
229 def self.visible_count
230 ResourcePicture.count
235 @flags = JSON.parse(self.settings) unless @flags
246 @flag_open = flags["open"] unless @flag_open
251 @flag_commercial = flags["commercial"] unless @flag_commercial
256 @flag_official = flags["official"] unless @flag_official
261 @flag_attribution = flags["attribution"] unless @flag_attribution
266 @flag_derive = flags["derive"] unless @flag_derive
271 @flag_thumbnail = flags["thumbnail"] unless @flag_thumbnail
276 @flag_gif_convert = flags["gif_convert"] unless @flag_gif_convert
281 @flag_reverse = flags["reverse"] unless @flag_reverse
286 @flag_resize = flags["resize"] unless @flag_resize
291 @flag_sync_vh = flags["sync_vh"] unless @flag_sync_vh
296 @flag_overlap = flags["overlap"] unless @flag_overlap