OSDN Git Service

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