OSDN Git Service

classname rename to module_name
[pettanr/pettanr.git] / app / models / resource_picture.rb
1 #素材
2 class ResourcePicture < Peta::Content
3   load_manifest
4   belongs_to :artist
5   belongs_to :license
6   belongs_to :system_picture
7   belongs_to :picture
8   belongs_to :original_picture
9   
10   validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
11   validates :width, :presence => true, :numericality => true, :natural_number => true
12   validates :height, :presence => true, :numericality => true, :natural_number => true
13   validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
14   validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
15   validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
16   validates :license_id, :presence => true, :numericality => true, :existence => {:both => false}
17   validates :system_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
18   validates :original_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
19   validates :artist_name, :presence => true
20   validates :license_group_module_name, :presence => true, :length => {:maximum => 50}
21   validates :picture_id, :presence => true, :numericality => true, :existence => {:both => false}
22   
23   def supply_default
24   end
25   
26   def overwrite op
27     attr = {:width => op.width, :height => op.height, :ext => op.ext, :filesize => op.filesize, 
28       :original_picture_id => op.id, :artist_id => op.artist_id, :md5 => op.md5
29     }
30     self.attributes = attr
31   end
32   
33   def visible? operators
34     # no super
35     # content model call to owner checker
36     self.user_visible? operators
37   end
38   
39   def filename
40     "#{self.id}.#{self.ext}"
41   end
42   
43   def gifname
44     "#{self.id}.gif"
45   end
46   
47   def mime_type
48     "image/#{self.ext}"
49   end
50   
51   def url subdir = nil
52     '/resource_pictures/' + filename + (subdir.to_s.empty? ? '' : '?subdir=' + subdir.to_s)
53   end
54   
55   def to_gif?
56     self.ext == 'png' and self.license_extend.gif_convert >= 0
57   end
58   
59   def thumbnail(imager)
60     tw, th = ResourcePicture.fix_size_both(Manifest.manifest.magic_numbers['thumbnail_width'], Manifest.manifest.magic_numbers['thumbnail_height'], rimg.columns, rimg.rows)
61     ResourcePicture.resize(rimg.to_blob, tw, th).to_blob
62   end
63   
64   def tmb_opt_img_tag
65     tw, th = PettanImager.thumbnail_size(self.width, self.height)
66     {:src => self.url, :width => tw, :height => th}
67   end
68   
69   def opt_img_tag
70     {:src => self.url('full'), :width => self.width, :height => self.height}
71   end
72   
73   def alt_name
74     self.license.license_group.caption.to_s + '[' + self.license.caption.to_s + ']'
75   end
76   
77   def symbol_option
78     self.tmb_opt_img_tag
79   end
80   
81   def self.list_order
82     'resource_pictures.updated_at desc'
83   end
84   
85   def self.list_where
86     ''
87   end
88   
89   def self.list_opt
90     {:license => {}, :artist => {}, :picture => {} }
91   end
92   
93   def self.list_json_opt
94     {:include => {:license => {}, :artist => {}, :picture => {}} }
95   end
96   
97   def self.show_opt
98     {:include => {:license => {}, :artist => {}, :picture => {}} }
99   end
100   
101   def self.show_json_opt
102     {:include => {:license => {}, :artist => {}, :picture => {}} }
103   end
104   
105   def new_picture imager
106     pc = Picture.new
107     pc.supply_default
108     pc.overwrite self
109     pc.boosts 'post'
110     r = pc.store imager
111     return pc if r
112     self.errors.add :base, Picture.model_name.human + I18n.t('errors.not_create')
113     false
114   end
115   
116   def store imager
117     return false unless imager
118     res = false
119     self.overwrite self.original_picture
120     ResourcePicture.transaction do
121       self.original_picture.published_at = Time.now
122       self.original_picture.stopped_at = nil
123       raise ActiveRecord::Rollback unless self.original_picture.save
124       pc = self.new_picture imager
125       if pc
126         self.picture_id = pc.id
127         if res = self.save
128           res = self.store_picture_with_gif(imager)
129         end
130       else
131       end
132       raise ActiveRecord::Rollback unless res
133     end
134     res
135   end
136   
137   def store_picture_with_gif(imager)
138     if res = self.store_picture(imager, self.filename)
139       if self.to_gif?
140         if gifimager = imager.to_gif
141           if res = self.store_picture(gifimager, self.gifname)
142           else
143             self.errors.add :base, I18n.t('picture_io.error')
144           end
145         else
146           self.errors.add :base, I18n.t('errors.not_convert')
147           res = false
148         end
149       end
150     else
151       self.errors.add :base, I18n.t('picture_io.error')
152     end
153     res
154   end
155   
156   def store_picture(imager, fn)
157     res = false
158     thumbnail_imager = self.license_extend.thumbnail >= 0 ? imager.to_thumbnail : imager
159     return false unless thumbnail_imager
160     begin
161       PictureIO.resource_picture_io.put(thumbnail_imager.binary, fn)
162       PictureIO.resource_picture_io.put(imager.binary, fn, 'full')
163       res = true
164     rescue PictureIO::Error
165       res = false
166     end
167     res
168   end
169   
170   def restore(subdir = nil)
171     PictureIO.resource_picture_io.get self.filename, subdir
172   end
173   
174   def unpublish
175     res = false
176     ResourcePicture.transaction do
177       self.original_picture.published_at = nil
178       self.original_picture.stopped_at = Time.now
179       raise ActiveRecord::Rollback unless self.original_picture.save
180       begin
181         PictureIO.resource_picture_io.delete(self.filename) if PictureIO.resource_picture_io.exist?(self.filename)
182         PictureIO.resource_picture_io.delete(self.filename, 'full') if PictureIO.resource_picture_io.exist?(self.filename, 'full')
183       rescue PictureIO::Error
184         res = false
185         raise ActiveRecord::Rollback
186       end
187       res = self.destroy
188       raise ActiveRecord::Rollback unless res
189     end
190     res
191   end
192   
193   def self.visible_count
194     ResourcePicture.count
195   end
196   
197   def picture_data
198     Base64.encode64(self.restore 'full')
199   end
200   
201   def credit_template
202     "#{self.license_group_module_name.tableize}/attributes/credit"
203   end
204   
205   def full_credit_template
206     "#{self.license_group_module_name.tableize}/attributes/full_credit"
207   end
208   
209 end