Class: Google::Genai::Files

Inherits:
Object
  • Object
show all
Defined in:
lib/google/genai/files.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_client) ⇒ Files

Returns a new instance of Files.



8
9
10
# File 'lib/google/genai/files.rb', line 8

def initialize(api_client)
  @api_client = api_client
end

Instance Method Details

#delete(name:, config: nil) ⇒ Object



53
54
55
56
57
# File 'lib/google/genai/files.rb', line 53

def delete(name:, config: nil)
  file_id = name.start_with?('files/') ? name.split('/').last : name
  @api_client.delete("v1beta/files/#{file_id}")
  nil
end

#download(file:, config: nil) ⇒ Object



80
81
82
# File 'lib/google/genai/files.rb', line 80

def download(file:, config: nil)
  # TODO: Implement file download logic
end

#get(name:, config: nil) ⇒ Object



47
48
49
50
51
# File 'lib/google/genai/files.rb', line 47

def get(name:, config: nil)
  file_id = name.start_with?('files/') ? name.split('/').last : name
  response = @api_client.get("v1beta/files/#{file_id}")
  Types::File.new(JSON.parse(response.body))
end

#list(config: nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/google/genai/files.rb', line 59

def list(config: nil)
  list_request = ->(options) do
    path = "v1beta/files"
    params = {}
    params[:pageToken] = options[:page_token] if options&.key?(:page_token)
    params[:pageSize] = options[:page_size] if options&.key?(:page_size)
    path += "?#{URI.encode_www_form(params)}" unless params.empty?
    @api_client.get(path)
  end

  response = list_request.call(config)

  Pager.new(
    name: :files,
    request: list_request,
    response: response,
    config: config,
    item_class: Types::File
  )
end

#upload(file:, config: nil) ⇒ Object

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/google/genai/files.rb', line 12

def upload(file:, config: nil)
  file_path = file.is_a?(String) ? file : file.path
  raise ArgumentError, "File not found: #{file_path}" unless File.exist?(file_path)

  config ||= {}
  mime_type = config[:mime_type] || MimeMagic.by_path(file_path)&.type
  raise ArgumentError, "Could not determine MIME type for file: #{file_path}" unless mime_type

  display_name = config[:display_name] || File.basename(file_path)
  file_size = File.size(file_path)

  response_data = @api_client.upload_file(file_path, file_size, mime_type, display_name: display_name)
  file_info = Types::File.new(response_data['file'])

  # Poll for file processing to complete with exponential backoff
  timeout = 120 # 2 minutes
  start_time = Time.now
  delay = 0.1 # Initial delay of 100ms

  loop do
    file_info = get(name: file_info.name)
    case file_info.state
    when 'ACTIVE'
      return file_info
    when 'FAILED'
      raise "File processing failed: #{file_info.error}"
    end

    raise "File processing timed out" if Time.now - start_time > timeout

    sleep delay
    delay = [delay * 1.5, 5].min # Increase delay, but cap at 5 seconds
  end
end