OSDN Git Service

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