2 class ResourcePicture < Pettanr::Item
6 belongs_to :original_picture
8 validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
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 :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
13 validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
14 validates :license_id, :presence => true, :numericality => true, :existence => {:both => false}
15 validates :original_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
16 validates :artist_name, :presence => true
17 validates :classname, :presence => true, :length => {:maximum => 50}
18 validates :picture_id, :presence => true, :numericality => true, :existence => {:both => false}
32 def self.valid_encode_columns
33 super.merge ['artist_name', 'classname', 'credit', 'settings']
40 attr = {:width => op.width, :height => op.height, :ext => op.ext, :filesize => op.filesize,
41 :original_picture_id => op.id, :artist_id => op.artist_id, :md5 => op.md5
43 self.attributes = attr
47 roles = [roles] unless roles.respond_to?(:each)
48 ar = ResourcePicture.get_artist_from_roles roles
49 return false unless ar
50 self.artist_id == ar.id
54 if MagicNumber['run_mode'] == 0
55 return false unless guest_role_check(roles)
57 return false unless resource_reader_role_check(roles)
63 "#{self.id}.#{self.ext}"
75 '/resource_pictures/' + (subdir.to_s.empty? ? '' : subdir.to_s + '/' ) + filename
79 self.ext == 'png' and self.flag_gif_convert >= 0
83 tw, th = ResourcePicture.fix_size_both(MagicNumber['thumbnail_width'], MagicNumber['thumbnail_height'], rimg.columns, rimg.rows)
84 ResourcePicture.resize(rimg.to_blob, tw, th).to_blob
88 tw, th = PettanImager.thumbnail_size(self.width, self.height)
89 {:src => self.url, :width => tw, :height => th}
93 {:src => self.url('full'), :width => self.width, :height => self.height}
100 def self.default_page_size
104 def self.max_page_size
108 def self.page prm = nil
114 def self.page_size prm = self.default_page_size
116 page_size = self.max_page_size if page_size > self.max_page_size
117 page_size = self.default_page_size if page_size < 1
121 def self.mylist_where ar
122 ['resource_pictures.artist_id = ?', ar.id]
125 def self.himlist_where ar
126 ['resource_pictures.artist_id = ?', ar.id]
129 def self.list page = 1, page_size = self.default_page_size
130 ResourcePicture.includes(ResourcePicture.list_opt).order('resource_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
133 def self.mylist ar, page = 1, page_size = Author.default_resource_picture_page_size
134 ResourcePicture.where(self.mylist_where(ar)).includes(ResourcePicture.list_opt).order('resource_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
137 def self.himlist ar, page = 1, page_size = Author.default_resource_picture_page_size
138 ResourcePicture.where(self.himlist_where(ar)).includes(ResourcePicture.list_opt).order('resource_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
141 def self.list_paginate page = 1, page_size = self.default_page_size
142 Kaminari.paginate_array(Array.new(ResourcePicture.count, nil)).page(page).per(page_size)
145 def self.mylist_paginate ar, page = 1, page_size = Author.default_resource_picture_page_size
146 Kaminari.paginate_array(Array.new(ResourcePicture.where(self.mylist_where(ar)).count, nil)).page(page).per(page_size)
149 def self.himlist_paginate ar, page = 1, page_size = Author.default_resource_picture_page_size
150 Kaminari.paginate_array(Array.new(ResourcePicture.where(self.himlist_where(ar)).count, nil)).page(page).per(page_size)
153 def self.list_by_original_picture_where original_picture_id
154 ['resource_pictures.original_picture_id = ?', original_picture_id]
157 def self.list_by_original_picture original_picture_id, roles, page = 1, page_size = self.default_page_size
158 self.where(self.list_by_original_picture_where(original_picture_id)).includes(self.list_opt).order('resource_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
161 def self.list_by_license_where license_id
162 ['resource_pictures.license_id = ?', license_id]
165 def self.list_by_license license_id, roles, page = 1, page_size = self.default_page_size
166 self.where(self.list_by_license_where(license_id)).includes(self.list_opt).order('resource_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
169 def self.list_by_artist_where artist_id
170 ['resource_pictures.artist_id = ?', artist_id]
173 def self.list_by_artist artist_id, roles, page = 1, page_size = self.default_page_size
174 self.where(self.list_by_artist_where(artist_id)).includes(self.list_opt).order('resource_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
178 {:license => {}, :artist => {}, :picture => {} }
181 def self.list_json_opt
182 {:include => {:license => {}, :artist => {}, :picture => {}} }
185 def self.show rid, roles
187 opt.merge!(self.show_opt)
188 r = ResourcePicture.find(rid, opt)
189 raise ActiveRecord::Forbidden unless r.visible?(roles)
194 {:include => {:license => {}, :artist => {}, :picture => {}} }
197 def self.show_json_opt
198 {:include => {:license => {}, :artist => {}, :picture => {}} }
201 def self.edit rid, ar
203 opt.merge!(self.show_opt)
204 r = ResourcePicture.find(rid, opt)
205 raise ActiveRecord::Forbidden unless r.own?(ar)
209 def new_picture imager
215 self.errors.add :base, Picture.model_name.human + I18n.t('errors.not_create')
220 return false unless imager
222 self.overwrite self.original_picture
223 ResourcePicture.transaction do
224 self.original_picture.published_at = Time.now
225 self.original_picture.stopped_at = nil
226 raise ActiveRecord::Rollback unless self.original_picture.save
227 pc = self.new_picture imager
229 self.picture_id = pc.id
231 res = self.store_picture_with_gif(imager)
235 raise ActiveRecord::Rollback unless res
240 def store_picture_with_gif(imager)
241 if res = self.store_picture(imager, self.filename)
243 if gifimager = imager.to_gif
244 if res = self.store_picture(gifimager, self.gifname)
246 self.errors.add :base, I18n.t('picture_io.error')
249 self.errors.add :base, I18n.t('errors.not_convert')
254 self.errors.add :base, I18n.t('picture_io.error')
259 def store_picture(imager, fn)
261 thumbnail_imager = self.flag_thumbnail >= 0 ? imager.to_thumbnail : imager
262 return false unless thumbnail_imager
264 PictureIO.resource_picture_io.put(thumbnail_imager.binary, fn)
265 PictureIO.resource_picture_io.put(imager.binary, fn, 'full')
267 rescue PictureIO::Error
273 def restore(subdir = nil)
274 PictureIO.resource_picture_io.get self.filename, subdir
279 ResourcePicture.transaction do
280 self.original_picture.published_at = nil
281 self.original_picture.stopped_at = Time.now
282 raise ActiveRecord::Rollback unless self.original_picture.save
284 PictureIO.resource_picture_io.delete(self.filename) if PictureIO.resource_picture_io.exist?(self.filename)
285 PictureIO.resource_picture_io.delete(self.filename, 'full') if PictureIO.resource_picture_io.exist?(self.filename, 'full')
286 rescue PictureIO::Error
288 raise ActiveRecord::Rollback
291 raise ActiveRecord::Rollback unless res
296 def self.visible_count
297 ResourcePicture.count
301 Base64.encode64(self.restore 'full')
305 "#{self.classname.tableize}/attributes/credit"
308 def full_credit_template
309 "#{self.classname.tableize}/attributes/full_credit"
314 @credit_data = JSON.parse(self.credit) unless @credit_data
322 @flags = JSON.parse(self.settings) unless @flags
333 @flag_open = flags["open"] unless @flag_open
338 @flag_commercial = flags["commercial"] unless @flag_commercial
343 @flag_official = flags["official"] unless @flag_official
348 @flag_attribution = flags["attribution"] unless @flag_attribution
353 @flag_derive = flags["derive"] unless @flag_derive
358 @flag_thumbnail = flags["thumbnail"] unless @flag_thumbnail
363 @flag_gif_convert = flags["gif_convert"] unless @flag_gif_convert
368 @flag_reverse = flags["reverse"] unless @flag_reverse
373 @flag_resize = flags["resize"] unless @flag_resize
378 @flag_sync_vh = flags["sync_vh"] unless @flag_sync_vh
383 @flag_overlap = flags["overlap"] unless @flag_overlap