OSDN Git Service

fix anything
[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 :picture
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 visible? operators
32     return false unless super
33     true
34   end
35   
36   def filename
37     "#{self.id}.#{self.ext}"
38   end
39   
40   def gifname
41     "#{self.id}.gif"
42   end
43   
44   def mime_type
45     "image/#{self.ext}"
46   end
47   
48   def url subdir = nil
49     '/resource_pictures/' + (subdir.to_s.empty? ? '' : subdir.to_s + '/' ) + filename
50   end
51   
52   def to_gif?
53     self.ext == 'png' and self.flag_gif_convert >= 0
54   end
55   
56   def thumbnail(imager)
57     tw, th = ResourcePicture.fix_size_both(Manifest.manifest.magic_numbers['thumbnail_width'], Manifest.manifest.magic_numbers['thumbnail_height'], rimg.columns, rimg.rows)
58     ResourcePicture.resize(rimg.to_blob, tw, th).to_blob
59   end
60   
61   def tmb_opt_img_tag
62     tw, th = PettanImager.thumbnail_size(self.width, self.height)
63     {:src => self.url, :width => tw, :height => th}
64   end
65   
66   def opt_img_tag
67     {:src => self.url('full'), :width => self.width, :height => self.height}
68   end
69   
70   def symbol_option
71     self.tmb_opt_img_tag
72   end
73   
74   def self.list_order
75     'resource_pictures.updated_at desc'
76   end
77   
78   def self.list_where
79     ''
80   end
81   
82   def self.list_opt
83     {:license => {}, :artist => {}, :picture => {} }
84   end
85   
86   def self.list_json_opt
87     {:include => {:license => {}, :artist => {}, :picture => {}} }
88   end
89   
90   def self.show_opt
91     {:include => {:license => {}, :artist => {}, :picture => {}} }
92   end
93   
94   def self.show_json_opt
95     {:include => {:license => {}, :artist => {}, :picture => {}} }
96   end
97   
98   def new_picture imager
99     pc = Picture.new
100     pc.supply_default
101     pc.overwrite self
102     r = pc.store imager
103     return pc if r
104     self.errors.add :base, Picture.model_name.human + I18n.t('errors.not_create')
105     false
106   end
107   
108   def store imager
109     return false unless imager
110     res = false
111     self.overwrite self.original_picture
112     ResourcePicture.transaction do
113       self.original_picture.published_at = Time.now
114       self.original_picture.stopped_at = nil
115       raise ActiveRecord::Rollback unless self.original_picture.save
116       pc = self.new_picture imager
117       if pc
118         self.picture_id = pc.id
119         if res = self.save
120           res = self.store_picture_with_gif(imager)
121         end
122       else
123       end
124       raise ActiveRecord::Rollback unless res
125     end
126     res
127   end
128   
129   def store_picture_with_gif(imager)
130     if res = self.store_picture(imager, self.filename)
131       if self.to_gif?
132         if gifimager = imager.to_gif
133           if res = self.store_picture(gifimager, self.gifname)
134           else
135             self.errors.add :base, I18n.t('picture_io.error')
136           end
137         else
138           self.errors.add :base, I18n.t('errors.not_convert')
139           res = false
140         end
141       end
142     else
143       self.errors.add :base, I18n.t('picture_io.error')
144     end
145     res
146   end
147   
148   def store_picture(imager, fn)
149     res = false
150     thumbnail_imager = self.flag_thumbnail >= 0 ? imager.to_thumbnail : imager
151     return false unless thumbnail_imager
152     begin
153       PictureIO.resource_picture_io.put(thumbnail_imager.binary, fn)
154       PictureIO.resource_picture_io.put(imager.binary, fn, 'full')
155       res = true
156     rescue PictureIO::Error
157       res = false
158     end
159     res
160   end
161   
162   def restore(subdir = nil)
163     PictureIO.resource_picture_io.get self.filename, subdir
164   end
165   
166   def unpublish
167     res = false
168     ResourcePicture.transaction do
169       self.original_picture.published_at = nil
170       self.original_picture.stopped_at = Time.now
171       raise ActiveRecord::Rollback unless self.original_picture.save
172       begin
173         PictureIO.resource_picture_io.delete(self.filename) if PictureIO.resource_picture_io.exist?(self.filename)
174         PictureIO.resource_picture_io.delete(self.filename, 'full') if PictureIO.resource_picture_io.exist?(self.filename, 'full')
175       rescue PictureIO::Error
176         res = false
177         raise ActiveRecord::Rollback
178       end
179       res = self.destroy
180       raise ActiveRecord::Rollback unless res
181     end
182     res
183   end
184   
185   def self.visible_count
186     ResourcePicture.count
187   end
188   
189   def picture_data
190     Base64.encode64(self.restore 'full')
191   end
192   
193   def credit_template
194     "#{self.classname.tableize}/attributes/credit"
195   end
196   
197   def full_credit_template
198     "#{self.classname.tableize}/attributes/full_credit"
199   end
200   
201   def credit_data
202     begin
203       @credit_data = JSON.parse(self.credit) unless @credit_data
204     rescue 
205     end
206     @credit_data
207   end
208   
209   def flags
210     begin
211       @flags = JSON.parse(self.settings) unless @flags
212     rescue 
213     end
214     @flags
215   end
216   
217   def flags=(s)
218     @flags = s
219   end
220   
221   def flag_open
222     @flag_open = flags["open"] unless @flag_open
223     @flag_open
224   end
225   
226   def flag_commercial
227     @flag_commercial = flags["commercial"] unless @flag_commercial
228     @flag_commercial
229   end
230   
231   def flag_official
232     @flag_official = flags["official"] unless @flag_official
233     @flag_official
234   end
235   
236   def flag_attribution
237     @flag_attribution = flags["attribution"] unless @flag_attribution
238     @flag_attribution
239   end
240   
241   def flag_derive
242     @flag_derive = flags["derive"] unless @flag_derive
243     @flag_derive
244   end
245   
246   def flag_thumbnail
247     @flag_thumbnail = flags["thumbnail"] unless @flag_thumbnail
248     @flag_thumbnail
249   end
250   
251   def flag_gif_convert
252     @flag_gif_convert = flags["gif_convert"] unless @flag_gif_convert
253     @flag_gif_convert
254   end
255   
256   def flag_reverse
257     @flag_reverse = flags["reverse"] unless @flag_reverse
258     @flag_reverse
259   end
260   
261   def flag_resize
262     @flag_resize = flags["resize"] unless @flag_resize
263     @flag_resize
264   end
265   
266   def flag_sync_vh
267     @flag_sync_vh = flags["sync_vh"] unless @flag_sync_vh
268     @flag_sync_vh
269   end
270   
271   def flag_overlap
272     @flag_overlap = flags["overlap"] unless @flag_overlap
273     @flag_overlap
274   end
275   
276 end