Class: Service::ImageKitIoService

Inherits:
Service
  • Object
show all
Includes:
ImageKitIo::Constantable
Defined in:
lib/active_storage/service/image_kit_io_service.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ImageKitIo::Constantable

#constants, included

Constructor Details

#initialize(**options) ⇒ ImageKitIoService

Returns a new instance of ImageKitIoService.



67
68
69
# File 'lib/active_storage/service/image_kit_io_service.rb', line 67

def initialize(**options)
  @options = options
end

Class Method Details

.delete_ik_file(blob) ⇒ Object



54
55
56
# File 'lib/active_storage/service/image_kit_io_service.rb', line 54

def delete_ik_file(blob)
  ik_file(blob).delete
end

.ik_file(blob) ⇒ Object



62
63
64
# File 'lib/active_storage/service/image_kit_io_service.rb', line 62

def ik_file(blob)
  self.new.send(:ik_file).new(blob.)
end

.remote_file(blob) ⇒ Object



58
59
60
# File 'lib/active_storage/service/image_kit_io_service.rb', line 58

def remote_file(blob)
  ik_file(blob)
end

Instance Method Details

#delete(key) ⇒ Object



99
100
101
102
103
104
# File 'lib/active_storage/service/image_kit_io_service.rb', line 99

def delete(key)
  instrument :delete, key: key do
    # image kit file is already deleted on before blob destroy callback
    key
  end
end

#delete_prefixed(prefix) ⇒ Object



106
107
108
109
# File 'lib/active_storage/service/image_kit_io_service.rb', line 106

def delete_prefixed(prefix)
  # delete the variants files
  puts 'Not implemented delete_prefixed'
end

#download(key, &block) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/active_storage/service/image_kit_io_service.rb', line 83

def download(key, &block)
  if block_given?
    instrument :stream_file, key: key do
      stream_file(key, &block)
    end
  else
    instrument :download, key: key do
      image_kit_file(key)
    end
  end
end

#download_chunk(key, range) ⇒ Object



95
96
97
# File 'lib/active_storage/service/image_kit_io_service.rb', line 95

def download_chunk(key, range)
  puts 'Not implemented download_chunk'
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/active_storage/service/image_kit_io_service.rb', line 111

def exist?(key)
  image_kit_file(key).exist?
end

#headers_for_direct_upload(key, content_type:, checksum:, **options) ⇒ Object



126
127
128
129
130
# File 'lib/active_storage/service/image_kit_io_service.rb', line 126

def headers_for_direct_upload(key, content_type:, checksum:, **options)
  {
    'Content-Type' => content_type
  }
end

#open(*args, **options, &block) ⇒ Object



140
141
142
# File 'lib/active_storage/service/image_kit_io_service.rb', line 140

def open(*args, **options, &block)
  DownloaderExtension.new(self).open(*args, **options, &block)
end

#path_for(key) ⇒ Object



132
133
134
# File 'lib/active_storage/service/image_kit_io_service.rb', line 132

def path_for(key)
  image_kit_file(key).path
end

#upload(key, io, checksum: nil, **options) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/active_storage/service/image_kit_io_service.rb', line 71

def upload(key, io, checksum: nil, **options)
  instrument :upload, key: key, checksum: checksum do
    blob = storage_blob(key)
    response = client.upload_file(file: io, file_name: blob.filename.to_s, content_type: blob.content_type)
    if response[:error].nil?
      blob.update_columns(metadata: response[:response].transform_keys(&:to_sym))
    else
      raise Exception.new response[:error]
    end
  end
end

#url(key, filename: nil, content_type: '', **options) ⇒ Object



136
137
138
# File 'lib/active_storage/service/image_kit_io_service.rb', line 136

def url(key, filename: nil, content_type: '', **options)
  generate_url(key, filename: filename, content_type: content_type, path: image_kit_file(key).path, **options)
end

#url_for_direct_upload(key, **options) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/active_storage/service/image_kit_io_service.rb', line 115

def url_for_direct_upload(key, **options)
  instrument :url, key: key do |payload|
    options.delete(:content_length)
    options.delete(:checksum)
    url = "#{constants.BASE_URL}#{constants.UPLOAD}"
    generated_url = client.url(src: url)
    payload[:url] = generated_url
    generated_url
  end
end