debian-mirror-gitlab/app/uploaders/object_storage/cdn.rb

57 lines
1.3 KiB
Ruby
Raw Permalink Normal View History

2022-10-11 01:57:18 +05:30
# rubocop:disable Naming/FileName
# frozen_string_literal: true
require_relative 'cdn/google_cdn'
module ObjectStorage
module CDN
module Concern
extend ActiveSupport::Concern
include Gitlab::Utils::StrongMemoize
2022-11-25 23:54:43 +05:30
UrlResult = Struct.new(:url, :used_cdn)
2023-01-13 00:05:48 +05:30
def cdn_enabled_url(ip_address, params = {})
if use_cdn?(ip_address)
UrlResult.new(cdn_signed_url(params), true)
2022-11-25 23:54:43 +05:30
else
2023-04-23 21:23:45 +05:30
UrlResult.new(url(query: params), false)
2022-11-25 23:54:43 +05:30
end
end
2022-10-11 01:57:18 +05:30
def use_cdn?(request_ip)
return false unless cdn_options.is_a?(Hash) && cdn_options['provider']
return false unless cdn_provider
cdn_provider.use_cdn?(request_ip)
end
2023-01-13 00:05:48 +05:30
def cdn_signed_url(params = {})
cdn_provider&.signed_url(path, params: params)
2022-10-11 01:57:18 +05:30
end
private
def cdn_provider
strong_memoize(:cdn_provider) do
provider = cdn_options['provider']&.downcase
next unless provider
next GoogleCDN.new(cdn_options) if provider == 'google'
raise "Unknown CDN provider: #{provider}"
end
end
def cdn_options
return {} unless options.object_store.key?('cdn')
options.object_store.cdn
end
end
end
end
# rubocop:enable Naming/FileName