OSDN Git Service

fix js filer
[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_order
99     'resource_pictures.updated_at desc'
100   end
101   
102   def self.list_where
103     ''
104   end
105   
106   def self.list_opt
107     {:license => {}, :artist => {}, :picture => {} }
108   end
109   
110   def self.list_json_opt
111     {:include => {:license => {}, :artist => {}, :picture => {}} }
112   end
113   
114   def self.show_opt
115     {:include => {:license => {}, :artist => {}, :picture => {}} }
116   end
117   
118   def self.show_json_opt
119     {:include => {:license => {}, :artist => {}, :picture => {}} }
120   end
121   
122   def new_picture imager
123     pc = Picture.new
124     pc.supply_default
125     pc.overwrite self
126     pc.boosts 'post'
127     r = pc.store imager
128     return pc if r
129     self.errors.add :base, Picture.model_name.human + I18n.t('errors.not_create')
130     false
131   end
132   
133   def store imager
134     return false unless imager
135     res = false
136     self.overwrite self.original_picture
137     ResourcePicture.transaction do
138       self.original_picture.published_at = Time.now
139       self.original_picture.stopped_at = nil
140       raise ActiveRecord::Rollback unless self.original_picture.save
141       pc = self.new_picture imager
142       if pc
143         self.picture_id = pc.id
144         if res = self.save
145           res = self.store_picture_with_gif(imager)
146         end
147       else
148       end
149       raise ActiveRecord::Rollback unless res
150     end
151     res
152   end
153   
154   def store_picture_with_gif(imager)
155     if res = self.store_picture(imager, self.filename)
156       if self.to_gif?
157         if gifimager = imager.to_gif
158           if res = self.store_picture(gifimager, self.gifname)
159           else
160             self.errors.add :base, I18n.t('picture_io.error')
161           end
162         else
163           self.errors.add :base, I18n.t('errors.not_convert')
164           res = false
165         end
166       end
167     else
168       self.errors.add :base, I18n.t('picture_io.error')
169     end
170     res
171   end
172   
173   def store_picture(imager, fn)
174     res = false
175     thumbnail_imager = self.license_extend.thumbnail >= 0 ? imager.to_thumbnail : imager
176     return false unless thumbnail_imager
177     begin
178       PictureIO.resource_picture_io.put(thumbnail_imager.binary, fn)
179       PictureIO.resource_picture_io.put(imager.binary, fn, 'full')
180       res = true
181     rescue PictureIO::Error
182       res = false
183     end
184     res
185   end
186   
187   def restore(subdir = nil)
188     PictureIO.resource_picture_io.get self.filename, subdir
189   end
190   
191   def unpublish
192     res = false
193     ResourcePicture.transaction do
194       self.original_picture.published_at = nil
195       self.original_picture.stopped_at = Time.now
196       raise ActiveRecord::Rollback unless self.original_picture.save
197       begin
198         PictureIO.resource_picture_io.delete(self.filename) if PictureIO.resource_picture_io.exist?(self.filename)
199         PictureIO.resource_picture_io.delete(self.filename, 'full') if PictureIO.resource_picture_io.exist?(self.filename, 'full')
200       rescue PictureIO::Error
201         res = false
202         raise ActiveRecord::Rollback
203       end
204       res = self.destroy
205       raise ActiveRecord::Rollback unless res
206     end
207     res
208   end
209   
210   def self.visible_count
211     ResourcePicture.count
212   end
213   
214   def picture_data
215     Base64.encode64(self.restore 'full')
216   end
217   
218   def credit_template
219     "#{self.license_group_module_name.tableize}/attributes/credit"
220   end
221   
222   def full_credit_template
223     "#{self.license_group_module_name.tableize}/attributes/full_credit"
224   end
225   
226 end