OSDN Git Service

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