<commit_before>require "fog/core/model"

module Fog
  module Google
    class StorageJSON
      class File < Fog::Model
        identity :key, :aliases => "Key"

        # TODO: Verify
        attribute :acl
        attribute :cache_control,       :aliases => "cacheControl"
        attribute :content_disposition, :aliases => "contentDisposition"
        attribute :content_encoding,    :aliases => "contentEncoding"
        attribute :content_length,      :aliases => "size", :type => :integer
        attribute :content_md5,         :aliases => "md5Hash"
        attribute :content_type,        :aliases => "contentType"
        attribute :crc32c
        attribute :etag,                :aliases => "etag"
        attribute :time_created,        :aliases => "timeCreated"
        attribute :last_modified,       :aliases => "updated"
        attribute :generation
        attribute :metageneration
        attribute :metadata
        attribute :self_link,           :aliases => "selfLink"
        attribute :media_link,          :aliases => "mediaLink"
        attribute :owner
        attribute :storage_class,       :aliases => "storageClass"

        # TODO: Verify
        def body
          attributes[:body] ||= last_modified && (file = collection.get(identity)) ? file.body : ""
        end

        # TODO: Verify
        def body=(new_body)
          attributes[:body] = new_body
        end

        attr_reader :directory

        # TODO: Verify
        def copy(target_directory_key, target_file_key)
          requires :directory, :key
          service.copy_object(directory.key, key, target_directory_key, target_file_key)
          target_directory = service.directories.new(:key => target_directory_key)
          target_directory.files.get(target_file_key)
        end

        # TODO: Verify
        def destroy
          requires :directory, :key
          begin
            service.delete_object(directory.key, key)
          rescue Excon::Errors::NotFound
          end
          true
        end

        # TODO: Verify
        remove_method :owner=
        def owner=(new_owner)
          if new_owner
            attributes[:owner] = {
              :display_name => new_owner["entity"],
              :id           => new_owner["entityId"]
            }
          end
        end

        # TODO: Verify
        def public=(new_public)
          if new_public
            @acl = "publicRead"
          else
            @acl = "private"
          end
          new_public
        end

        # TODO: Verify
        def public_url
          requires :directory, :key

          acl = service.get_object_acl(directory.key, key).body
          access_granted = acl["items"].detect { |entry| entry["entity"] == "allUsers" && entry["role"] == "READER" }

          if access_granted
            if directory.key.to_s =~ /^(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}$))(?:[a-z0-9]|\.(?![\.\-])|\-(?![\.])){1,61}[a-z0-9]$/
              "https://#{directory.key}.storage.googleapis.com/#{key}"
            else
              "https://storage.googleapis.com/#{directory.key}/#{key}"
            end
          end
        end

        # TODO: Verify
        def save(options = {})
          requires :body, :directory, :key
          if options != {}
            Fog::Logger.deprecation("options param is deprecated, use acl= instead [light_black](#{caller.first})[/]")
          end
          options["contentType"] = content_type if content_type
          options["acl"] ||= @acl if @acl
          options["cacheControl"] = cache_control if cache_control
          options["contentDisposition"] = content_disposition if content_disposition
          options["contentEncoding"] = content_encoding if content_encoding
          options["md5Hash"] = content_md5 if content_md5
          options["crc32c"] = crc32c if crc32c
          options.merge!(metadata)

          data = service.put_object(directory.key, key, body, options)
          merge_attributes(data.headers.reject { |key, _value| ["Content-Length", "Content-Type"].include?(key) })
          self.content_length = Fog::Storage.get_body_size(body)
          self.content_type ||= Fog::Storage.get_content_type(body)
          true
        end

        def url(expires)
          requires :key
          collection.get_http_url(key, expires)
        end

        private

        attr_writer :directory
      end
    end
  end
end
<commit_msg>Treat metadata as an included hash and not as a set of attributes with a certain prefix.<commit_after>require "fog/core/model"

module Fog
  module Google
    class StorageJSON
      class File < Fog::Model
        identity :key, :aliases => "Key"

        # TODO: Verify
        attribute :acl
        attribute :cache_control,       :aliases => "cacheControl"
        attribute :content_disposition, :aliases => "contentDisposition"
        attribute :content_encoding,    :aliases => "contentEncoding"
        attribute :content_length,      :aliases => "size", :type => :integer
        attribute :content_md5,         :aliases => "md5Hash"
        attribute :content_type,        :aliases => "contentType"
        attribute :crc32c
        attribute :etag,                :aliases => "etag"
        attribute :time_created,        :aliases => "timeCreated"
        attribute :last_modified,       :aliases => "updated"
        attribute :generation
        attribute :metageneration
        attribute :metadata
        attribute :self_link,           :aliases => "selfLink"
        attribute :media_link,          :aliases => "mediaLink"
        attribute :owner
        attribute :storage_class,       :aliases => "storageClass"

        # TODO: Verify
        def body
          attributes[:body] ||= last_modified && (file = collection.get(identity)) ? file.body : ""
        end

        # TODO: Verify
        def body=(new_body)
          attributes[:body] = new_body
        end

        attr_reader :directory

        # TODO: Verify
        def copy(target_directory_key, target_file_key)
          requires :directory, :key
          service.copy_object(directory.key, key, target_directory_key, target_file_key)
          target_directory = service.directories.new(:key => target_directory_key)
          target_directory.files.get(target_file_key)
        end

        # TODO: Verify
        def destroy
          requires :directory, :key
          begin
            service.delete_object(directory.key, key)
          rescue Excon::Errors::NotFound
          end
          true
        end

        # TODO: Verify
        remove_method :metadata=
        def metadata=(new_metadata)
          metadata.merge!(new_metadata)
        end

        # TODO: Verify
        remove_method :owner=
        def owner=(new_owner)
          if new_owner
            attributes[:owner] = {
              :display_name => new_owner["entity"],
              :id           => new_owner["entityId"]
            }
          end
        end

        # TODO: Verify
        def public=(new_public)
          if new_public
            @acl = "publicRead"
          else
            @acl = "private"
          end
          new_public
        end

        # TODO: Verify
        def public_url
          requires :directory, :key

          acl = service.get_object_acl(directory.key, key).body
          access_granted = acl["items"].detect { |entry| entry["entity"] == "allUsers" && entry["role"] == "READER" }

          if access_granted
            if directory.key.to_s =~ /^(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}$))(?:[a-z0-9]|\.(?![\.\-])|\-(?![\.])){1,61}[a-z0-9]$/
              "https://#{directory.key}.storage.googleapis.com/#{key}"
            else
              "https://storage.googleapis.com/#{directory.key}/#{key}"
            end
          end
        end

        # TODO: Verify
        def save(options = {})
          requires :body, :directory, :key
          if options != {}
            Fog::Logger.deprecation("options param is deprecated, use acl= instead [light_black](#{caller.first})[/]")
          end
          options["contentType"] = content_type if content_type
          options["acl"] ||= @acl if @acl
          options["cacheControl"] = cache_control if cache_control
          options["contentDisposition"] = content_disposition if content_disposition
          options["contentEncoding"] = content_encoding if content_encoding
          options["md5Hash"] = content_md5 if content_md5
          options["crc32c"] = crc32c if crc32c
          options["metadata"] = metadata

          data = service.put_object(directory.key, key, body, options)
          merge_attributes(data.headers.reject { |key, _value| ["Content-Length", "Content-Type"].include?(key) })
          self.content_length = Fog::Storage.get_body_size(body)
          self.content_type ||= Fog::Storage.get_content_type(body)
          true
        end

        def url(expires)
          requires :key
          collection.get_http_url(key, expires)
        end

        private

        attr_writer :directory
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.11377179430589274, 'fraction_numerical': 0.005517545795630104, 'mean_word_length': 1.9578981723237598, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 4, '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': '3619411', 'n_tokens_mistral': 2531, 'n_tokens_neox': 2497, 'n_words': 555}