OSDN Git Service

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