OSDN Git Service

test speech balloon extended
[pettanr/pettanr.git] / app / models / picture.rb
1 #実素材
2 class Picture < Peta::Content
3   load_manifest
4   belongs_to :original_picture
5   belongs_to :license
6   belongs_to :artist
7   has_one :resource_picture
8   
9   validates :original_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
10   validates :revision, :presence => true, :numericality => true
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 :license_id, :presence => true, :numericality => true, :existence => {:both => false}
17   validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
18   validates :artist_name, :presence => true
19   validates :license_group_classname, :presence => true, :length => {:maximum => 50}
20   
21   def supply_default
22   end
23   
24   def overwrite rp
25     attr = {:width => rp.width, :height => rp.height, :ext => rp.ext, :filesize => rp.filesize, 
26       :original_picture_id => rp.original_picture_id, :license_id => rp.license_id, :artist_id => rp.artist_id, 
27       :md5 => rp.md5, :artist_name => rp.artist_name, 
28       :license_group_classname => rp.license_group_classname, 
29       :license_group_settings => rp.license_group_settings, 
30       :credit_picture_settings => rp.credit_picture_settings,
31       :license_settings => rp.license_settings
32     }
33     self.attributes = attr
34     self.revision = self.new_revision   #Do not move to attr. new_revision reffernces self.original_picture_id
35   end
36   
37   def visible? operators
38     return true
39   end
40   
41   def showable? operators = nil
42     return false unless self.original_picture
43     return true if self.own?(operators)
44     self.enable? and self.head?
45   end
46   
47   def filename
48     "#{self.id}.#{self.ext}"
49   end
50   
51   def gifname
52     "#{self.id}.gif"
53   end
54   
55   def mime_type
56     "image/#{self.ext}"
57   end
58   
59   def url
60     '/pictures/' + filename
61   end
62   
63   def opt_img_tag
64     {:src => self.url, :width => self.width, :height => self.height}
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 tail_opt_img_tag img
73     {:src => img, :width => self.width, :height => self.height}
74   end
75   
76   def tail_tmb_opt_img_tag img
77     tw, th = PettanImager.thumbnail_size(self.width, self.height)
78     {:src => img, :width => tw, :height => th}
79   end
80   
81   def symbol_option
82     self.tmb_opt_img_tag
83   end
84   
85   def new_revision
86     Picture.maximum(:revision, :conditions => ['original_picture_id = ?', self.original_picture_id]).to_i + 1
87   end
88   
89   def enable?
90     r = self.head.resource_picture
91     r ? true : false
92   end
93   
94   def self.head opid
95     Picture.find(:first, :conditions => ['original_picture_id = ?', opid], :order => 'pictures.revision desc')
96   end
97   
98   def head
99     Picture.head(self.original_picture_id)
100   end
101   
102   def head?
103     self == head
104   end
105   
106   def to_gif?
107     self.ext == 'png' and self.license_extend.gif_convert >= 0
108   end
109   
110   def subdirs
111     self.license_extend.reverse < 0 ? [''] : ['', 'v', 'h', 'vh']
112   end
113   
114   def self.find_by_md5 md5
115     r = Picture.find :all, :conditions => ['pictures.md5 = ?', md5], :order => 'pictures.updated_at desc'
116   end
117   
118   def self.list_by_md5 md5, opid = nil
119     cond = if opid.blank?
120       ['pictures.md5 = ?', md5]
121     else
122       ['pictures.md5 = ? and pictures.original_picture_id <> ?', md5, opid]
123     end
124     r = Picture.find :all, :conditions => cond, :order => 'pictures.updated_at desc'
125   end
126   
127   def self.exist_by_md5 md5, opid
128     Picture.list_by_md5(md5, opid).empty? ? false : true
129   end
130   
131   def self.list_by_original_picture_where original_picture_id
132     ['pictures.original_picture_id = ?', original_picture_id]
133   end
134   
135   def self.list_by_original_picture original_picture_id, roles, page = 1, page_size = self.default_page_size
136     self.where(self.list_by_original_picture_where(original_picture_id)).includes(self.list_opt).order('pictures.revision desc').offset((page -1) * page_size).limit(page_size)
137   end
138   
139   def self.list_opt
140     {:license => {}, :artist => {}}
141   end
142   
143   def self.list_json_opt
144     {:include => {:license => {}, :artist => {}} }
145   end
146   
147   def store(imager)
148     res = false
149     if res = self.save
150       if res = self.store_picture(imager, self.filename)
151         if self.to_gif?
152           if gifimager = imager.to_gif
153             res = self.store_picture(gifimager, self.gifname)
154           else
155             res = false
156           end
157         end
158       end
159     end
160     res
161   end
162   
163   def store_picture(imager, fn)
164     res = false
165     subdirs.each do |d|
166       picdata = d.empty? ? imager.binary : imager.__send__(d)
167       res = PictureIO.picture_io.put(picdata, fn, d)
168       break unless res
169     end
170     res
171   end
172   
173   def restore(subdir = nil)
174     PictureIO.picture_io.get self.filename, subdir
175   end
176   
177   def self.export(dt = nil)
178     opt = {}
179     cond = if dt
180       ['artists.author_id is not null and pictures.updated_at >= ?', dt]
181     else
182       'artists.author_id is not null'
183     end
184     opt.merge!({:conditions => cond}) 
185     opt.merge!({:include => {:artist => {}}, :order => 'pictures.updated_at desc'})
186     Picture.find(:all, opt)
187   end
188   
189   def self.list_as_json_text ary
190     '[' + ary.map {|i| i.to_json_with_picture_data }.join(',') + ']'
191   end
192   
193   def picture_data
194     Base64.encode64(self.restore)
195   end
196   
197   def to_json_with_picture_data
198     self.to_json({:methods => :picture_data})
199   end
200   
201   def unpublish
202     imager = PettanImager.load(File.open(Rails.root + 'app/assets/images/error.png', 'rb').read)
203     return false unless imager
204     self.store imager
205   end
206   
207   def credit_template
208     "#{self.license_group_classname.tableize}/attributes/credit"
209   end
210   
211   def full_credit_template
212     "#{self.license_group_classname.tableize}/attributes/full_credit"
213   end
214   
215 end