OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[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 :artist_name, :presence => true
21   validates :license_group_classname, :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, :artist_name => rp.artist_name, 
31       :license_group_classname => rp.license_group_classname, 
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.maximum(:revision, :conditions => ['original_picture_id = ?', self.original_picture_id]).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.find(:first, :conditions => ['original_picture_id = ?', opid], :order => 'pictures.revision desc')
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.find :all, :conditions => ['pictures.md5 = ?', md5], :order => 'pictures.updated_at desc'
123   end
124   
125   def self.list_by_md5 md5, opid = nil
126     cond = if opid.blank?
127       ['pictures.md5 = ?', md5]
128     else
129       ['pictures.md5 = ? and pictures.original_picture_id <> ?', md5, opid]
130     end
131     r = Picture.find :all, :conditions => cond, :order => 'pictures.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     opt = {}
186     cond = if dt
187       ['artists.author_id is not null and pictures.updated_at >= ?', dt]
188     else
189       'artists.author_id is not null'
190     end
191     opt.merge!({:conditions => cond}) 
192     opt.merge!({:include => {:artist => {}}, :order => 'pictures.updated_at desc'})
193     Picture.find(:all, opt)
194   end
195   
196   def self.list_as_json_text ary
197     '[' + ary.map {|i| i.to_json_with_picture_data }.join(',') + ']'
198   end
199   
200   def picture_data
201     Base64.encode64(self.restore)
202   end
203   
204   def to_json_with_picture_data
205     self.to_json({:methods => :picture_data})
206   end
207   
208   def unpublish
209     imager = PettanImager.load(File.open(Rails.root + 'app/assets/images/error.png', 'rb').read)
210     return false unless imager
211     self.store imager
212   end
213   
214   def credit_template
215     "#{self.license_group_classname.tableize}/attributes/credit"
216   end
217   
218   def full_credit_template
219     "#{self.license_group_classname.tableize}/attributes/full_credit"
220   end
221   
222 end