OSDN Git Service

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