OSDN Git Service

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