OSDN Git Service

temp
[pettanr/pettanr.git] / app / models / system_picture.rb
1 class SystemPicture < Pettanr::Item
2   has_many :balloons
3   has_many :balloon_templates
4   has_many :licenses
5   
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}
11   
12   before_destroy :destroy_with_file
13   
14   def destroy_with_file
15     PictureIO.system_picture_io.delete self.filename
16   end
17   
18   def supply_default
19   end
20   
21   def overwrite
22   end
23   
24   def filename
25     "#{self.id}.#{self.ext}"
26   end
27   
28   def mime_type
29     "image/#{self.ext}"
30   end
31   
32   def url
33     '/system_pictures/' + filename
34   end
35   
36   def opt_img_tag
37     {:src => self.url, :width => self.width, :height => self.height}
38   end
39   
40   def tmb_opt_img_tag
41     tw, th = PettanImager.thumbnail_size(self.width, self.height)
42     {:src => self.url, :width => tw, :height => th}
43   end
44   
45   def symbol_option
46     self.tmb_opt_img_tag
47   end
48   
49   def self.list_where
50     nil
51   end
52   
53   def self.list_order
54     'system_pictures.updated_at desc'
55   end
56   
57   def self.list_opt
58     {}
59   end
60   
61   def self.list_json_opt
62     {}
63   end
64   
65   def self.show_opt
66     {}
67   end
68   
69   def self.show_json_opt
70     {}
71   end
72   
73   def store(imager)
74     unless imager
75       self.errors.add :base, 'illegal picture data'
76       return false
77     end
78     res = false
79     SystemPicture.transaction do
80       if res = self.save
81         begin
82           res = PictureIO.system_picture_io.put(imager.binary, self.filename)
83         rescue PictureIO::Error
84           res = false
85           self.errors.add :base, I18n.t('picture_io.error')
86           raise ActiveRecord::Rollback
87         end
88       end
89     end
90     res
91   end
92   
93   def self.store(imager)
94     attr = {:ext => imager.ext, :width => imager.width, :height => imager.height, :filesize => imager.filesize, :md5 => imager.md5}
95     sp = SystemPicture.modify_object(imager.md5, attr, 'md5')
96     res = sp.store imager
97     res ? sp : nil
98   end
99   
100   def restore(subdir = nil)
101     PictureIO.system_picture_io.get self.filename, subdir
102   end
103   
104 end