OSDN Git Service

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