OSDN Git Service

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