OSDN Git Service

t#31470:create pager
[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_where ar
95     ['original_pictures.artist_id = ?', ar.id]
96   end
97   
98   def self.mylist ar, page = 1, page_size = self.default_page_size
99     OriginalPicture.where(self.mylist_where(ar)).includes(OriginalPicture.list_opt).order('original_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
100   end
101   
102   def self.mylist_paginate ar, page = 1, page_size = self.default_page_size
103     Kaminari.paginate_array(Array.new(OriginalPicture.where(self.mylist_where(ar)).count, nil)).page(page).per(page_size)
104   end
105   
106   def self.list_opt
107     {:resource_picture => {}, :pictures => {} }
108   end
109   
110   def self.list_json_opt
111     {:include => {:resource_picture => {}, :pictures => {}}}
112   end
113   
114   def history 
115     Picture.find(:all, {:conditions => ['pictures.original_picture_id = ?', self.id], :order => 'pictures.revision desc'} )
116   end
117   
118   def self.show cid, roles
119     opt = {}
120     opt.merge!(self.show_opt)
121     res = OriginalPicture.find(cid, opt)
122     raise ActiveRecord::Forbidden unless res.visible?(roles)
123     res
124   end
125   
126   def self.edit cid, ar
127     opt = {}
128     opt.merge!(self.show_opt)
129     res = OriginalPicture.find(cid, opt)
130     raise ActiveRecord::Forbidden unless res.own?(ar)
131     res
132   end
133   
134   def self.show_opt
135     {:include => {:resource_picture => {}, :pictures => {}}}
136   end
137   
138   def self.show_json_opt
139     {:include => {:resource_picture => {}, :pictures => {}}}
140   end
141   
142   def store(imager)
143     unless imager
144       self.errors.add :base, I18n.t('errors.invalid_image')
145       return false
146     end
147     res = false
148     self.attributes = {:ext => imager.ext, :width => imager.width, :height => imager.height, :filesize => imager.filesize, :md5 => imager.md5}
149     OriginalPicture.transaction do
150       if res = self.save
151         begin
152           res = PictureIO.original_picture_io.put(imager.binary, self.filename)
153         rescue PictureIO::Error
154           res = false
155           self.errors.add :base, I18n.t('picture_io.error')
156           raise ActiveRecord::Rollback
157         end
158       end
159     end
160     res
161   end
162   
163   def restore(subdir = nil)
164     PictureIO.original_picture_io.get self.filename, subdir
165   end
166   
167   def self.export(dt = nil)
168     opt = {}
169     cond = if dt
170       ['artists.author_id is not null and original_pictures.updated_at >= ?', dt]
171     else
172       'artists.author_id is not null'
173     end
174     opt.merge!({:conditions => cond}) 
175     opt.merge!({:include => {:resource_picture => {}, :artist => {}}, :order => 'original_pictures.id'})
176     OriginalPicture.find(:all, opt)
177   end
178   
179   def list_as_json_with_resource_picture
180     self.to_json({:include => {:resource_picture => {:methods => :picture_data}}})
181   end
182   
183   def self.list_as_json_text ary
184     '[' + ary.map {|i| i.list_as_json_with_resource_picture }.join(',') + ']'
185   end
186   
187   def destroy_with_resource_picture
188     res = false
189     OriginalPicture.transaction do
190       begin
191         PictureIO.original_picture_io.delete(self.filename) if PictureIO.original_picture_io.exist?(self.filename)
192       rescue PictureIO::Error
193         res = false
194         raise ActiveRecord::Rollback
195       end
196       if self.resource_picture
197         res = self.resource_picture.unpublish
198         raise ActiveRecord::Rollback unless res
199       end
200       self.pictures.each do |picture|
201         res = picture.unpublish
202         raise ActiveRecord::Rollback unless res
203       end
204       res = self.destroy
205       raise ActiveRecord::Rollback unless res
206     end
207     res
208   end
209   
210   def self.publish oid, lsname, attr
211     l = License.find_by_name lsname
212     op = OriginalPicture.find oid
213     lg = l.license_group
214     attr[:license_id] = l.id
215     
216     ctl = lg.classname.pluralize + '::Attribute'
217     le = ctl.constantize.new attr
218     
219     rp = ResourcePicture.new
220     rp.attributes = le.resource_picture_attributes op
221     rp.overwrite op
222     
223     imager = PettanImager.load op.restore
224     rp.store imager
225   end
226   
227   def self.upload fn, auth
228     b = Base64.encode64(File.open(fn, 'rb').read)
229     u = 'http://localhost:3000/original_pictures'
230     r = RestClient.post(u, 
231       {:original_picture => {:file  => b}, :auth_token => auth}.to_json, 
232       :content_type => :json, :accept => :json
233     )
234     o = JSON.parse r
235     oid = o['id']
236     oid
237   end
238   
239   def self.auto_publish dirname, auth
240     Dir.glob File.expand_path(dirname) + '/*' do |filename|
241       if File.directory?(filename)
242         img = nil
243         lsname = nil
244         attr  = nil
245         Dir.glob(filename + '/*') do |fn|
246           ext = File.extname(fn).downcase
247           case ext
248           when '.json'
249             json = JSON.parse(File.open(fn).read)
250             lsname = json["license_name"]
251             attr = json["attributes"]
252           when '.png', '.gif', '.jpeg'
253             img = fn
254           end
255         end
256         oid = OriginalPicture.upload img, auth
257         OriginalPicture.publish oid, lsname, attr
258       end
259     end
260   end
261   
262 end