2 class ResourcePicture < ActiveRecord::Base
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}
20 before_validation :valid_encode
23 ['artist_name', 'classname', 'credit', 'settings'].each do |a|
24 next if attributes[a] == nil
25 raise Pettanr::BadRequest unless attributes[a].valid_encoding?
33 attr = {:width => op.width, :height => op.height, :ext => op.ext, :filesize => op.filesize,
34 :original_picture_id => op.id, :artist_id => op.artist_id, :md5 => op.md5
36 self.attributes = attr
40 roles = [roles] unless roles.respond_to?(:each)
41 ar = ResourcePicture.get_artist_from_roles roles
42 return false unless ar
43 self.artist_id == ar.id
47 if MagicNumber['run_mode'] == 0
48 return false unless guest_role_check(roles)
50 return false unless resource_reader_role_check(roles)
56 "#{self.id}.#{self.ext}"
68 '/resource_pictures/' + (subdir.to_s.empty? ? '' : subdir.to_s + '/' ) + filename
72 self.ext == 'png' and self.flag_gif_convert >= 0
76 tw, th = ResourcePicture.fix_size_both(MagicNumber['thumbnail_width'], MagicNumber['thumbnail_height'], rimg.columns, rimg.rows)
77 ResourcePicture.resize(rimg.to_blob, tw, th).to_blob
81 tw, th = PettanImager.thumbnail_size(self.width, self.height)
82 {:src => self.url, :width => tw, :height => th}
86 {:src => self.url('full'), :width => self.width, :height => self.height}
93 def self.default_page_size
97 def self.max_page_size
101 def self.page prm = nil
107 def self.page_size prm = self.default_page_size
109 page_size = self.max_page_size if page_size > self.max_page_size
110 page_size = self.default_page_size if page_size < 1
114 def self.mylist_where ar
115 ['resource_pictures.artist_id = ?', ar.id]
118 def self.himlist_where ar
119 ['resource_pictures.artist_id = ?', ar.id]
122 def self.list page = 1, page_size = self.default_page_size
123 ResourcePicture.includes(ResourcePicture.list_opt).order('resource_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
126 def self.mylist ar, page = 1, page_size = Author.default_resource_picture_page_size
127 ResourcePicture.where(self.mylist_where(ar)).includes(ResourcePicture.list_opt).order('resource_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
130 def self.himlist ar, page = 1, page_size = Author.default_resource_picture_page_size
131 ResourcePicture.where(self.himlist_where(ar)).includes(ResourcePicture.list_opt).order('resource_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
134 def self.list_paginate page = 1, page_size = self.default_page_size
135 Kaminari.paginate_array(Array.new(ResourcePicture.count, nil)).page(page).per(page_size)
138 def self.mylist_paginate ar, page = 1, page_size = Author.default_resource_picture_page_size
139 Kaminari.paginate_array(Array.new(ResourcePicture.where(self.mylist_where(ar)).count, nil)).page(page).per(page_size)
142 def self.himlist_paginate ar, page = 1, page_size = Author.default_resource_picture_page_size
143 Kaminari.paginate_array(Array.new(ResourcePicture.where(self.himlist_where(ar)).count, nil)).page(page).per(page_size)
146 def self.list_by_original_picture_where original_picture_id
147 ['resource_pictures.original_picture_id = ?', original_picture_id]
150 def self.list_by_original_picture original_picture_id, roles, page = 1, page_size = self.default_page_size
151 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)
154 def self.list_by_license_where license_id
155 ['resource_pictures.license_id = ?', license_id]
158 def self.list_by_license license_id, roles, page = 1, page_size = self.default_page_size
159 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)
162 def self.list_by_artist_where artist_id
163 ['resource_pictures.artist_id = ?', artist_id]
166 def self.list_by_artist artist_id, roles, page = 1, page_size = self.default_page_size
167 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)
171 {:license => {}, :artist => {}, :picture => {} }
174 def self.list_json_opt
175 {:include => {:license => {}, :artist => {}, :picture => {}} }
178 def self.show rid, roles
180 opt.merge!(self.show_opt)
181 r = ResourcePicture.find(rid, opt)
182 raise ActiveRecord::Forbidden unless r.visible?(roles)
187 {:include => {:license => {}, :artist => {}, :picture => {}} }
190 def self.show_json_opt
191 {:include => {:license => {}, :artist => {}, :picture => {}} }
194 def self.edit rid, ar
196 opt.merge!(self.show_opt)
197 r = ResourcePicture.find(rid, opt)
198 raise ActiveRecord::Forbidden unless r.own?(ar)
202 def new_picture imager
208 self.errors.add :base, Picture.model_name.human + I18n.t('errors.not_create')
213 return false unless imager
215 self.overwrite self.original_picture
216 ResourcePicture.transaction do
217 self.original_picture.published_at = Time.now
218 self.original_picture.stopped_at = nil
219 raise ActiveRecord::Rollback unless self.original_picture.save
220 pc = self.new_picture imager
222 self.picture_id = pc.id
224 res = self.store_picture_with_gif(imager)
228 raise ActiveRecord::Rollback unless res
233 def store_picture_with_gif(imager)
234 if res = self.store_picture(imager, self.filename)
236 if gifimager = imager.to_gif
237 if res = self.store_picture(gifimager, self.gifname)
239 self.errors.add :base, I18n.t('picture_io.error')
242 self.errors.add :base, I18n.t('errors.not_convert')
247 self.errors.add :base, I18n.t('picture_io.error')
252 def store_picture(imager, fn)
254 thumbnail_imager = self.flag_thumbnail >= 0 ? imager.to_thumbnail : imager
255 return false unless thumbnail_imager
257 PictureIO.resource_picture_io.put(thumbnail_imager.binary, fn)
258 PictureIO.resource_picture_io.put(imager.binary, fn, 'full')
260 rescue PictureIO::Error
266 def restore(subdir = nil)
267 PictureIO.resource_picture_io.get self.filename, subdir
272 ResourcePicture.transaction do
273 self.original_picture.published_at = nil
274 self.original_picture.stopped_at = Time.now
275 raise ActiveRecord::Rollback unless self.original_picture.save
277 PictureIO.resource_picture_io.delete(self.filename) if PictureIO.resource_picture_io.exist?(self.filename)
278 PictureIO.resource_picture_io.delete(self.filename, 'full') if PictureIO.resource_picture_io.exist?(self.filename, 'full')
279 rescue PictureIO::Error
281 raise ActiveRecord::Rollback
284 raise ActiveRecord::Rollback unless res
289 def self.visible_count
290 ResourcePicture.count
294 Base64.encode64(self.restore 'full')
298 "#{self.classname.tableize}/attributes/credit"
301 def full_credit_template
302 "#{self.classname.tableize}/attributes/full_credit"
307 @credit_data = JSON.parse(self.credit) unless @credit_data
315 @flags = JSON.parse(self.settings) unless @flags
326 @flag_open = flags["open"] unless @flag_open
331 @flag_commercial = flags["commercial"] unless @flag_commercial
336 @flag_official = flags["official"] unless @flag_official
341 @flag_attribution = flags["attribution"] unless @flag_attribution
346 @flag_derive = flags["derive"] unless @flag_derive
351 @flag_thumbnail = flags["thumbnail"] unless @flag_thumbnail
356 @flag_gif_convert = flags["gif_convert"] unless @flag_gif_convert
361 @flag_reverse = flags["reverse"] unless @flag_reverse
366 @flag_resize = flags["resize"] unless @flag_resize
371 @flag_sync_vh = flags["sync_vh"] unless @flag_sync_vh
376 @flag_overlap = flags["overlap"] unless @flag_overlap