]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/traces_controller.rb
Switch traces to use ActiveStorage
[rails.git] / app / controllers / api / traces_controller.rb
1 module Api
2   class TracesController < ApiController
3     before_action :authorize_web
4     before_action :set_locale
5     before_action :authorize
6
7     authorize_resource
8
9     before_action :check_database_readable, :except => [:show, :data]
10     before_action :check_database_writable, :only => [:create, :update, :destroy]
11     before_action :check_api_readable, :only => [:show, :data]
12     before_action :check_api_writable, :only => [:create, :update, :destroy]
13     before_action :offline_error, :only => [:create, :destroy, :data]
14     around_action :api_call_handle_error
15
16     def show
17       @trace = Trace.visible.find(params[:id])
18
19       head :forbidden unless @trace.public? || @trace.user == current_user
20     end
21
22     def update
23       trace = Trace.visible.find(params[:id])
24
25       if trace.user == current_user
26         trace.update_from_xml(request.raw_post)
27         trace.save!
28
29         head :ok
30       else
31         head :forbidden
32       end
33     end
34
35     def destroy
36       trace = Trace.visible.find(params[:id])
37
38       if trace.user == current_user
39         trace.visible = false
40         trace.save!
41         TraceDestroyerJob.perform_later(trace)
42
43         head :ok
44       else
45         head :forbidden
46       end
47     end
48
49     def data
50       trace = Trace.visible.find(params[:id])
51
52       if trace.public? || trace.user == current_user
53         if request.format == Mime[:xml]
54           send_data(trace.xml_file.read, :filename => "#{trace.id}.xml", :type => request.format.to_s, :disposition => "attachment")
55         elsif request.format == Mime[:gpx]
56           send_data(trace.xml_file.read, :filename => "#{trace.id}.gpx", :type => request.format.to_s, :disposition => "attachment")
57         elsif trace.file.attached?
58           redirect_to rails_blob_path(trace.file, :disposition => "attachment")
59         else
60           send_file(trace.trace_name, :filename => "#{trace.id}#{trace.extension_name}", :type => trace.mime_type, :disposition => "attachment")
61         end
62       else
63         head :forbidden
64       end
65     end
66
67     def create
68       tags = params[:tags] || ""
69       description = params[:description] || ""
70       visibility = params[:visibility]
71
72       if visibility.nil?
73         visibility = if params[:public]&.to_i&.nonzero?
74                        "public"
75                      else
76                        "private"
77                      end
78       end
79
80       if params[:file].respond_to?(:read)
81         trace = do_create(params[:file], tags, description, visibility)
82
83         if trace.id
84           TraceImporterJob.perform_later(trace)
85           render :plain => trace.id.to_s
86         elsif trace.valid?
87           head :internal_server_error
88         else
89           head :bad_request
90         end
91       else
92         head :bad_request
93       end
94     end
95
96     private
97
98     def do_create(file, tags, description, visibility)
99       # Sanitise the user's filename
100       name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, "_")
101
102       # Create the trace object, falsely marked as already
103       # inserted to stop the import daemon trying to load it
104       trace = Trace.new(
105         :name => name,
106         :tagstring => tags,
107         :description => description,
108         :visibility => visibility,
109         :inserted => false,
110         :user => current_user,
111         :timestamp => Time.now.getutc,
112         :file => file
113       )
114
115       # Save the trace object
116       trace.save!
117
118       # Finally save the user's preferred privacy level
119       if pref = current_user.preferences.where(:k => "gps.trace.visibility").first
120         pref.v = visibility
121         pref.save
122       else
123         current_user.preferences.create(:k => "gps.trace.visibility", :v => visibility)
124       end
125
126       trace
127     end
128
129     def offline_error
130       report_error "GPX files offline for maintenance", :service_unavailable if Settings.status == "gpx_offline"
131     end
132   end
133 end