Module: Shrine::Plugins::Transloadit::InstanceMethods

Defined in:
lib/shrine/plugins/transloadit.rb

Instance Method Summary collapse

Instance Method Details

#transloadit_export_step(name = "export", **options) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/shrine/plugins/transloadit.rb', line 166

def transloadit_export_step(name = "export", **options)
  unless options.key?(:credentials)
    options[:credentials] = self.class.transloadit_credentials(storage_key).to_s
  end

  if defined?(Storage::S3) && storage.is_a?(Storage::S3)
    transloadit_s3_store_step(name, **options)
  elsif defined?(Storage::GoogleCloudStorage) && storage.is_a?(Storage::GoogleCloudStorage)
    transloadit_google_store_step(name, **options)
  elsif defined?(Storage::YouTube) && storage.is_a?(Storage::YouTube)
    transloadit_youtube_store_step(name, **options)
  else
    fail Error, "cannot construct export step for #{storage.inspect}"
  end
end

#transloadit_file(result) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/shrine/plugins/transloadit.rb', line 141

def transloadit_file(result)
  result = result.first if result.is_a?(Array)
  uri    = URI.parse(result.fetch("url"))

  if defined?(Storage::S3) && storage.is_a?(Storage::S3)
    prefix = "#{storage.prefix}/" if storage.prefix
    id = uri.path.match(%r{^/#{prefix}})&.post_match or
      fail Error, "URL path doesn't start with storage prefix: #{uri}"
  elsif defined?(Storage::Url) && storage.is_a?(Storage::Url)
    id = uri.to_s
  else
    fail Error, "storage not supported: #{storage.inspect}"
  end

  self.class::UploadedFile.new(
    id:       id,
    storage:  storage_key,
    metadata: result.fetch("meta").merge(
      "filename"  => result.fetch("name"),
      "size"      => result.fetch("size"),
      "mime_type" => result.fetch("mime"),
    )
  )
end

#transloadit_files(results) ⇒ Object



137
138
139
# File 'lib/shrine/plugins/transloadit.rb', line 137

def transloadit_files(results)
  results.map { |result| transloadit_file(result) }
end