From 91143a59593ac7f3ef80a47dc73aea9d4b904aaa Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Thu, 28 Mar 2024 17:26:04 +0000 Subject: [PATCH] Use imported scope to simplify trace controller methods --- app/controllers/traces/icons_controller.rb | 18 +++++++----------- app/controllers/traces/pictures_controller.rb | 18 +++++++----------- app/models/trace.rb | 1 + 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/app/controllers/traces/icons_controller.rb b/app/controllers/traces/icons_controller.rb index ec67a6bb1..c86e391e4 100644 --- a/app/controllers/traces/icons_controller.rb +++ b/app/controllers/traces/icons_controller.rb @@ -6,21 +6,17 @@ module Traces authorize_resource :trace def show - trace = Trace.visible.find(params[:trace_id]) + trace = Trace.visible.imported.find(params[:trace_id]) - if trace.inserted? - if trace.public? || (current_user && current_user == trace.user) - if trace.icon.attached? - redirect_to rails_blob_path(trace.icon, :disposition => "inline") - else - 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") - end + if trace.public? || (current_user && current_user == trace.user) + if trace.icon.attached? + redirect_to rails_blob_path(trace.icon, :disposition => "inline") else - head :forbidden + 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") end else - head :not_found + head :forbidden end rescue ActiveRecord::RecordNotFound head :not_found diff --git a/app/controllers/traces/pictures_controller.rb b/app/controllers/traces/pictures_controller.rb index 0e0d588cb..e19062167 100644 --- a/app/controllers/traces/pictures_controller.rb +++ b/app/controllers/traces/pictures_controller.rb @@ -6,21 +6,17 @@ module Traces authorize_resource :trace def show - trace = Trace.visible.find(params[:trace_id]) + trace = Trace.visible.imported.find(params[:trace_id]) - if trace.inserted? - if trace.public? || (current_user && current_user == trace.user) - if trace.icon.attached? - redirect_to rails_blob_path(trace.image, :disposition => "inline") - else - 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") - end + if trace.public? || (current_user && current_user == trace.user) + if trace.icon.attached? + redirect_to rails_blob_path(trace.image, :disposition => "inline") else - head :forbidden + 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") end else - head :not_found + head :forbidden end rescue ActiveRecord::RecordNotFound head :not_found diff --git a/app/models/trace.rb b/app/models/trace.rb index 818cc363b..d1f917571 100644 --- a/app/models/trace.rb +++ b/app/models/trace.rb @@ -38,6 +38,7 @@ class Trace < ApplicationRecord scope :visible_to, ->(u) { visible.where(:visibility => %w[public identifiable]).or(visible.where(:user => u)) } scope :visible_to_all, -> { where(:visibility => %w[public identifiable]) } scope :tagged, ->(t) { joins(:tags).where(:gpx_file_tags => { :tag => t }) } + scope :imported, -> { where(:inserted => true) } has_one_attached :file, :service => Settings.trace_file_storage has_one_attached :image, :service => Settings.trace_image_storage -- 2.39.5