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
33 "#{self.id}.#{self.ext}"
41 '/system_pictures/' + filename
45 {:src => self.url, :width => self.width, :height => self.height}
49 tw, th = PettanImager.thumbnail_size(self.width, self.height)
50 {:src => self.url, :width => tw, :height => th}
53 def self.default_page_size
57 def self.max_page_size
61 def self.page prm = nil
67 def self.page_size prm = self.default_page_size
69 page_size = self.max_page_size if page_size > self.max_page_size
70 page_size = self.default_page_size if page_size < 1
74 def self.list page = 1, page_size = self.default_page_size
76 opt.merge!(self.list_opt)
77 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
78 opt.merge!({:order => 'system_pictures.updated_at desc'})
79 SystemPicture.find(:all, opt)
86 def self.list_json_opt
92 opt.merge!(self.show_opt)
93 res = SystemPicture.find(sid, opt)
94 raise ActiveRecord::Forbidden unless res.visible?(ad)
102 def self.show_json_opt
108 self.errors.add :base, 'illegal picture data'
112 SystemPicture.transaction do
115 res = PictureIO.system_picture_io.put(imager.binary, self.filename)
116 rescue PictureIO::Error
118 self.errors.add :base, I18n.t('picture_io.error')
119 raise ActiveRecord::Rollback
126 def self.store(imager)
127 attr = {:ext => imager.ext, :width => imager.width, :height => imager.height, :filesize => imager.filesize, :md5 => imager.md5}
128 sp = SystemPicture.modify_object(imager.md5, attr, 'md5')
129 res = sp.store imager
133 def restore(subdir = nil)
134 PictureIO.system_picture_io.get self.filename, subdir