OSDN Git Service

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