From 3f8d68bdf3224cd6023b3d7f8e64221222872820 Mon Sep 17 00:00:00 2001 From: Maksim Pechnikov Date: Sat, 16 May 2020 15:16:33 +0300 Subject: [PATCH] added example cache purge script --- config/config.exs | 2 +- installation/nginx-cache-purge.example | 39 +++++++++++++++++++ .../web/media_proxy/invalidations/http.ex | 16 ++++++++ .../web/media_proxy/invalidations/nginx.ex | 12 ------ .../web/media_proxy/invalidations/script.ex | 11 ++++-- 5 files changed, 64 insertions(+), 16 deletions(-) create mode 100755 installation/nginx-cache-purge.example create mode 100644 lib/pleroma/web/media_proxy/invalidations/http.ex delete mode 100644 lib/pleroma/web/media_proxy/invalidations/nginx.ex diff --git a/config/config.exs b/config/config.exs index 5394c7c7a..882d25069 100644 --- a/config/config.exs +++ b/config/config.exs @@ -382,7 +382,7 @@ config :pleroma, :media_proxy, enabled: false, provider: Pleroma.Web.MediaProxy.Invalidation.Script, options: %{ - script_path: "" + script_path: "./installation/nginx-cache-purge.example" } ], proxy_opts: [ diff --git a/installation/nginx-cache-purge.example b/installation/nginx-cache-purge.example new file mode 100755 index 000000000..12dfa733c --- /dev/null +++ b/installation/nginx-cache-purge.example @@ -0,0 +1,39 @@ +#!/bin/bash + +# A simple Bash script to delete an media from the Nginx cache. + +SCRIPTNAME=${0##*/} + +# NGINX cache directory +CACHE_DIRECTORY="/tmp/pleroma-media-cache" + +function get_cache_files() { + local max_parallel=${3-16} + find $2 -maxdepth 1 -type d | xargs -P $max_parallel -n 1 grep -ERl "^KEY:.*$1" | sort -u +} + +function purge_item() { + local cache_files + cache_files=$(get_cache_files "$1" "$2") + + if [ -n "$cache_files" ]; then + for i in $cache_files; do + [ -f $i ] || continue + echo "Deleting $i from $2." + rm $i + done + else + echo "$1 is not cached." + fi +} + +function purge() { + for url in "$@" + do + echo "$SCRIPTNAME delete $url from cache ($CACHE_DIRECTORY)" + purge_item $url $CACHE_DIRECTORY + done + +} + +purge $1 diff --git a/lib/pleroma/web/media_proxy/invalidations/http.ex b/lib/pleroma/web/media_proxy/invalidations/http.ex new file mode 100644 index 000000000..40c624efc --- /dev/null +++ b/lib/pleroma/web/media_proxy/invalidations/http.ex @@ -0,0 +1,16 @@ +defmodule Pleroma.Web.MediaProxy.Invalidation.Http do + @behaviour Pleroma.Web.MediaProxy.Invalidation + + @impl Pleroma.Web.MediaProxy.Invalidation + def purge(urls, opts) do + method = Map.get(opts, :http_method, :purge) + headers = Map.get(opts, :http_headers, []) + options = Map.get(opts, :http_options, []) + + Enum.each(urls, fn url -> + Pleroma.HTTP.request(method, url, "", headers, options) + end) + + {:ok, "success"} + end +end diff --git a/lib/pleroma/web/media_proxy/invalidations/nginx.ex b/lib/pleroma/web/media_proxy/invalidations/nginx.ex deleted file mode 100644 index 5bfdd505c..000000000 --- a/lib/pleroma/web/media_proxy/invalidations/nginx.ex +++ /dev/null @@ -1,12 +0,0 @@ -defmodule Pleroma.Web.MediaProxy.Invalidation.Nginx do - @behaviour Pleroma.Web.MediaProxy.Invalidation - - @impl Pleroma.Web.MediaProxy.Invalidation - def purge(urls, _opts) do - Enum.each(urls, fn url -> - Pleroma.HTTP.request(:purge, url, "", [], []) - end) - - {:ok, "success"} - end -end diff --git a/lib/pleroma/web/media_proxy/invalidations/script.ex b/lib/pleroma/web/media_proxy/invalidations/script.ex index f458845a0..94c79511a 100644 --- a/lib/pleroma/web/media_proxy/invalidations/script.ex +++ b/lib/pleroma/web/media_proxy/invalidations/script.ex @@ -2,9 +2,14 @@ defmodule Pleroma.Web.MediaProxy.Invalidation.Script do @behaviour Pleroma.Web.MediaProxy.Invalidation @impl Pleroma.Web.MediaProxy.Invalidation - def purge(urls, %{script_path: script_path} = options) do - script_args = List.wrap(Map.get(options, :script_args, [])) - System.cmd(Path.expand(script_path), [urls] ++ script_args) + def purge(urls, %{script_path: script_path} = _options) do + args = + urls + |> List.wrap() + |> Enum.uniq() + |> Enum.join(" ") + + System.cmd(Path.expand(script_path), [args]) {:ok, "success"} end end -- 2.45.2