]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/traces/data_controller.rb
Merge pull request #6394 from openstreetmap/dependabot/github_actions/ruby/setup...
[rails.git] / app / controllers / api / traces / data_controller.rb
1 # frozen_string_literal: true
2
3 module Api
4   module Traces
5     class DataController < ApiController
6       before_action :set_locale
7       before_action :authorize
8
9       authorize_resource :trace
10
11       before_action :offline_error
12
13       def show
14         trace = Trace.visible.find(params[:trace_id])
15
16         if trace.public? || trace.user == current_user
17           if request.format == Mime[:xml]
18             send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
19           elsif request.format == Mime[:gpx]
20             send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
21           elsif trace.file.attached?
22             redirect_to rails_blob_path(trace.file, :disposition => "attachment")
23           else
24             send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
25           end
26         else
27           head :forbidden
28         end
29       end
30
31       private
32
33       def offline_error
34         report_error "GPX files offline for maintenance", :service_unavailable if Settings.status == "gpx_offline"
35       end
36     end
37   end
38 end