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 => true
15 validates :license_id, :presence => true, :numericality => true, :existence => true
16 validates :original_picture_id, :presence => true, :numericality => true, :existence => true
17 validates :artist_name, :presence => true
18 validates :classname, :presence => true, :length => {:maximum => 50}
19 validates :picture_id, :presence => true, :numericality => true, :existence => true
21 before_destroy :destroy_with_file
24 PictureIO.resource_picture_io.delete self.filename
25 PictureIO.resource_picture_io.class.subdirs.each do |d|
27 PictureIO.resource_picture_io.delete(self.filename, d) if PictureIO.resource_picture_io.exist?(self.filename, d)
35 attr = {:width => op.width, :height => op.height, :ext => op.ext, :filesize => op.filesize,
36 :original_picture_id => op.id, :artist_id => op.artist_id, :md5 => op.md5
38 self.attributes = attr
50 "#{self.id}.#{self.ext}"
62 '/resource_pictures/' + (subdir.to_s.empty? ? '' : subdir.to_s + '/' ) + filename
66 self.ext == 'png' and self.flag_gif_convert >= 0
70 tw, th = ResourcePicture.fix_size_both(MagicNumber['thumbnail_width'], MagicNumber['thumbnail_height'], rimg.columns, rimg.rows)
71 ResourcePicture.resize(rimg.to_blob, tw, th).to_blob
75 tw, th = ResourcePicture.fix_size_both(MagicNumber['thumbnail_width'], MagicNumber['thumbnail_height'], self.width, self.height)
76 {:src => self.url, :width => tw, :height => th}
79 def self.default_page_size
83 def self.max_page_size
87 def self.page prm = nil
93 def self.page_size prm = self.default_page_size
95 page_size = self.max_page_size if page_size > self.max_page_size
96 page_size = self.default_page_size if page_size < 1
100 def self.list page = 1, page_size = self.default_page_size
102 opt.merge!(self.list_opt)
103 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
104 opt.merge!({:order => 'updated_at desc'})
105 ResourcePicture.find(:all, opt)
109 {:include => {:license => {}, :artist => {}, :picture => {}} }
112 def self.list_json_opt
113 {:include => {:license => {}, :artist => {}, :picture => {}} }
116 def self.show rid, au
118 opt.merge!(self.show_opt)
119 r = ResourcePicture.find(rid, opt)
120 raise ActiveRecord::Forbidden unless r.visible?(au)
125 {:include => {:license => {}, :artist => {}, :picture => {}} }
128 def self.show_json_opt
129 {:include => {:license => {}, :artist => {}, :picture => {}} }
132 def new_picture imager
138 self.errors.add :base, 'picture does not create' + pc.errors.full_messages.join("\n")
143 return false unless imager
145 self.overwrite self.original_picture
146 ResourcePicture.transaction do
147 pc = self.new_picture imager
149 self.picture_id = pc.id
151 res = self.store_picture_with_gif(imager)
155 raise ActiveRecord::Rollback unless res
160 def store_picture_with_gif(imager)
161 if res = self.store_picture(imager, self.filename)
163 if gifimager = imager.to_gif
164 res = self.store_picture(gifimager, self.gifname)
166 self.errors.add :base, 'picture data can not conv to gif'
171 self.errors.add :base, 'resource picture io does not work'
176 def store_picture(imager, fn)
178 thumbnail_imager = self.flag_thumbnail >= 0 ? imager.to_thumbnail : imager
179 return false unless thumbnail_imager
180 return false unless PictureIO.resource_picture_io.put(thumbnail_imager.binary, fn)
181 return false unless PictureIO.resource_picture_io.put(imager.binary, fn, 'full')
185 def restore(subdir = nil)
186 PictureIO.resource_picture_io.get self.filename, subdir
189 def self.visible_count
190 ResourcePicture.count
194 "#{self.classname.tableize}/attributes/credit"
199 @credit_data = JSON.parse(self.credit) unless @credit_data
207 @flags = JSON.parse(self.settings) unless @flags
218 @flag_open = flags["open"] unless @flag_open
223 @flag_commercial = flags["commercial"] unless @flag_commercial
228 @flag_official = flags["official"] unless @flag_official
233 @flag_attribution = flags["attribution"] unless @flag_attribution
238 @flag_derive = flags["derive"] unless @flag_derive
243 @flag_thumbnail = flags["thumbnail"] unless @flag_thumbnail
248 @flag_gif_convert = flags["gif_convert"] unless @flag_gif_convert
253 @flag_reverse = flags["reverse"] unless @flag_reverse
258 @flag_resize = flags["resize"] unless @flag_resize
263 @flag_sync_vh = flags["sync_vh"] unless @flag_sync_vh
268 @flag_overlap = flags["overlap"] unless @flag_overlap