OSDN Git Service

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