<commit_before>module Spree
  class DigitalsController < Spree::BaseController
  
    ssl_required :show
  
    def show
      link = DigitalLink.find_by_secret(params[:secret])
      if link.present? and link.digital.attachment.present?
        attachment = link.digital.attachment
        if link.authorize! and File.file?(attachment.path)
          send_file attachment.path, :filename => attachment.original_filename, :type => attachment.content_type and return
        end
      end
      render :unauthorized
    end
    
  end
end
<commit_msg>Don't authorize! until we know the digital file exists<commit_after>module Spree
  class DigitalsController < Spree::BaseController
    ssl_required :show
  
    def show
      link = DigitalLink.find_by_secret(params[:secret])

      if link.present? and link.digital.attachment.present?
        attachment = link.digital.attachment

        # don't authorize the link unless the file exists
        # the logger error will help track down customer issues easier

        if File.file?(attachment.path)
          if link.authorize!
            send_file attachment.path, :filename => attachment.original_filename, :type => attachment.content_type and return
          end
        else
          Rails.logger.error "Missing Digital Item: #{attachment.path}"
        end
      end
      
      render :unauthorized
    end
    
  end
end

{'fraction_non_alphanumeric': 0.07065609228550829, 'fraction_numerical': 0.0, 'mean_word_length': 2.6526315789473682, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6636598', 'n_tokens_mistral': 371, 'n_tokens_neox': 359, 'n_words': 105}