Class: ImageKitFactory

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeImageKitFactory

Returns a new instance of ImageKitFactory.



8
9
10
11
# File 'app/lib/image_kit_factory.rb', line 8

def initialize
  require 'imagekitio'
  require 'active_storage'
end

Class Method Details

.build_urlObject



142
143
144
# File 'app/lib/image_kit_factory.rb', line 142

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

.clientObject



13
14
15
# File 'app/lib/image_kit_factory.rb', line 13

def self.client
  instance.client
end

.copy_fileObject



158
159
160
# File 'app/lib/image_kit_factory.rb', line 158

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

.delete_fileObject



162
163
164
# File 'app/lib/image_kit_factory.rb', line 162

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

.get_fileObject



150
151
152
# File 'app/lib/image_kit_factory.rb', line 150

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

.get_metadataObject



166
167
168
# File 'app/lib/image_kit_factory.rb', line 166

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

.get_metadata_from_urlObject



170
171
172
# File 'app/lib/image_kit_factory.rb', line 170

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

.get_purge_statusObject



182
183
184
# File 'app/lib/image_kit_factory.rb', line 182

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

.list_assetsObject



174
175
176
# File 'app/lib/image_kit_factory.rb', line 174

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

.purge_cacheObject



178
179
180
# File 'app/lib/image_kit_factory.rb', line 178

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

.rename_fileObject



154
155
156
# File 'app/lib/image_kit_factory.rb', line 154

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

.upload_fileObject



146
147
148
# File 'app/lib/image_kit_factory.rb', line 146

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

.url_endpointObject



28
29
30
# File 'app/lib/image_kit_factory.rb', line 28

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



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

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.present? ? query_parameters.stringify_keys : nil
  )

  client.helper.build_url(options)
end

#clientObject



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

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



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

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



106
107
108
109
# File 'app/lib/image_kit_factory.rb', line 106

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

#get_file(file_id) ⇒ Object



68
69
70
71
# File 'app/lib/image_kit_factory.rb', line 68

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

#get_metadata(file_id) ⇒ Object



111
112
113
114
# File 'app/lib/image_kit_factory.rb', line 111

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

#get_metadata_from_url(url) ⇒ Object



116
117
118
119
# File 'app/lib/image_kit_factory.rb', line 116

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

#get_purge_status(request_id) ⇒ Object



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

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



121
122
123
124
# File 'app/lib/image_kit_factory.rb', line 121

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

#purge_cache(url) ⇒ Object



126
127
128
129
# File 'app/lib/image_kit_factory.rb', line 126

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



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

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



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

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



32
33
34
# File 'app/lib/image_kit_factory.rb', line 32

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