Class: Dscf::Core::FileStorage::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
app/services/dscf/core/file_storage/client.rb

Overview

Low-level HTTP client for MinIO service This is an internal class - other engines should NOT use this directly

Defined Under Namespace

Classes: ConfigurationError, DownloadError, UploadError

Instance Method Summary collapse

Constructor Details

#initialize(app_name: nil, record_class: nil) ⇒ Client

Returns a new instance of Client.



15
16
17
18
19
20
# File 'app/services/dscf/core/file_storage/client.rb', line 15

def initialize(app_name: nil, record_class: nil)
  @base_uri = ENV.fetch("MINIO_SERVICE_URL", "http://localhost:3001")
  @record_class = record_class
  @api_key = resolve_api_key(app_name)
  validate_configuration!
end

Instance Method Details

#delete(file_key) ⇒ Boolean

Delete a file from MinIO

Parameters:

  • file_key (String)

    the file key/name in MinIO

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
79
80
# File 'app/services/dscf/core/file_storage/client.rb', line 70

def delete(file_key)
  response = self.class.delete(
    "#{@base_uri}/delete/#{URI.encode_www_form_component(file_key)}",
    headers: auth_headers,
    timeout: 30
  )
  response.success?
rescue StandardError => e
  Rails.logger.error("FileStorage::Client delete error: #{e.message}")
  false
end

#download(file_key) ⇒ Hash

Download a file from MinIO

Parameters:

  • file_key (String)

    the file key/name in MinIO

Returns:

  • (Hash)

    { data:, filename:, content_type: }



43
44
45
46
47
48
49
50
51
# File 'app/services/dscf/core/file_storage/client.rb', line 43

def download(file_key)
  response = self.class.get(
    "#{@base_uri}/download/#{URI.encode_www_form_component(file_key)}",
    headers: auth_headers,
    timeout: 60
  )

  handle_download_response(response, file_key)
end

#exists?(file_key) ⇒ Boolean

Check if file exists

Parameters:

  • file_key (String)

    the file key/name in MinIO

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
# File 'app/services/dscf/core/file_storage/client.rb', line 56

def exists?(file_key)
  response = self.class.head(
    "#{@base_uri}/download/#{URI.encode_www_form_component(file_key)}",
    headers: auth_headers,
    timeout: 10
  )
  response.success?
rescue StandardError
  false
end

#upload(file, filename: nil) ⇒ Hash

Upload a file to MinIO

Parameters:

  • file (ActionDispatch::Http::UploadedFile, File, IO)

    the file to upload

  • filename (String) (defaults to: nil)

    optional custom filename

Returns:

  • (Hash)

    { file_key:, filename:, size:, content_type: }



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/services/dscf/core/file_storage/client.rb', line 26

def upload(file, filename: nil)
  prepared = prepare_file(file, filename)

  response = self.class.post(
    "#{@base_uri}/upload",
    headers: auth_headers,
    body: {file: prepared[:io]},
    multipart: true,
    timeout: 60
  )

  handle_upload_response(response, prepared)
end