OSDN Git Service

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