OSDN Git Service

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