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|
# 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}")
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
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