OSDN Git Service

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