OSDN Git Service

Merge branch 'v05' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v05
[pettanr/pettanr.git] / app / models / original_picture.rb
1 class OriginalPicture < ActiveRecord::Base
2   belongs_to :artist
3   belongs_to :original_picture_license_group
4   has_one :resource_picture
5   has_many :pictures
6   
7   validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
8   validates :width, :presence => true, :numericality => true, :natural_number => true
9   validates :height, :presence => true, :numericality => true, :natural_number => true
10   validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
11   validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
12   validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
13   
14   def supply_default
15   end
16   
17   def overwrite ar
18     self.uploaded_at = Time.now
19     self.artist_id = ar.id
20   end
21   
22   def own? roles
23     roles = [roles] unless roles.respond_to?(:each)
24     ar = OriginalPicture.get_artist_from_roles roles
25     return false unless ar
26     self.artist_id == ar.id
27   end
28   
29   def visible? roles
30     return true if self.admin_role_check roles
31     self.own?(roles)
32   end
33   
34   def filename
35     "#{self.id}.#{self.ext}"
36   end
37   
38   def mime_type
39     "image/#{self.ext}"
40   end
41   
42   def url
43     '/original_pictures/' + filename
44   end
45   
46   def opt_img_tag
47     {:src => self.url, :width => self.width, :height => self.height}
48   end
49   
50   def tmb_opt_img_tag
51     tw, th = PettanImager.thumbnail_size(self.width, self.height)
52     {:src => self.url, :width => tw, :height => th}
53   end
54   
55   def unpublished?
56     self.published_at == nil and self.stopped_at == nil
57   end
58   
59   def stopped?
60     self.stopped_at != nil
61   end
62   
63   def unlicensed?
64     dt = self.published_at || self.stopped_at
65     return false unless dt
66     self.uploaded_at > dt
67   end
68   
69   def published?
70     self.published_at != nil
71   end
72   
73   def self.default_page_size
74     25
75   end
76   
77   def self.max_page_size
78     100
79   end
80   
81   def self.page prm = nil
82     page = prm.to_i
83     page = 1 if page < 1
84     page
85   end
86   
87   def self.page_size prm = self.default_page_size
88     page_size = prm.to_i
89     page_size = self.max_page_size if page_size > self.max_page_size
90     page_size = self.default_page_size if page_size < 1
91     page_size
92   end
93   
94   def self.mylist artist_id, page = 1, page_size = self.default_page_size
95     opt = {}
96     opt.merge!(self.list_opt)
97     opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
98     opt.merge!({:conditions => ['original_pictures.artist_id = ?', artist_id], :order => 'original_pictures.updated_at desc'})
99     OriginalPicture.find(:all, opt)
100   end
101   
102   def history 
103     Picture.find(:all, {:conditions => ['pictures.original_picture_id = ?', self.id], :order => 'pictures.revision desc'} )
104   end
105   
106   def self.list_opt
107     {:include => {:resource_picture => {}, :pictures => {}}}
108   end
109   
110   def self.list_json_opt
111     {:include => {:resource_picture => {}, :pictures => {}}}
112   end
113   
114   def self.show cid, roles
115     opt = {}
116     opt.merge!(self.show_opt)
117     res = OriginalPicture.find(cid, opt)
118     raise ActiveRecord::Forbidden unless res.visible?(roles)
119     res
120   end
121   
122   def self.edit cid, ar
123     opt = {}
124     opt.merge!(self.show_opt)
125     res = OriginalPicture.find(cid, opt)
126     raise ActiveRecord::Forbidden unless res.own?(ar)
127     res
128   end
129   
130   def self.show_opt
131     {:include => {:resource_picture => {}, :pictures => {}}}
132   end
133   
134   def self.show_json_opt
135     {:include => {:resource_picture => {}, :pictures => {}}}
136   end
137   
138   def store(imager)
139     unless imager
140       self.errors.add :base, I18n.t('errors.invalid_image')
141       return false
142     end
143     res = false
144     self.attributes = {:ext => imager.ext, :width => imager.width, :height => imager.height, :filesize => imager.filesize, :md5 => imager.md5}
145     OriginalPicture.transaction do
146       if res = self.save
147         begin
148           res = PictureIO.original_picture_io.put(imager.binary, self.filename)
149         rescue PictureIO::Error
150           res = false
151           self.errors.add :base, I18n.t('picture_io.error')
152           raise ActiveRecord::Rollback
153         end
154       end
155     end
156     res
157   end
158   
159   def restore(subdir = nil)
160     PictureIO.original_picture_io.get self.filename, subdir
161   end
162   
163   def self.export(dt = nil)
164     opt = {}
165     cond = if dt
166       ['artists.author_id is not null and original_pictures.updated_at >= ?', dt]
167     else
168       'artists.author_id is not null'
169     end
170     opt.merge!({:conditions => cond}) 
171     opt.merge!({:include => {:resource_picture => {}, :artist => {}}, :order => 'original_pictures.id'})
172     OriginalPicture.find(:all, opt)
173   end
174   
175   def list_as_json_with_resource_picture
176     self.to_json({:include => {:resource_picture => {:methods => :picture_data}}})
177   end
178   
179   def self.list_as_json_text ary
180     '[' + ary.map {|i| i.list_as_json_with_resource_picture }.join(',') + ']'
181   end
182   
183   def destroy_with_resource_picture
184     res = false
185     OriginalPicture.transaction do
186       begin
187         PictureIO.original_picture_io.delete(self.filename) if PictureIO.original_picture_io.exist?(self.filename)
188       rescue PictureIO::Error
189         res = false
190         raise ActiveRecord::Rollback
191       end
192       if self.resource_picture
193         res = self.resource_picture.unpublish
194         raise ActiveRecord::Rollback unless res
195       end
196       self.pictures.each do |picture|
197         res = picture.unpublish
198         raise ActiveRecord::Rollback unless res
199       end
200       res = self.destroy
201       raise ActiveRecord::Rollback unless res
202     end
203     res
204   end
205   
206   def self.publish oid, lsname, attr
207     l = License.find_by_name lsname
208     op = OriginalPicture.find oid
209     lg = l.license_group
210     attr[:license_id] = l.id
211     
212     ctl = lg.classname.pluralize + '::Attribute'
213     le = ctl.constantize.new attr
214     
215     rp = ResourcePicture.new
216     rp.attributes = le.resource_picture_attributes op
217     rp.overwrite op
218     
219     imager = PettanImager.load op.restore
220     rp.store imager
221   end
222   
223   def self.upload fn, auth
224     b = Base64.encode64(File.open(fn, 'rb').read)
225     u = 'http://localhost:3000/original_pictures'
226     r = RestClient.post(u, 
227       {:original_picture => {:file  => b}, :auth_token => auth}.to_json, 
228       :content_type => :json, :accept => :json
229     )
230     o = JSON.parse r
231     oid = o['id']
232     oid
233   end
234   
235   def self.auto_publish dirname, auth
236     Dir.glob File.expand_path(dirname) + '/*' do |filename|
237       if File.directory?(filename)
238         img = nil
239         lsname = nil
240         attr  = nil
241         Dir.glob(filename + '/*') do |fn|
242           ext = File.extname(fn).downcase
243           case ext
244           when '.json'
245             json = JSON.parse(File.open(fn).read)
246             lsname = json["license_name"]
247             attr = json["attributes"]
248           when '.png', '.gif', '.jpeg'
249             img = fn
250           end
251         end
252         oid = OriginalPicture.upload img, auth
253         OriginalPicture.publish oid, lsname, attr
254       end
255     end
256   end
257   
258 end