1 class OriginalPicture < Peta::Content
4 belongs_to :original_picture_license_group
5 has_one :resource_picture
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}
19 def overwrite operators
20 self.uploaded_at = Time.now
21 return unless operators.artist
22 self.artist_id = operators.artist.id
25 def visible? operators
29 "#{self.id}.#{self.ext}"
37 '/original_pictures/' + filename
41 {:src => self.url, :width => self.width, :height => self.height}
45 tw, th = PettanImager.thumbnail_size(self.width, self.height)
46 {:src => self.url, :width => tw, :height => th}
54 head = self.history.first
55 head ? head.revision : 'unpublished'
59 self.published_at == nil and self.stopped_at == nil
63 self.stopped_at != nil
67 dt = self.published_at || self.stopped_at
68 return false unless dt
73 self.published_at != nil
77 Picture.find(:all, {:conditions => ['pictures.original_picture_id = ?', self.id], :order => 'pictures.revision desc'} )
81 {:include => {:resource_picture => {}, :pictures => {}}}
86 self.errors.add :base, I18n.t('errors.invalid_image')
90 self.attributes = {:ext => imager.ext, :width => imager.width, :height => imager.height, :filesize => imager.filesize, :md5 => imager.md5}
91 OriginalPicture.transaction do
94 res = PictureIO.original_picture_io.put(imager.binary, self.filename)
95 rescue PictureIO::Error
97 self.errors.add :base, I18n.t('picture_io.error')
98 raise ActiveRecord::Rollback
105 def restore(subdir = nil)
106 PictureIO.original_picture_io.get self.filename, subdir
109 def self.export(dt = nil)
112 ['artists.author_id is not null and original_pictures.updated_at >= ?', dt]
114 'artists.author_id is not null'
116 opt.merge!({:conditions => cond})
117 opt.merge!({:include => {:resource_picture => {}, :artist => {}}, :order => 'original_pictures.id'})
118 OriginalPicture.find(:all, opt)
121 def list_as_json_with_resource_picture
122 self.to_json({:include => {:resource_picture => {:methods => :picture_data}}})
125 def self.list_as_json_text ary
126 '[' + ary.map {|i| i.list_as_json_with_resource_picture }.join(',') + ']'
129 def destroy_with_resource_picture
131 OriginalPicture.transaction do
133 PictureIO.original_picture_io.delete(self.filename) if PictureIO.original_picture_io.exist?(self.filename)
134 rescue PictureIO::Error
136 raise ActiveRecord::Rollback
138 if self.resource_picture
139 res = self.resource_picture.unpublish
140 raise ActiveRecord::Rollback unless res
142 self.pictures.each do |picture|
143 res = picture.unpublish
144 raise ActiveRecord::Rollback unless res
147 raise ActiveRecord::Rollback unless res
152 def self.publish oid, lsname, attr
153 l = License.find_by_name lsname
154 op = OriginalPicture.find oid
156 attr[:license_id] = l.id
158 ctl = lg.module_name.pluralize + '::Attribute'
159 le = ctl.constantize.new attr
161 rp = ResourcePicture.new
162 rp.attributes = le.resource_picture_attributes op
165 imager = PettanImager.load op.restore
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
181 def self.auto_publish dirname, auth
182 Dir.glob File.expand_path(dirname) + '/*' do |filename|
183 if File.directory?(filename)
187 Dir.glob(filename + '/*') do |fn|
188 ext = File.extname(fn).downcase
191 json = JSON.parse(File.open(fn).read)
192 lsname = json["license_name"]
193 attr = json["attributes"]
194 when '.png', '.gif', '.jpeg'
198 oid = OriginalPicture.upload img, auth
199 OriginalPicture.publish oid, lsname, attr