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
|
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?
|
def enabled?
|
||||||
api_token.present? && zone.present?
|
api_token.present? && zone.present?
|
||||||
end
|
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)
|
def ips(expiry: 24.hours)
|
||||||
text, code = HttpartyCache.get("https://api.cloudflare.com/client/v4/ips", expiry: expiry)
|
response = Danbooru::Http.cache(expiry).get("https://api.cloudflare.com/client/v4/ips")
|
||||||
return [] if code != 200
|
return [] if response.code != 200
|
||||||
|
|
||||||
json = JSON.parse(text, symbolize_names: true)
|
result = response.parse["result"]
|
||||||
ips = json[:result][:ipv4_cidrs] + json[:result][:ipv6_cidrs]
|
ips = result["ipv4_cidrs"] + result["ipv6_cidrs"]
|
||||||
ips.map { |ip| IPAddr.new(ip) }
|
ips.map { |ip| IPAddr.new(ip) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete(md5, ext)
|
def purge_cache(urls)
|
||||||
return unless enabled?
|
return unless enabled?
|
||||||
|
|
||||||
url = "https://api.cloudflare.com/client/v4/zones/#{zone}/purge_cache"
|
cloudflare = Danbooru::Http.headers(
|
||||||
files = ["#{md5}.#{ext}", "preview/#{md5}.jpg", "sample/sample-#{md5}.jpg"].map do |name|
|
"Authorization" => "Bearer #{api_token}",
|
||||||
["danbooru", "safebooru", "raikou1", "raikou2", "raikou3", "raikou4"].map do |subdomain|
|
"Content-Type" => "application/json"
|
||||||
"http://#{subdomain}.donmai.us/data/#{name}"
|
)
|
||||||
end
|
|
||||||
end.flatten
|
|
||||||
body = {
|
|
||||||
"files" => files
|
|
||||||
}.to_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
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ module Danbooru
|
|||||||
attr_accessor :cache, :http
|
attr_accessor :cache, :http
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
delegate :get, :post, :cache, :auth, :basic_auth, :headers, to: :new
|
delegate :get, :post, :delete, :cache, :auth, :basic_auth, :headers, to: :new
|
||||||
end
|
end
|
||||||
|
|
||||||
def get(url, **options)
|
def get(url, **options)
|
||||||
@@ -14,6 +14,10 @@ module Danbooru
|
|||||||
request(:post, url, **options)
|
request(:post, url, **options)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def delete(url, **options)
|
||||||
|
request(:delete, url, **options)
|
||||||
|
end
|
||||||
|
|
||||||
def cache(expiry)
|
def cache(expiry)
|
||||||
dup.tap { |o| o.cache = expiry.to_i }
|
dup.tap { |o| o.cache = expiry.to_i }
|
||||||
end
|
end
|
||||||
@@ -41,7 +45,7 @@ module Danbooru
|
|||||||
end
|
end
|
||||||
|
|
||||||
def cached_request(method, url, **options)
|
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
|
cached_response = Cache.get(key, @cache) do
|
||||||
response = raw_request(method, url, **options)
|
response = raw_request(method, url, **options)
|
||||||
|
|||||||
@@ -123,6 +123,8 @@ class UploadService
|
|||||||
CurrentUser.as(User.system) do
|
CurrentUser.as(User.system) do
|
||||||
post.comments.create!(body: comment_replacement_message(post, replacement), do_not_bump_post: true)
|
post.comments.create!(body: comment_replacement_message(post, replacement), do_not_bump_post: true)
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
purge_cached_urls(post)
|
||||||
end
|
end
|
||||||
|
|
||||||
replacement.save!
|
replacement.save!
|
||||||
@@ -131,6 +133,18 @@ class UploadService
|
|||||||
post.update_iqdb_async
|
post.update_iqdb_async
|
||||||
end
|
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)
|
def rescale_notes(post)
|
||||||
x_scale = post.image_width.to_f / post.image_width_was.to_f
|
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
|
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, :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, :large)
|
||||||
Danbooru.config.backup_storage_manager.delete_file(post_id, md5, file_ext, :preview)
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user