Class: ImageKitFactory

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
app/lib/image_kit_factory.rb

Overview

Library code: image kit factory.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeImageKitFactory

Returns a new instance of ImageKitFactory.



10
11
12
13
# File 'app/lib/image_kit_factory.rb', line 10

def initialize
  require 'imagekitio'
  require 'active_storage'
end

Class Method Details

.build_urlObject



144
145
146
# File 'app/lib/image_kit_factory.rb', line 144

def build_url(*, **)
  instance.build_url(*, **)
end

.clientObject



15
16
17
# File 'app/lib/image_kit_factory.rb', line 15

def self.client
  instance.client
end

.copy_fileObject



160
161
162
# File 'app/lib/image_kit_factory.rb', line 160

def copy_file(*, **)
  instance.copy_file(*, **)
end

.delete_fileObject



164
165
166
# File 'app/lib/image_kit_factory.rb', line 164

def delete_file(*, **)
  instance.delete_file(*, **)
end

.get_fileObject



152
153
154
# File 'app/lib/image_kit_factory.rb', line 152

def get_file(*, **)
  instance.get_file(*, **)
end

.get_metadataObject



168
169
170
# File 'app/lib/image_kit_factory.rb', line 168

def (*, **)
  instance.(*, **)
end

.get_metadata_from_urlObject



172
173
174
# File 'app/lib/image_kit_factory.rb', line 172

def (*, **)
  instance.(*, **)
end

.get_purge_statusObject



184
185
186
# File 'app/lib/image_kit_factory.rb', line 184

def get_purge_status(*, **)
  instance.get_purge_status(*, **)
end

.list_assetsObject



176
177
178
# File 'app/lib/image_kit_factory.rb', line 176

def list_assets(*, **)
  instance.list_assets(*, **)
end

.purge_cacheObject



180
181
182
# File 'app/lib/image_kit_factory.rb', line 180

def purge_cache(*, **)
  instance.purge_cache(*, **)
end

.rename_fileObject



156
157
158
# File 'app/lib/image_kit_factory.rb', line 156

def rename_file(*, **)
  instance.rename_file(*, **)
end

.upload_fileObject



148
149
150
# File 'app/lib/image_kit_factory.rb', line 148

def upload_file(*, **)
  instance.upload_file(*, **)
end

.url_endpointObject



30
31
32
# File 'app/lib/image_kit_factory.rb', line 30

def self.url_endpoint
  instance.url_endpoint
end

Instance Method Details

#build_url(src:, transformations: [], query_parameters: {}, transformation_position: 'query') ⇒ Object

Helper methods to build ImageKit parameters
This centralizes all ImageKit-specific knowledge in one place



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/lib/image_kit_factory.rb', line 41

def build_url(src:, transformations: [], query_parameters: {}, transformation_position: 'query')
  if transformations.present?
    transformation_objects = transformations.map do |t|
      ::Imagekitio::Transformation.new(**t.symbolize_keys)
    end
  end

  options = ::Imagekitio::SrcOptions.new(
    src: src,
    url_endpoint: url_endpoint,
    transformation: transformation_objects,
    transformation_position: transformation_position.to_sym,
    query_parameters: query_parameters.presence&.stringify_keys
  )

  client.helper.build_url(options)
end

#clientObject



19
20
21
22
23
24
25
26
27
28
# File 'app/lib/image_kit_factory.rb', line 19

def client
  # ImageKit 4.0: Create client instance with credentials
  # Note: Client only needs private_key, public_key is not used by v4.0 API
  @client ||= begin
    ikioc = Heatwave::Configuration.fetch(:imagekit)
    ::Imagekitio::Client.new(
      private_key: ikioc[:private_key]
    )
  end
end

#copy_file(source_file_path:, destination_path:, new_file_name: nil, include_file_versions: false) ⇒ Object

Copy a file to a new location (preserves original)

Parameters:

  • source_file_path (String)

    Full path of source file (e.g., '/img/old-slug')

  • destination_path (String)

    Destination folder path (e.g., '/img/')

  • new_file_name (String) (defaults to: nil)

    New filename for the copy

  • include_file_versions (Boolean) (defaults to: false)

    Whether to copy all versions



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/lib/image_kit_factory.rb', line 88

def copy_file(source_file_path:, destination_path:, new_file_name: nil, include_file_versions: false)
  params = ::Imagekitio::FileCopyParams.new(
    source_file_path: source_file_path,
    destination_path: destination_path,
    include_file_versions: include_file_versions
  )
  result = client.files.copy(params)

  # If a new filename is specified and different from source, rename the copy
  if new_file_name.present?
    source_filename = File.basename(source_file_path)
    if source_filename != new_file_name
      new_file_path = File.join(destination_path, source_filename)
      rename_file(file_path: new_file_path, new_file_name: new_file_name)
    end
  end

  result
end

#delete_file(file_id) ⇒ Object



108
109
110
111
# File 'app/lib/image_kit_factory.rb', line 108

def delete_file(file_id)
  params = ::Imagekitio::FileDeleteParams.new
  client.files.delete(file_id, params)
end

#get_file(file_id) ⇒ Object



70
71
72
73
# File 'app/lib/image_kit_factory.rb', line 70

def get_file(file_id)
  params = ::Imagekitio::FileGetParams.new
  client.files.get(file_id, params)
end

#get_metadata(file_id) ⇒ Object



113
114
115
116
# File 'app/lib/image_kit_factory.rb', line 113

def (file_id)
  params = ::Imagekitio::Files::MetadataGetParams.new
  client.files..get(file_id, params)
end

#get_metadata_from_url(url) ⇒ Object



118
119
120
121
# File 'app/lib/image_kit_factory.rb', line 118

def (url)
  params = ::Imagekitio::Files::MetadataGetFromUrlParams.new(url: url)
  client.files..get_from_url(params)
end

#get_purge_status(request_id) ⇒ Object



133
134
135
136
137
138
139
140
# File 'app/lib/image_kit_factory.rb', line 133

def get_purge_status(request_id)
  params = ::Imagekitio::Cache::InvalidationGetParams.new
  client.cache.invalidation.get(request_id, params)
rescue ::Imagekitio::Errors::BadRequestError => e
  # Request ID is invalid or expired - return nil to indicate failure
  Rails.logger.warn("ImageKit purge status check failed for request_id #{request_id}: #{e.message}")
  nil
end

#list_assets(tags: []) ⇒ Object



123
124
125
126
# File 'app/lib/image_kit_factory.rb', line 123

def list_assets(tags: [])
  params = ::Imagekitio::AssetListParams.new(tags: tags)
  client.assets.list(params)
end

#purge_cache(url) ⇒ Object



128
129
130
131
# File 'app/lib/image_kit_factory.rb', line 128

def purge_cache(url)
  params = ::Imagekitio::Cache::InvalidationCreateParams.new(url: url)
  client.cache.invalidation.create(params)
end

#rename_file(file_path:, new_file_name:) ⇒ Object



75
76
77
78
79
80
81
# File 'app/lib/image_kit_factory.rb', line 75

def rename_file(file_path:, new_file_name:)
  params = ::Imagekitio::FileRenameParams.new(
    file_path: file_path,
    new_file_name: new_file_name
  )
  client.files.rename(params)
end

#upload_file(file:, file_name:, tags: [], folder: 'img/', use_unique_file_name: false) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'app/lib/image_kit_factory.rb', line 59

def upload_file(file:, file_name:, tags: [], folder: 'img/', use_unique_file_name: false)
  params = ::Imagekitio::FileUploadParams.new(
    file: file,
    file_name: file_name,
    tags: tags,
    use_unique_file_name: use_unique_file_name,
    folder: folder
  )
  client.files.upload(params)
end

#url_endpointObject



34
35
36
# File 'app/lib/image_kit_factory.rb', line 34

def url_endpoint
  @url_endpoint ||= Heatwave::Configuration.fetch(:imagekit)[:url_endpoint]
end