<commit_before>#encoding: utf-8

module PostgresqlLoStreamer
  class LoController < ActionController::Base
    def stream
      send_file_headers! configuration.options
      self.status = 200
      self.response_body = Enumerator.new do |y|
        connection.transaction do
          lo = connection.lo_open(params[:id].to_i, ::PG::INV_READ)
          while data = connection.lo_read(lo, 4096) do
            y << data
          end
          connection.lo_close(lo)
        end
      end
    end

    def connection
      @con ||= ActiveRecord::Base.connection.raw_connection
    end

    private

    def configuration
      PostgresqlLoStreamer.configuration
    end
  end
end
<commit_msg>Set status code to 404 if object does not exist<commit_after>#encoding: utf-8

module PostgresqlLoStreamer
  class LoController < ActionController::Base
    def stream
      send_file_headers! configuration.options

      object_identifier = params[:id].to_i
      if !object_exists?(object_identifier)
        self.status = 404
        render :nothing => true and return
      end

      self.status = 200
      self.response_body = Enumerator.new do |y|
        connection.transaction do
          lo = connection.lo_open(object_identifier, ::PG::INV_READ)
          while data = connection.lo_read(lo, 4096) do
            y << data
          end
          connection.lo_close(lo)
        end
      end
    end

    def object_exists?(identifier)
      begin
        connection.lo_open(identifier, ::PG::INV_READ)
      rescue PG::Error => e
        if e.to_s.include? "does not exist"
          return false
        end
      end

      return true
    end

    def connection
      @con ||= ActiveRecord::Base.connection.raw_connection
    end

    private

    def configuration
      PostgresqlLoStreamer.configuration
    end
  end
end

{'fraction_non_alphanumeric': 0.07181719260065289, 'fraction_numerical': 0.011969532100108813, 'mean_word_length': 2.2491166077738516, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '2887749', 'n_tokens_mistral': 544, 'n_tokens_neox': 517, 'n_words': 133}