OSDN Git Service

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