# frozen_string_literal: true class ExternalUploadsController < ApplicationController skip_before_action :require_authentication! def show upload = Upload.includes(:blob).find(params[:id]) redirect_to upload.blob.url(disposition: :inline, filename: upload.filename), allow_other_host: true rescue ActiveRecord::RecordNotFound head :not_found end def rescue url = params[:url] if url.blank? head :bad_request return end upload = Upload.includes(:blob).find_by(original_url: url) if upload redirect_to upload.cdn_url, allow_other_host: true else render_not_found_response(url) end end private def render_not_found_response(url) if url.match?(/\.(png|jpe?g)$/i) render_error_image else head :not_found end end def render_error_image svg = <<~SVG 404 Original URL not found in CDN This file hasn't been uploaded or rescued yet. Try uploading it at cdn.hackclub.com SVG render inline: svg, content_type: "image/svg+xml" end end