Refactor the GPX upload to try and avoid the import daemon loading traces

which do not exist yet.
This commit is contained in:
Tom Hughes 2009-03-20 00:08:53 +00:00
parent 994104e92e
commit eff9fa3b00

View file

@ -293,20 +293,37 @@ class TraceController < ApplicationController
private private
def do_create(file, tags, description, public) def do_create(file, tags, description, public)
# Sanitise the user's filename
name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, '_') name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, '_')
# Get a temporary filename...
filename = "/tmp/#{rand}" filename = "/tmp/#{rand}"
# ...and save the uploaded file to that location
File.open(filename, "w") { |f| f.write(file.read) } File.open(filename, "w") { |f| f.write(file.read) }
@trace = Trace.new({:name => name, :tagstring => tags, # Create the trace object, falsely marked as already
:description => description, :public => public}) # inserted to stop the import daemon trying to load it
@trace.inserted = false @trace = Trace.new({
@trace.user = @user :name => name,
@trace.timestamp = Time.now :tagstring => tags,
:description => description,
:public => public,
:inserted => true,
:user => @user,
:timestamp => Time.now
})
# Save the trace object
if @trace.save if @trace.save
# Rename the temporary file to the final name
FileUtils.mv(filename, @trace.trace_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 else
# Remove the file as we have failed to update the database
FileUtils.rm_f(filename) FileUtils.rm_f(filename)
end end
end end