OSDN Git Service

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