<commit_before>module CarrierWave
  module Storage
    class TrueVault < Abstract
      def store!(file)
        f = CarrierWave::Storage::TrueVault::File.new(uploader, self, uploader.store_path)
        f.store(file)
        f
      end

      def retrieve!(identifier)
        CarrierWave::Storage::TrueVault::File.new(uploader, self, uploader.store_path(identifier))
      end

      class File
        include CarrierWave::Utilities::Uri
        ##
        # Current local path to file
        #
        # === Returns
        #
        # [String] a path to file
        #
        attr_reader :path

        attr_accessor :blob_id, :blob_filename, :transaction_id

        ##
        # Return all attributes from file
        #
        # === Returns
        #
        # [Hash] attributes from file
        #
        def attributes
          file.attributes
        end

        ##
        # Lookup value for file content-type header
        #
        # === Returns
        #
        # [String] value of content-type
        #
        def content_type
          @content_type || file.content_type
        end

        ##
        # Set non-default content-type header (default is file.content_type)
        #
        # === Returns
        #
        # [String] returns new content type value
        #
        def content_type=(new_content_type)
          @content_type = new_content_type
        end

        def initialize(uploader, base, path)
          @uploader, @base, @path = uploader, base, path
        end

        ##
        # Read content of file from service
        #
        # === Returns
        #
        # [String] contents of file
        def read
          file
        end

        ##
        # Return size of file body
        #
        # === Returns
        #
        # [Integer] size of file body
        #
        def size
          file.content_length
        end

        ##
        # Write file to service
        #
        # === Returns
        #
        # [Boolean] true on success or raises error
        #
        def store(file)
          truevault_file = file.to_file
          @content_type ||= file.content_type
          response = client.create_blob(@uploader.truevault_vault_id, truevault_file)
          @blob_id = response["blob_id"]
          @blob_filename ||= file.filename
          truevault_file.close if truevault_file && !truevault_file.closed?
          true
        end

        class TrueVaultStoreError < Exception; end

        private

        def client
          CarrierWave::TrueVault::Client.new(@uploader.truevault_api_key)
        end

        def file
          @file ||= client.get_blob(@uploader.truevault_vault_id, @blob_id)
          tmp = Tempfile.new('blob')
          tmp.binmode
          tmp.write(body)
          tmp.rewind
          tmp
        end

      end
    end
  end
end<commit_msg>file methods<commit_after>module CarrierWave
  module Storage
    class TrueVault < Abstract
      def store!(file)
        f = CarrierWave::Storage::TrueVault::File.new(uploader, self, uploader.store_path)
        f.store(file)
        f
      end

      def retrieve!(identifier)
        CarrierWave::Storage::TrueVault::File.new(uploader, self, uploader.store_path(identifier))
      end

      class File
        include CarrierWave::Utilities::Uri
        ##
        # Current local path to file
        #
        # === Returns
        #
        # [String] a path to file
        #
        attr_reader :path

        attr_accessor :blob_id, :blob_filename, :transaction_id

        ##
        # Return all attributes from file
        #
        # === Returns
        #
        # [Hash] attributes from file
        #
        def attributes
          file.attributes
        end

        ##
        # Lookup value for file content-type header
        #
        # === Returns
        #
        # [String] value of content-type
        #
        def content_type
          @content_type || file.content_type
        end

        ##
        # Set non-default content-type header (default is file.content_type)
        #
        # === Returns
        #
        # [String] returns new content type value
        #
        def content_type=(new_content_type)
          @content_type = new_content_type
        end

        def initialize(uploader, base, path)
          @uploader, @base, @path = uploader, base, path
        end

        ##
        # Read content of file from service
        #
        # === Returns
        #
        # [String] contents of file
        def read
          file
        end

        ##
        # Return size of file body
        #
        # === Returns
        #
        # [Integer] size of file body
        #
        def size
          file.content_length
        end

        ##
        # Write file to service
        #
        # === Returns
        #
        # [Boolean] true on success or raises error
        #
        def store(file)
          truevault_file = file.to_file
          @content_type ||= file.content_type
          @file = client.create_blob(@uploader.truevault_vault_id, truevault_file)
          @blob_id = @file.blob_id
          truevault_file.close if truevault_file && !truevault_file.closed?
          true
        end

        private

        def client
          CarrierWave::TrueVault::Client.new(@uploader.truevault_api_key)
        end

        def file
          tmp = client.get_blob(@uploader.truevault_vault_id, @blob_id)
          @file ||= IO.binread(@file)
          @file
        end

      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08271760677599567, 'fraction_numerical': 0.0, 'mean_word_length': 1.483221476510067, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, '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': '1867671', 'n_tokens_mistral': 1556, 'n_tokens_neox': 1518, 'n_words': 369}