OSDN Git Service

manifest list mg
[pettanr/pettanr.git] / app / models / resource_picture.rb
1 #素材
2 class ResourcePicture < Peta::Content
3   belongs_to :artist
4   belongs_to :license
5   belongs_to :picture
6   belongs_to :original_picture
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   validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
14   validates :license_id, :presence => true, :numericality => true, :existence => {:both => false}
15   validates :original_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
16   validates :artist_name, :presence => true
17   validates :classname, :presence => true, :length => {:maximum => 50}
18   validates :picture_id, :presence => true, :numericality => true, :existence => {:both => false}
19   
20   def self.owner_type
21     :artist
22   end
23   
24   def self.valid_encode_columns
25     super + ['artist_name', 'classname', 'credit', 'settings']
26   end
27   
28   def supply_default
29   end
30   
31   def overwrite op
32     attr = {:width => op.width, :height => op.height, :ext => op.ext, :filesize => op.filesize, 
33       :original_picture_id => op.id, :artist_id => op.artist_id, :md5 => op.md5
34     }
35     self.attributes = attr
36   end
37   
38   def visible? operators
39     return false unless super
40     true
41   end
42   
43   def filename
44     "#{self.id}.#{self.ext}"
45   end
46   
47   def gifname
48     "#{self.id}.gif"
49   end
50   
51   def mime_type
52     "image/#{self.ext}"
53   end
54   
55   def url subdir = nil
56     '/resource_pictures/' + (subdir.to_s.empty? ? '' : subdir.to_s + '/' ) + filename
57   end
58   
59   def to_gif?
60     self.ext == 'png' and self.flag_gif_convert >= 0
61   end
62   
63   def thumbnail(imager)
64     tw, th = ResourcePicture.fix_size_both(MagicNumber['thumbnail_width'], MagicNumber['thumbnail_height'], rimg.columns, rimg.rows)
65     ResourcePicture.resize(rimg.to_blob, tw, th).to_blob
66   end
67   
68   def tmb_opt_img_tag
69     tw, th = PettanImager.thumbnail_size(self.width, self.height)
70     {:src => self.url, :width => tw, :height => th}
71   end
72   
73   def opt_img_tag
74     {:src => self.url('full'), :width => self.width, :height => self.height}
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     r = pc.store imager
110     return pc if r
111     self.errors.add :base, Picture.model_name.human + I18n.t('errors.not_create')
112     false
113   end
114   
115   def store imager
116     return false unless imager
117     res = false
118     self.overwrite self.original_picture
119     ResourcePicture.transaction do
120       self.original_picture.published_at = Time.now
121       self.original_picture.stopped_at = nil
122       raise ActiveRecord::Rollback unless self.original_picture.save
123       pc = self.new_picture imager
124       if pc
125         self.picture_id = pc.id
126         if res = self.save
127           res = self.store_picture_with_gif(imager)
128         end
129       else
130       end
131       raise ActiveRecord::Rollback unless res
132     end
133     res
134   end
135   
136   def store_picture_with_gif(imager)
137     if res = self.store_picture(imager, self.filename)
138       if self.to_gif?
139         if gifimager = imager.to_gif
140           if res = self.store_picture(gifimager, self.gifname)
141           else
142             self.errors.add :base, I18n.t('picture_io.error')
143           end
144         else
145           self.errors.add :base, I18n.t('errors.not_convert')
146           res = false
147         end
148       end
149     else
150       self.errors.add :base, I18n.t('picture_io.error')
151     end
152     res
153   end
154   
155   def store_picture(imager, fn)
156     res = false
157     thumbnail_imager = self.flag_thumbnail >= 0 ? imager.to_thumbnail : imager
158     return false unless thumbnail_imager
159     begin
160       PictureIO.resource_picture_io.put(thumbnail_imager.binary, fn)
161       PictureIO.resource_picture_io.put(imager.binary, fn, 'full')
162       res = true
163     rescue PictureIO::Error
164       res = false
165     end
166     res
167   end
168   
169   def restore(subdir = nil)
170     PictureIO.resource_picture_io.get self.filename, subdir
171   end
172   
173   def unpublish
174     res = false
175     ResourcePicture.transaction do
176       self.original_picture.published_at = nil
177       self.original_picture.stopped_at = Time.now
178       raise ActiveRecord::Rollback unless self.original_picture.save
179       begin
180         PictureIO.resource_picture_io.delete(self.filename) if PictureIO.resource_picture_io.exist?(self.filename)
181         PictureIO.resource_picture_io.delete(self.filename, 'full') if PictureIO.resource_picture_io.exist?(self.filename, 'full')
182       rescue PictureIO::Error
183         res = false
184         raise ActiveRecord::Rollback
185       end
186       res = self.destroy
187       raise ActiveRecord::Rollback unless res
188     end
189     res
190   end
191   
192   def self.visible_count
193     ResourcePicture.count
194   end
195   
196   def picture_data
197     Base64.encode64(self.restore 'full')
198   end
199   
200   def credit_template
201     "#{self.classname.tableize}/attributes/credit"
202   end
203   
204   def full_credit_template
205     "#{self.classname.tableize}/attributes/full_credit"
206   end
207   
208   def credit_data
209     begin
210       @credit_data = JSON.parse(self.credit) unless @credit_data
211     rescue 
212     end
213     @credit_data
214   end
215   
216   def flags
217     begin
218       @flags = JSON.parse(self.settings) unless @flags
219     rescue 
220     end
221     @flags
222   end
223   
224   def flags=(s)
225     @flags = s
226   end
227   
228   def flag_open
229     @flag_open = flags["open"] unless @flag_open
230     @flag_open
231   end
232   
233   def flag_commercial
234     @flag_commercial = flags["commercial"] unless @flag_commercial
235     @flag_commercial
236   end
237   
238   def flag_official
239     @flag_official = flags["official"] unless @flag_official
240     @flag_official
241   end
242   
243   def flag_attribution
244     @flag_attribution = flags["attribution"] unless @flag_attribution
245     @flag_attribution
246   end
247   
248   def flag_derive
249     @flag_derive = flags["derive"] unless @flag_derive
250     @flag_derive
251   end
252   
253   def flag_thumbnail
254     @flag_thumbnail = flags["thumbnail"] unless @flag_thumbnail
255     @flag_thumbnail
256   end
257   
258   def flag_gif_convert
259     @flag_gif_convert = flags["gif_convert"] unless @flag_gif_convert
260     @flag_gif_convert
261   end
262   
263   def flag_reverse
264     @flag_reverse = flags["reverse"] unless @flag_reverse
265     @flag_reverse
266   end
267   
268   def flag_resize
269     @flag_resize = flags["resize"] unless @flag_resize
270     @flag_resize
271   end
272   
273   def flag_sync_vh
274     @flag_sync_vh = flags["sync_vh"] unless @flag_sync_vh
275     @flag_sync_vh
276   end
277   
278   def flag_overlap
279     @flag_overlap = flags["overlap"] unless @flag_overlap
280     @flag_overlap
281   end
282   
283 end