- @trace = Trace.new({:name => name, :tagstring => tags,
- :description => description, :public => public})
- @trace.inserted = false
- @trace.user = @user
- @trace.timestamp = Time.now
-
- FileUtils.mv(filename, @trace.trace_name)
- unless @trace.save
- FileUtils.rm_f(@trace.trace_name)
+ # Create the trace object, falsely marked as already
+ # inserted to stop the import daemon trying to load it
+ @trace = Trace.new({
+ :name => name,
+ :tagstring => tags,
+ :description => description,
+ :public => public,
+ :inserted => true,
+ :user => @user,
+ :timestamp => Time.now.getutc
+ })
+
+ # Save the trace object
+ if @trace.save
+ # Rename the temporary file to the final name
+ FileUtils.mv(filename, @trace.trace_name)
+
+ # Clear the inserted flag to make the import daemon load the trace
+ @trace.inserted = false
+ @trace.save!
+ else
+ # Remove the file as we have failed to update the database
+ FileUtils.rm_f(filename)
+ end
+
+ # Finally save whether the user marked the trace as being public
+ if @trace.public?
+ if @user.trace_public_default.nil?
+ @user.preferences.create(:k => "gps.trace.public", :v => "default")
+ end
+ else
+ pref = @user.trace_public_default
+ pref.destroy unless pref.nil?