Cache purging callbacks (#87)

This commit is contained in:
liamwhite 2020-12-16 10:53:26 -05:00 committed by GitHub
parent 2e8a5e92c9
commit 57427079f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 72 additions and 0 deletions

View file

@ -27,4 +27,5 @@ RUN git clone https://github.com/philomena-dev/mediatools /tmp/mediatools \
COPY docker/app/run-development /usr/local/bin/run-development
COPY docker/app/run-test /usr/local/bin/run-test
COPY docker/app/safe-rsvg-convert /usr/local/bin/safe-rsvg-convert
COPY docker/app/purge-cache /usr/local/bin/purge-cache
CMD run-development

18
docker/app/purge-cache Executable file
View file

@ -0,0 +1,18 @@
#!/bin/bash
# Run your custom purge command here.
#
# The script receives the list of URLs to be purged
# as JSON on the first argument.
#
# {"files":[]}
body="$1"
api_token=
zone_id=
# curl -XPOST \
# -H "Content-Type: application/json" \
# -H "Authorization: Bearer ${api_token}" \
# "https://api.cloudflare.com/client/v4/zones/${zone_id}/purge_cache" \
# -d "${body}"

View file

@ -10,11 +10,13 @@ defmodule Philomena.Images do
alias Philomena.Elasticsearch
alias Philomena.ThumbnailWorker
alias Philomena.ImagePurgeWorker
alias Philomena.DuplicateReports.DuplicateReport
alias Philomena.Images.Image
alias Philomena.Images.Hider
alias Philomena.Images.Uploader
alias Philomena.Images.Tagging
alias Philomena.Images.Thumbnailer
alias Philomena.Images.ElasticsearchIndex, as: ImageIndex
alias Philomena.IndexWorker
alias Philomena.ImageFeatures.ImageFeature
@ -127,6 +129,7 @@ defmodule Philomena.Images do
|> case do
{:ok, image} ->
Uploader.unpersist_old_upload(image)
purge_files(image, image.hidden_image_key)
Hider.destroy_thumbnails(image)
{:ok, image}
@ -195,6 +198,7 @@ defmodule Philomena.Images do
Uploader.unpersist_old_upload(image)
repair_image(image)
purge_files(image, image.hidden_image_key)
reindex_image(image)
{:ok, image}
@ -464,6 +468,7 @@ defmodule Philomena.Images do
Tags.reindex_tags(tags)
reindex_image(image)
reindex_copied_tags(result)
purge_files(image, image.hidden_image_key)
{:ok, result}
@ -510,6 +515,7 @@ defmodule Philomena.Images do
Hider.unhide_thumbnails(image, key)
reindex_image(image)
purge_files(image, image.hidden_image_key)
Comments.reindex_comments(image)
Tags.reindex_tags(tags)
@ -677,6 +683,22 @@ defmodule Philomena.Images do
|> Elasticsearch.reindex(Image)
end
def purge_files(image, hidden_key) do
files =
if is_nil(hidden_key) do
Thumbnailer.thumbnail_urls(image, nil)
else
Thumbnailer.thumbnail_urls(image, hidden_key) ++
Thumbnailer.thumbnail_urls(image, nil)
end
Exq.enqueue(Exq, "indexing", ImagePurgeWorker, [files])
end
def perform_purge(files) do
Hider.purge_cache(files)
end
alias Philomena.Images.Subscription
def subscribed?(_image, nil), do: false

View file

@ -29,6 +29,12 @@ defmodule Philomena.Images.Hider do
File.rm_rf(normal)
end
def purge_cache(files) do
{_out, 0} = System.cmd("purge-cache", [Jason.encode!(%{files: files})])
:ok
end
# fixme: these are copied from the thumbnailer
defp image_thumb_dir(%Image{created_at: created_at, id: id}),
do: Path.join([image_thumbnail_root(), time_identifier(created_at), to_string(id)])

View file

@ -22,6 +22,14 @@ defmodule Philomena.Images.Thumbnailer do
full: nil
]
def thumbnail_urls(image, hidden_key) do
Path.join([image_thumb_dir(image), "*"])
|> Path.wildcard()
|> Enum.map(fn version_name ->
Path.join([image_url_base(image, hidden_key), Path.basename(version_name)])
end)
end
def generate_thumbnails(image_id) do
image = Repo.get!(Image, image_id)
file = image_file(image)
@ -124,6 +132,12 @@ defmodule Philomena.Images.Thumbnailer do
defp image_thumb_dir(%Image{created_at: created_at, id: id}),
do: Path.join([image_thumbnail_root(), time_identifier(created_at), to_string(id)])
defp image_url_base(%Image{created_at: created_at, id: id}, nil),
do: Path.join([image_url_root(), time_identifier(created_at), to_string(id)])
defp image_url_base(%Image{created_at: created_at, id: id}, key),
do: Path.join([image_url_root(), time_identifier(created_at), "#{id}-#{key}"])
defp time_identifier(time),
do: Enum.join([time.year, time.month, time.day], "/")
@ -132,4 +146,7 @@ defmodule Philomena.Images.Thumbnailer do
defp image_thumbnail_root,
do: Application.get_env(:philomena, :image_file_root) <> "/thumbs"
defp image_url_root,
do: Application.get_env(:philomena, :image_url_root)
end

View file

@ -0,0 +1,7 @@
defmodule Philomena.ImagePurgeWorker do
alias Philomena.Images
def perform(files) do
Images.perform_purge(files)
end
end

View file

@ -9,6 +9,7 @@ defmodule PhilomenaWeb.Image.RepairController do
def create(conn, _params) do
Images.repair_image(conn.assigns.image)
Images.purge_files(conn.assigns.image, conn.assigns.image.hidden_image_key)
conn
|> put_flash(:info, "Repair job enqueued.")