OSDN Git Service

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