cloudflare: fix purging of cached urls.
* 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.
This commit is contained in:
@@ -1,48 +1,32 @@
|
||||
# donmai.us specific
|
||||
|
||||
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 api_token
|
||||
Danbooru.config.cloudflare_api_token
|
||||
end
|
||||
|
||||
def zone
|
||||
Danbooru.config.cloudflare_zone
|
||||
end
|
||||
|
||||
def options
|
||||
Danbooru.config.httparty_options.deep_merge(headers: {
|
||||
"Authorization" => "Bearer #{api_token}",
|
||||
"Content-Type" => "application/json",
|
||||
"User-Agent" => "#{Danbooru.config.canonical_app_name}/#{Rails.application.config.x.git_hash}"
|
||||
})
|
||||
end
|
||||
|
||||
def ips(expiry: 24.hours)
|
||||
text, code = HttpartyCache.get("https://api.cloudflare.com/client/v4/ips", expiry: expiry)
|
||||
return [] if code != 200
|
||||
response = Danbooru::Http.cache(expiry).get("https://api.cloudflare.com/client/v4/ips")
|
||||
return [] if response.code != 200
|
||||
|
||||
json = JSON.parse(text, symbolize_names: true)
|
||||
ips = json[:result][:ipv4_cidrs] + json[:result][:ipv6_cidrs]
|
||||
result = response.parse["result"]
|
||||
ips = result["ipv4_cidrs"] + result["ipv6_cidrs"]
|
||||
ips.map { |ip| IPAddr.new(ip) }
|
||||
end
|
||||
|
||||
def delete(md5, ext)
|
||||
def purge_cache(urls)
|
||||
return unless enabled?
|
||||
|
||||
url = "https://api.cloudflare.com/client/v4/zones/#{zone}/purge_cache"
|
||||
files = ["#{md5}.#{ext}", "preview/#{md5}.jpg", "sample/sample-#{md5}.jpg"].map do |name|
|
||||
["danbooru", "safebooru", "raikou1", "raikou2", "raikou3", "raikou4"].map do |subdomain|
|
||||
"http://#{subdomain}.donmai.us/data/#{name}"
|
||||
end
|
||||
end.flatten
|
||||
body = {
|
||||
"files" => files
|
||||
}.to_json
|
||||
cloudflare = Danbooru::Http.headers(
|
||||
"Authorization" => "Bearer #{api_token}",
|
||||
"Content-Type" => "application/json"
|
||||
)
|
||||
|
||||
HTTParty.delete(url, options.merge(body: body)) #, body: body)
|
||||
url = "https://api.cloudflare.com/client/v4/zones/#{zone}/purge_cache"
|
||||
cloudflare.delete(url, json: { files: urls })
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,7 +3,7 @@ module Danbooru
|
||||
attr_accessor :cache, :http
|
||||
|
||||
class << self
|
||||
delegate :get, :post, :cache, :auth, :basic_auth, :headers, to: :new
|
||||
delegate :get, :post, :delete, :cache, :auth, :basic_auth, :headers, to: :new
|
||||
end
|
||||
|
||||
def get(url, **options)
|
||||
@@ -14,6 +14,10 @@ module Danbooru
|
||||
request(:post, url, **options)
|
||||
end
|
||||
|
||||
def delete(url, **options)
|
||||
request(:delete, url, **options)
|
||||
end
|
||||
|
||||
def cache(expiry)
|
||||
dup.tap { |o| o.cache = expiry.to_i }
|
||||
end
|
||||
@@ -41,7 +45,7 @@ module Danbooru
|
||||
end
|
||||
|
||||
def cached_request(method, url, **options)
|
||||
key = Cache.hash({ method: method, url: url, **options }.to_json)
|
||||
key = Cache.hash({ method: method, url: url, headers: http.default_options.headers.to_h, **options }.to_json)
|
||||
|
||||
cached_response = Cache.get(key, @cache) do
|
||||
response = raw_request(method, url, **options)
|
||||
|
||||
@@ -123,6 +123,8 @@ class UploadService
|
||||
CurrentUser.as(User.system) do
|
||||
post.comments.create!(body: comment_replacement_message(post, replacement), do_not_bump_post: true)
|
||||
end
|
||||
else
|
||||
purge_cached_urls(post)
|
||||
end
|
||||
|
||||
replacement.save!
|
||||
@@ -131,6 +133,18 @@ class UploadService
|
||||
post.update_iqdb_async
|
||||
end
|
||||
|
||||
def purge_cached_urls(post)
|
||||
urls = [
|
||||
post.preview_file_url,
|
||||
post.large_file_url,
|
||||
post.file_url,
|
||||
post.tagged_large_file_url,
|
||||
post.tagged_file_url
|
||||
]
|
||||
|
||||
CloudflareService.new.purge_cache(urls)
|
||||
end
|
||||
|
||||
def rescale_notes(post)
|
||||
x_scale = post.image_width.to_f / post.image_width_was.to_f
|
||||
y_scale = post.image_height.to_f / post.image_height_was.to_f
|
||||
|
||||
@@ -77,10 +77,6 @@ class Post < ApplicationRecord
|
||||
Danbooru.config.backup_storage_manager.delete_file(post_id, md5, file_ext, :original)
|
||||
Danbooru.config.backup_storage_manager.delete_file(post_id, md5, file_ext, :large)
|
||||
Danbooru.config.backup_storage_manager.delete_file(post_id, md5, file_ext, :preview)
|
||||
|
||||
if Danbooru.config.cloudflare_key
|
||||
CloudflareService.new.delete(md5, file_ext)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user