1 class SystemPicture < ActiveRecord::Base
3 has_many :balloon_templates
6 validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
7 validates :width, :presence => true, :numericality => true, :natural_number => true
8 validates :height, :presence => true, :numericality => true, :natural_number => true
9 validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
10 validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
12 before_destroy :destroy_with_file
15 PictureIO.system_picture_io.delete self.filename
25 roles = [roles] unless roles.respond_to?(:each)
26 ad = SystemPicture.get_admin_from_roles roles
27 return false unless ad
36 "#{self.id}.#{self.ext}"
44 '/system_pictures/' + filename
48 {:src => self.url, :width => self.width, :height => self.height}
52 tw, th = PettanImager.thumbnail_size(self.width, self.height)
53 {:src => self.url, :width => tw, :height => th}
56 def self.default_page_size
60 def self.max_page_size
64 def self.page prm = nil
70 def self.page_size prm = self.default_page_size
72 page_size = self.max_page_size if page_size > self.max_page_size
73 page_size = self.default_page_size if page_size < 1
77 def self.list page = 1, page_size = self.default_page_size
79 opt.merge!(self.list_opt)
80 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
81 opt.merge!({:order => 'system_pictures.updated_at desc'})
82 SystemPicture.find(:all, opt)
89 def self.list_json_opt
95 opt.merge!(self.show_opt)
96 res = SystemPicture.find(sid, opt)
97 raise ActiveRecord::Forbidden unless res.visible?(ad)
105 def self.show_json_opt
111 self.errors.add :base, 'illegal picture data'
115 SystemPicture.transaction do
118 res = PictureIO.system_picture_io.put(imager.binary, self.filename)
119 rescue PictureIO::Error
121 self.errors.add :base, I18n.t('picture_io.error')
122 raise ActiveRecord::Rollback
129 def self.store(imager)
130 attr = {:ext => imager.ext, :width => imager.width, :height => imager.height, :filesize => imager.filesize, :md5 => imager.md5}
131 sp = SystemPicture.modify_object(imager.md5, attr, 'md5')
132 res = sp.store imager
136 def restore(subdir = nil)
137 PictureIO.system_picture_io.get self.filename, subdir