OSDN Git Service

t#30339:test
[pettanr/pettanr.git] / app / models / resource_picture.rb
1 #素材
2 class ResourcePicture < ActiveRecord::Base
3   belongs_to :artist
4   belongs_to :license
5   belongs_to :picture
6   has_many :panel_pictures
7   belongs_to :original_picture
8   
9   validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
10   validates :width, :presence => true, :numericality => true, :natural_number => true
11   validates :height, :presence => true, :numericality => true, :natural_number => true
12   validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
13   validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
14   validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
15   validates :license_id, :presence => true, :numericality => true, :existence => {:both => false}
16   validates :original_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
17   validates :artist_name, :presence => true
18   validates :classname, :presence => true, :length => {:maximum => 50}
19   validates :picture_id, :presence => true, :numericality => true, :existence => {:both => false}
20   
21   def supply_default
22   end
23   
24   def overwrite op
25     attr = {:width => op.width, :height => op.height, :ext => op.ext, :filesize => op.filesize, 
26       :original_picture_id => op.id, :artist_id => op.artist_id, :md5 => op.md5
27     }
28     self.attributes = attr
29   end
30   
31   def own? ar
32     return false unless ar.is_a?(Artist)
33     ar.id == self.artist_id
34   end
35   
36   def visible? au
37     if au == nil
38       return false if MagicNumber['run_mode'] == 1
39     elsif au.is_a?(Author)
40       return true
41     else
42       return false
43     end
44     true
45   end
46   
47   def filename
48     "#{self.id}.#{self.ext}"
49   end
50   
51   def gifname
52     "#{self.id}.gif"
53   end
54   
55   def mime_type
56     "image/#{self.ext}"
57   end
58   
59   def url subdir = nil
60     '/resource_pictures/' + (subdir.to_s.empty? ? '' : subdir.to_s + '/' ) + filename
61   end
62   
63   def to_gif?
64     self.ext == 'png' and self.flag_gif_convert >= 0
65   end
66   
67   def thumbnail(imager)
68     tw, th = ResourcePicture.fix_size_both(MagicNumber['thumbnail_width'], MagicNumber['thumbnail_height'], rimg.columns, rimg.rows)
69     ResourcePicture.resize(rimg.to_blob, tw, th).to_blob
70   end
71   
72   def tmb_opt_img_tag
73     tw, th = PettanImager.thumbnail_size(self.width, self.height)
74     {:src => self.url, :width => tw, :height => th}
75   end
76   
77   def opt_img_tag
78     {:src => self.url('full'), :width => self.width, :height => self.height}
79   end
80   
81   def self.default_page_size
82     25
83   end
84   
85   def self.max_page_size
86     100
87   end
88   
89   def self.page prm = nil
90     page = prm.to_i
91     page = 1 if page < 1
92     page
93   end
94   
95   def self.page_size prm = self.default_page_size
96     page_size = prm.to_i
97     page_size = self.max_page_size if page_size > self.max_page_size
98     page_size = self.default_page_size if page_size < 1
99     page_size
100   end
101   
102   def self.list page = 1, page_size = self.default_page_size
103     opt = {}
104     opt.merge!(self.list_opt)
105     opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
106     opt.merge!({:order => 'updated_at desc'})
107     ResourcePicture.find(:all, opt)
108   end
109   
110   def self.list_opt
111     {:include => {:license => {}, :artist => {}, :picture => {}} }
112   end
113   
114   def self.list_json_opt
115     {:include => {:license => {}, :artist => {}, :picture => {}} }
116   end
117   
118   def self.mylist ar, page = 1, page_size = Author.default_resource_picture_page_size
119     opt = {}
120     opt.merge!(ResourcePicture.list_opt)
121     opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
122     opt.merge!({:conditions => ['resource_pictures.artist_id = ?', ar.id], :order => 'resource_pictures.updated_at desc'})
123     ResourcePicture.find(:all, opt)
124   end
125   
126   def self.show rid, au
127     opt = {}
128     opt.merge!(self.show_opt)
129     r = ResourcePicture.find(rid, opt)
130     raise ActiveRecord::Forbidden unless r.visible?(au)
131     r
132   end
133   
134   def self.show_opt
135     {:include => {:license => {}, :artist => {}, :picture => {}} }
136   end
137   
138   def self.show_json_opt
139     {:include => {:license => {}, :artist => {}, :picture => {}} }
140   end
141   
142   def self.edit rid, ar
143     opt = {}
144     opt.merge!(self.show_opt)
145     r = ResourcePicture.find(rid, opt)
146     raise ActiveRecord::Forbidden unless r.own?(ar)
147     r
148   end
149   
150   def new_picture imager
151     pc = Picture.new
152     pc.supply_default
153     pc.overwrite self
154     r = pc.store imager
155     return pc if r
156     self.errors.add :base, Picture.model_name.human + I18n.t('errors.not_create')
157     false
158   end
159   
160   def store imager
161     return false unless imager
162     res = false
163     self.overwrite self.original_picture
164     ResourcePicture.transaction do
165       self.original_picture.published_at = Time.now
166       self.original_picture.stopped_at = nil
167       raise ActiveRecord::Rollback unless self.original_picture.save
168       pc = self.new_picture imager
169       if pc
170         self.picture_id = pc.id
171         if res = self.save
172           res = self.store_picture_with_gif(imager)
173         end
174       else
175       end
176       raise ActiveRecord::Rollback unless res
177     end
178     res
179   end
180   
181   def store_picture_with_gif(imager)
182     if res = self.store_picture(imager, self.filename)
183       if self.to_gif?
184         if gifimager = imager.to_gif
185           if res = self.store_picture(gifimager, self.gifname)
186           else
187             self.errors.add :base, I18n.t('picture_io.error')
188           end
189         else
190           self.errors.add :base, I18n.t('errors.not_convert')
191           res = false
192         end
193       end
194     else
195       self.errors.add :base, I18n.t('picture_io.error')
196     end
197     res
198   end
199   
200   def store_picture(imager, fn)
201     res = false
202     thumbnail_imager = self.flag_thumbnail >= 0 ? imager.to_thumbnail : imager
203     return false unless thumbnail_imager
204     begin
205       PictureIO.resource_picture_io.put(thumbnail_imager.binary, fn)
206       PictureIO.resource_picture_io.put(imager.binary, fn, 'full')
207       res = true
208     rescue PictureIO::Error
209       res = false
210     end
211     res
212   end
213   
214   def restore(subdir = nil)
215     PictureIO.resource_picture_io.get self.filename, subdir
216   end
217   
218   def unpublish
219     res = false
220     ResourcePicture.transaction do
221       self.original_picture.published_at = nil
222       self.original_picture.stopped_at = Time.now
223       raise ActiveRecord::Rollback unless self.original_picture.save
224       begin
225         PictureIO.resource_picture_io.delete(self.filename) if PictureIO.resource_picture_io.exist?(self.filename)
226         PictureIO.resource_picture_io.delete(self.filename, 'full') if PictureIO.resource_picture_io.exist?(self.filename, 'full')
227       rescue PictureIO::Error
228         res = false
229         raise ActiveRecord::Rollback
230       end
231       res = self.destroy
232       raise ActiveRecord::Rollback unless res
233     end
234     res
235   end
236   
237   def self.visible_count
238     ResourcePicture.count
239   end
240   
241   def picture_data
242     Base64.encode64(self.restore 'full')
243   end
244   
245   def credit_template
246     "#{self.classname.tableize}/attributes/credit"
247   end
248   
249   def full_credit_template
250     "#{self.classname.tableize}/attributes/full_credit"
251   end
252   
253   def credit_data
254     begin
255       @credit_data = JSON.parse(self.credit) unless @credit_data
256     rescue 
257     end
258     @credit_data
259   end
260   
261   def flags
262     begin
263       @flags = JSON.parse(self.settings) unless @flags
264     rescue 
265     end
266     @flags
267   end
268   
269   def flags=(s)
270     @flags = s
271   end
272   
273   def flag_open
274     @flag_open = flags["open"] unless @flag_open
275     @flag_open
276   end
277   
278   def flag_commercial
279     @flag_commercial = flags["commercial"] unless @flag_commercial
280     @flag_commercial
281   end
282   
283   def flag_official
284     @flag_official = flags["official"] unless @flag_official
285     @flag_official
286   end
287   
288   def flag_attribution
289     @flag_attribution = flags["attribution"] unless @flag_attribution
290     @flag_attribution
291   end
292   
293   def flag_derive
294     @flag_derive = flags["derive"] unless @flag_derive
295     @flag_derive
296   end
297   
298   def flag_thumbnail
299     @flag_thumbnail = flags["thumbnail"] unless @flag_thumbnail
300     @flag_thumbnail
301   end
302   
303   def flag_gif_convert
304     @flag_gif_convert = flags["gif_convert"] unless @flag_gif_convert
305     @flag_gif_convert
306   end
307   
308   def flag_reverse
309     @flag_reverse = flags["reverse"] unless @flag_reverse
310     @flag_reverse
311   end
312   
313   def flag_resize
314     @flag_resize = flags["resize"] unless @flag_resize
315     @flag_resize
316   end
317   
318   def flag_sync_vh
319     @flag_sync_vh = flags["sync_vh"] unless @flag_sync_vh
320     @flag_sync_vh
321   end
322   
323   def flag_overlap
324     @flag_overlap = flags["overlap"] unless @flag_overlap
325     @flag_overlap
326   end
327   
328 end