X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/a0e8c35c1b3bfba11a755d9f8e282fc0773d953f..c11d961f624cb610c4b8184d24e522b2c093d1ea:/app/models/trace.rb?ds=inline diff --git a/app/models/trace.rb b/app/models/trace.rb index 0307b5f65..f2dcf9ad9 100644 --- a/app/models/trace.rb +++ b/app/models/trace.rb @@ -1,13 +1,25 @@ class Trace < ActiveRecord::Base set_table_name 'gpx_files' - validates_presence_of :user_id, :name, :public, :description, :timestamp + validates_presence_of :user_id, :name, :timestamp + validates_presence_of :description, :on => :create # validates_numericality_of :latitude, :longitude - validates_inclusion_of :inserted, :in => [ true, false] + validates_inclusion_of :public, :inserted, :in => [ true, false] belongs_to :user - has_many :tags, :class_name => 'Tracetag', :foreign_key => 'gpx_id', :dependent => :destroy - has_many :points, :class_name => 'Tracepoint', :foreign_key => 'gpx_id', :dependent => :destroy + has_many :tags, :class_name => 'Tracetag', :foreign_key => 'gpx_id', :dependent => :delete_all + has_many :points, :class_name => 'Tracepoint', :foreign_key => 'gpx_id', :dependent => :delete_all + + def destroy + super + FileUtils.rm_f(trace_name) + FileUtils.rm_f(icon_picture_name) + FileUtils.rm_f(large_picture_name) + end + + def tagstring + return tags.collect {|tt| tt.tag}.join(" ") + end def tagstring=(s) self.tags = s.split().collect {|tag| @@ -80,11 +92,15 @@ class Trace < ActiveRecord::Base # TODO *nix specific, could do to work on windows... would be functionally inferior though - check for '.gz' filetype = `file -b #{trace_name}`.chomp gzipped = filetype =~ /^gzip/ + bzipped = filetype =~ /^bzip2/ zipped = filetype =~ /^Zip/ if gzipped filename = tempfile = "/tmp/#{rand}" system("gunzip -c #{trace_name} > #{filename}") + elsif bzipped + filename = tempfile = "/tmp/#{rand}" + system("bunzip2 -c #{trace_name} > #{filename}") elsif zipped filename = tempfile = "/tmp/#{rand}" system("unzip -p #{trace_name} > #{filename}") @@ -110,6 +126,7 @@ class Trace < ActiveRecord::Base tp.lat = point['latitude'].to_f tp.lng = point['longitude'].to_f tp.altitude = point['altitude'].to_f + tp.timestamp = point['timestamp'] tp.user_id = user.id tp.gpx_id = id tp.trackid = point['segment'].to_i @@ -134,21 +151,11 @@ class Trace < ActiveRecord::Base self.size = gpx.actual_points self.inserted = true self.save - - Notifier::deliver_gpx_success(self, gpx.possible_points) - else - FileUtils.rm_f("/home/osm/gpx/#{id}.gpx") - self.destroy - Notifier::deliver_gpx_failure(self, '0 points parsed ok. Do they all have lat,lng,alt,timestamp?') end logger.info "done trace #{id}" - rescue Exception => ex - logger.info ex - ex.backtrace.each {|l| logger.info l } - FileUtils.rm_f("/home/osm/gpx/#{id}.gpx") - self.destroy - Notifier::deliver_gpx_failure(self, ex.to_s + ex.backtrace.join("\n") ) + + return gpx ensure FileUtils.rm_f(tempfile) if tempfile end