mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-02-19 20:04:23 +01:00
Cache purging callbacks (#87)
This commit is contained in:
parent
2e8a5e92c9
commit
57427079f1
7 changed files with 72 additions and 0 deletions
|
@ -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-development /usr/local/bin/run-development
|
||||||
COPY docker/app/run-test /usr/local/bin/run-test
|
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/safe-rsvg-convert /usr/local/bin/safe-rsvg-convert
|
||||||
|
COPY docker/app/purge-cache /usr/local/bin/purge-cache
|
||||||
CMD run-development
|
CMD run-development
|
||||||
|
|
18
docker/app/purge-cache
Executable file
18
docker/app/purge-cache
Executable 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}"
|
|
@ -10,11 +10,13 @@ defmodule Philomena.Images do
|
||||||
|
|
||||||
alias Philomena.Elasticsearch
|
alias Philomena.Elasticsearch
|
||||||
alias Philomena.ThumbnailWorker
|
alias Philomena.ThumbnailWorker
|
||||||
|
alias Philomena.ImagePurgeWorker
|
||||||
alias Philomena.DuplicateReports.DuplicateReport
|
alias Philomena.DuplicateReports.DuplicateReport
|
||||||
alias Philomena.Images.Image
|
alias Philomena.Images.Image
|
||||||
alias Philomena.Images.Hider
|
alias Philomena.Images.Hider
|
||||||
alias Philomena.Images.Uploader
|
alias Philomena.Images.Uploader
|
||||||
alias Philomena.Images.Tagging
|
alias Philomena.Images.Tagging
|
||||||
|
alias Philomena.Images.Thumbnailer
|
||||||
alias Philomena.Images.ElasticsearchIndex, as: ImageIndex
|
alias Philomena.Images.ElasticsearchIndex, as: ImageIndex
|
||||||
alias Philomena.IndexWorker
|
alias Philomena.IndexWorker
|
||||||
alias Philomena.ImageFeatures.ImageFeature
|
alias Philomena.ImageFeatures.ImageFeature
|
||||||
|
@ -127,6 +129,7 @@ defmodule Philomena.Images do
|
||||||
|> case do
|
|> case do
|
||||||
{:ok, image} ->
|
{:ok, image} ->
|
||||||
Uploader.unpersist_old_upload(image)
|
Uploader.unpersist_old_upload(image)
|
||||||
|
purge_files(image, image.hidden_image_key)
|
||||||
Hider.destroy_thumbnails(image)
|
Hider.destroy_thumbnails(image)
|
||||||
|
|
||||||
{:ok, image}
|
{:ok, image}
|
||||||
|
@ -195,6 +198,7 @@ defmodule Philomena.Images do
|
||||||
Uploader.unpersist_old_upload(image)
|
Uploader.unpersist_old_upload(image)
|
||||||
|
|
||||||
repair_image(image)
|
repair_image(image)
|
||||||
|
purge_files(image, image.hidden_image_key)
|
||||||
reindex_image(image)
|
reindex_image(image)
|
||||||
|
|
||||||
{:ok, image}
|
{:ok, image}
|
||||||
|
@ -464,6 +468,7 @@ defmodule Philomena.Images do
|
||||||
Tags.reindex_tags(tags)
|
Tags.reindex_tags(tags)
|
||||||
reindex_image(image)
|
reindex_image(image)
|
||||||
reindex_copied_tags(result)
|
reindex_copied_tags(result)
|
||||||
|
purge_files(image, image.hidden_image_key)
|
||||||
|
|
||||||
{:ok, result}
|
{:ok, result}
|
||||||
|
|
||||||
|
@ -510,6 +515,7 @@ defmodule Philomena.Images do
|
||||||
Hider.unhide_thumbnails(image, key)
|
Hider.unhide_thumbnails(image, key)
|
||||||
|
|
||||||
reindex_image(image)
|
reindex_image(image)
|
||||||
|
purge_files(image, image.hidden_image_key)
|
||||||
Comments.reindex_comments(image)
|
Comments.reindex_comments(image)
|
||||||
Tags.reindex_tags(tags)
|
Tags.reindex_tags(tags)
|
||||||
|
|
||||||
|
@ -677,6 +683,22 @@ defmodule Philomena.Images do
|
||||||
|> Elasticsearch.reindex(Image)
|
|> Elasticsearch.reindex(Image)
|
||||||
end
|
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
|
alias Philomena.Images.Subscription
|
||||||
|
|
||||||
def subscribed?(_image, nil), do: false
|
def subscribed?(_image, nil), do: false
|
||||||
|
|
|
@ -29,6 +29,12 @@ defmodule Philomena.Images.Hider do
|
||||||
File.rm_rf(normal)
|
File.rm_rf(normal)
|
||||||
end
|
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
|
# fixme: these are copied from the thumbnailer
|
||||||
defp image_thumb_dir(%Image{created_at: created_at, id: id}),
|
defp image_thumb_dir(%Image{created_at: created_at, id: id}),
|
||||||
do: Path.join([image_thumbnail_root(), time_identifier(created_at), to_string(id)])
|
do: Path.join([image_thumbnail_root(), time_identifier(created_at), to_string(id)])
|
||||||
|
|
|
@ -22,6 +22,14 @@ defmodule Philomena.Images.Thumbnailer do
|
||||||
full: nil
|
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
|
def generate_thumbnails(image_id) do
|
||||||
image = Repo.get!(Image, image_id)
|
image = Repo.get!(Image, image_id)
|
||||||
file = image_file(image)
|
file = image_file(image)
|
||||||
|
@ -124,6 +132,12 @@ defmodule Philomena.Images.Thumbnailer do
|
||||||
defp image_thumb_dir(%Image{created_at: created_at, id: id}),
|
defp image_thumb_dir(%Image{created_at: created_at, id: id}),
|
||||||
do: Path.join([image_thumbnail_root(), time_identifier(created_at), to_string(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),
|
defp time_identifier(time),
|
||||||
do: Enum.join([time.year, time.month, time.day], "/")
|
do: Enum.join([time.year, time.month, time.day], "/")
|
||||||
|
|
||||||
|
@ -132,4 +146,7 @@ defmodule Philomena.Images.Thumbnailer do
|
||||||
|
|
||||||
defp image_thumbnail_root,
|
defp image_thumbnail_root,
|
||||||
do: Application.get_env(:philomena, :image_file_root) <> "/thumbs"
|
do: Application.get_env(:philomena, :image_file_root) <> "/thumbs"
|
||||||
|
|
||||||
|
defp image_url_root,
|
||||||
|
do: Application.get_env(:philomena, :image_url_root)
|
||||||
end
|
end
|
||||||
|
|
7
lib/philomena/workers/image_purge_worker.ex
Normal file
7
lib/philomena/workers/image_purge_worker.ex
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
defmodule Philomena.ImagePurgeWorker do
|
||||||
|
alias Philomena.Images
|
||||||
|
|
||||||
|
def perform(files) do
|
||||||
|
Images.perform_purge(files)
|
||||||
|
end
|
||||||
|
end
|
|
@ -9,6 +9,7 @@ defmodule PhilomenaWeb.Image.RepairController do
|
||||||
|
|
||||||
def create(conn, _params) do
|
def create(conn, _params) do
|
||||||
Images.repair_image(conn.assigns.image)
|
Images.repair_image(conn.assigns.image)
|
||||||
|
Images.purge_files(conn.assigns.image, conn.assigns.image.hidden_image_key)
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> put_flash(:info, "Repair job enqueued.")
|
|> put_flash(:info, "Repair job enqueued.")
|
||||||
|
|
Loading…
Reference in a new issue