OSDN Git Service

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