* Switch CloudflareService from HttpartyCache to Danbooru::Http. * Purge cached urls from Cloudflare when a post is replaced and the md5 doesn't change. This happens when a corrupted image is replaced or thumbnails are regenerated. Before we purged urls when a post was expunged, which was unneeded because those urls can expire naturally. It was also wrong because the subdomains were hardcoded, the urls used http:// instead of https://, and we didn't account for tagged urls.
33 lines
891 B
Ruby
33 lines
891 B
Ruby
class CloudflareService
|
|
attr_reader :api_token, :zone
|
|
|
|
def initialize(api_token: Danbooru.config.cloudflare_api_token, zone: Danbooru.config.cloudflare_zone)
|
|
@api_token, @zone = api_token, zone
|
|
end
|
|
|
|
def enabled?
|
|
api_token.present? && zone.present?
|
|
end
|
|
|
|
def ips(expiry: 24.hours)
|
|
response = Danbooru::Http.cache(expiry).get("https://api.cloudflare.com/client/v4/ips")
|
|
return [] if response.code != 200
|
|
|
|
result = response.parse["result"]
|
|
ips = result["ipv4_cidrs"] + result["ipv6_cidrs"]
|
|
ips.map { |ip| IPAddr.new(ip) }
|
|
end
|
|
|
|
def purge_cache(urls)
|
|
return unless enabled?
|
|
|
|
cloudflare = Danbooru::Http.headers(
|
|
"Authorization" => "Bearer #{api_token}",
|
|
"Content-Type" => "application/json"
|
|
)
|
|
|
|
url = "https://api.cloudflare.com/client/v4/zones/#{zone}/purge_cache"
|
|
cloudflare.delete(url, json: { files: urls })
|
|
end
|
|
end
|