+ if request.post?
+ logger.info(params[:trace][:gpx_file].class.name)
+
+ if params[:trace][:gpx_file].respond_to?(:read)
+ begin
+ do_create(params[:trace][:gpx_file], params[:trace][:tagstring],
+ params[:trace][:description], params[:trace][:visibility])
+ rescue StandardError => ex
+ logger.debug ex
+ end
+
+ if @trace.id
+ flash[:notice] = t "trace.create.trace_uploaded"
+
+ if current_user.traces.where(:inserted => false).count > 4
+ flash[:warning] = t "trace.trace_header.traces_waiting", :count => current_user.traces.where(:inserted => false).count
+ end
+
+ redirect_to :action => :list, :display_name => current_user.display_name
+ end
+ else
+ @trace = Trace.new(:name => "Dummy",
+ :tagstring => params[:trace][:tagstring],
+ :description => params[:trace][:description],
+ :visibility => params[:trace][:visibility],
+ :inserted => false, :user => current_user,
+ :timestamp => Time.now.getutc)
+ @trace.valid?
+ @trace.errors.add(:gpx_file, "can't be blank")
+ end
+ else
+ @trace = Trace.new(:visibility => default_visibility)
+ end
+
+ @title = t "trace.create.upload_trace"
+ end
+
+ def data
+ trace = Trace.find(params[:id])
+
+ if trace.visible? && (trace.public? || (current_user && current_user == trace.user))
+ if Acl.no_trace_download(request.remote_ip)
+ head :forbidden
+ elsif request.format == Mime[:xml]
+ send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
+ elsif request.format == Mime[:gpx]
+ send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
+ else
+ send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
+ end
+ else
+ head :not_found
+ end
+ rescue ActiveRecord::RecordNotFound
+ head :not_found
+ end
+
+ def edit
+ @trace = Trace.find(params[:id])
+
+ if !@trace.visible?
+ head :not_found
+ elsif current_user.nil? || @trace.user != current_user
+ head :forbidden
+ else
+ @title = t "trace.edit.title", :name => @trace.name
+
+ if request.post? && params[:trace]
+ @trace.description = params[:trace][:description]
+ @trace.tagstring = params[:trace][:tagstring]
+ @trace.visibility = params[:trace][:visibility]
+ if @trace.save
+ redirect_to :action => "view", :display_name => current_user.display_name
+ end
+ end
+ end
+ rescue ActiveRecord::RecordNotFound
+ head :not_found
+ end
+
+ def delete
+ trace = Trace.find(params[:id])
+
+ if !trace.visible?
+ head :not_found
+ elsif current_user.nil? || trace.user != current_user
+ head :forbidden
+ else
+ trace.visible = false
+ trace.save
+ flash[:notice] = t "trace.delete.scheduled_for_deletion"
+ redirect_to :action => :list, :display_name => current_user.display_name
+ end
+ rescue ActiveRecord::RecordNotFound
+ head :not_found
+ end
+
+ def georss
+ @traces = Trace.visible_to_all.visible
+
+ if params[:display_name]
+ @traces = @traces.joins(:user).where(:users => { :display_name => params[:display_name] })
+ end
+
+ @traces = @traces.tagged(params[:tag]) if params[:tag]
+ @traces = @traces.order("timestamp DESC")
+ @traces = @traces.limit(20)
+ @traces = @traces.includes(:user)
+ end
+
+ def picture
+ trace = Trace.find(params[:id])
+
+ if trace.visible? && trace.inserted?
+ if trace.public? || (current_user && current_user == trace.user)
+ expires_in 7.days, :private => !trace.public?, :public => trace.public?
+ send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => "image/gif", :disposition => "inline")
+ else
+ head :forbidden
+ end
+ else
+ head :not_found
+ end
+ rescue ActiveRecord::RecordNotFound
+ head :not_found
+ end
+
+ def icon
+ trace = Trace.find(params[:id])
+
+ if trace.visible? && trace.inserted?
+ if trace.public? || (current_user && current_user == trace.user)
+ expires_in 7.days, :private => !trace.public?, :public => trace.public?
+ send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => "image/gif", :disposition => "inline")
+ else
+ head :forbidden
+ end
+ else
+ head :not_found
+ end
+ rescue ActiveRecord::RecordNotFound
+ head :not_found
+ end
+
+ def api_read
+ trace = Trace.visible.find(params[:id])
+
+ if trace.public? || trace.user == current_user
+ render :xml => trace.to_xml.to_s
+ else
+ head :forbidden
+ end
+ end
+
+ def api_update
+ trace = Trace.visible.find(params[:id])
+
+ if trace.user == current_user
+ trace.update_from_xml(request.raw_post)
+ trace.save!
+
+ head :ok
+ else
+ head :forbidden
+ end
+ end
+
+ def api_delete
+ trace = Trace.visible.find(params[:id])
+
+ if trace.user == current_user
+ trace.visible = false
+ trace.save!
+
+ head :ok
+ else
+ head :forbidden
+ end
+ end
+
+ def api_data
+ trace = Trace.visible.find(params[:id])
+
+ if trace.public? || trace.user == current_user
+ if request.format == Mime[:xml]
+ send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
+ elsif request.format == Mime[:gpx]
+ send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
+ else
+ send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
+ end
+ else
+ head :forbidden
+ end
+ end
+
+ def api_create
+ tags = params[:tags] || ""
+ description = params[:description] || ""
+ visibility = params[:visibility]
+
+ if visibility.nil?
+ visibility = if params[:public] && params[:public].to_i.nonzero?
+ "public"
+ else
+ "private"
+ end
+ end
+
+ if params[:file].respond_to?(:read)
+ do_create(params[:file], tags, description, visibility)
+
+ if @trace.id
+ render :plain => @trace.id.to_s
+ elsif @trace.valid?
+ head :internal_server_error
+ else
+ head :bad_request
+ end
+ else
+ head :bad_request
+ end
+ end
+
+ private
+
+ def do_create(file, tags, description, visibility)
+ # Sanitise the user's filename
+ name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
+
+ # Get a temporary filename...