mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-01-31 19:36:44 +01:00
Compatible thumbnailer
This commit is contained in:
parent
1670fd9eb9
commit
4c97791a7b
1 changed files with 17 additions and 82 deletions
|
@ -39,7 +39,7 @@ defmodule Philomena.Images.Thumbnailer do
|
|||
file = download_image_file(image)
|
||||
{:ok, analysis} = Analyzers.analyze(file)
|
||||
|
||||
apply_edit_script(image, Processors.process(analysis, file, @versions))
|
||||
apply_edit_script(image, Processors.process(analysis, file, generated_sizes(image)))
|
||||
generate_dupe_reports(image)
|
||||
recompute_meta(image, file, &Image.thumbnail_changeset/2)
|
||||
|
||||
|
@ -54,16 +54,13 @@ defmodule Philomena.Images.Thumbnailer do
|
|||
do: ImageIntensities.create_image_intensity(image, intensities)
|
||||
|
||||
defp apply_change(image, {:replace_original, new_file}),
|
||||
do: copy(new_file, image_file(image))
|
||||
do: upload_file(image, new_file, "full.#{image.image_format}")
|
||||
|
||||
defp apply_change(image, {:thumbnails, thumbnails}),
|
||||
do: Enum.map(thumbnails, &apply_thumbnail(image, image_thumb_dir(image), &1))
|
||||
do: Enum.map(thumbnails, &apply_thumbnail(image, &1))
|
||||
|
||||
defp apply_thumbnail(_image, thumb_dir, {:copy, new_file, destination}),
|
||||
do: copy(new_file, Path.join(thumb_dir, destination))
|
||||
|
||||
defp apply_thumbnail(image, thumb_dir, {:symlink_original, destination}),
|
||||
do: symlink(image_file(image), Path.join(thumb_dir, destination))
|
||||
defp apply_thumbnail(image, {:copy, new_file, destination}),
|
||||
do: upload_file(image, new_file, destination)
|
||||
|
||||
defp generate_dupe_reports(image) do
|
||||
if not image.duplication_checked do
|
||||
|
@ -84,103 +81,41 @@ defmodule Philomena.Images.Thumbnailer do
|
|||
|> Repo.update!()
|
||||
end
|
||||
|
||||
# Copy from source to destination, creating parent directories along
|
||||
# the way and setting the appropriate permission bits when necessary.
|
||||
#
|
||||
# sobelow_skip ["Traversal.FileModule"]
|
||||
defp copy(source, destination) do
|
||||
prepare_dir(destination)
|
||||
|
||||
File.rm(destination)
|
||||
File.cp!(source, destination)
|
||||
|
||||
set_perms(destination)
|
||||
end
|
||||
|
||||
# Try to handle filesystems that don't support symlinks
|
||||
# by falling back to a copy.
|
||||
#
|
||||
# sobelow_skip ["Traversal.FileModule"]
|
||||
defp symlink(source, destination) do
|
||||
source = Path.absname(source)
|
||||
|
||||
prepare_dir(destination)
|
||||
|
||||
case File.ln_s(source, destination) do
|
||||
:ok ->
|
||||
set_perms(destination)
|
||||
|
||||
_err ->
|
||||
copy(source, destination)
|
||||
end
|
||||
end
|
||||
|
||||
# 0o644 = (S_IRUSR | S_IWUSR) | S_IRGRP | S_IROTH
|
||||
#
|
||||
# sobelow_skip ["Traversal.FileModule"]
|
||||
defp set_perms(destination),
|
||||
do: File.chmod(destination, 0o644)
|
||||
|
||||
# Prepare the directory by creating it if it does not yet exist.
|
||||
#
|
||||
# sobelow_skip ["Traversal.FileModule"]
|
||||
defp prepare_dir(destination) do
|
||||
destination
|
||||
|> Path.dirname()
|
||||
|> File.mkdir_p!()
|
||||
end
|
||||
|
||||
def image_file(%Image{image: image}),
|
||||
do: Path.join(image_file_root(), image)
|
||||
|
||||
defp download_image_file(%Image{image: path} = image) do
|
||||
defp download_image_file(image) do
|
||||
tempfile = Briefly.create!(extname: "." <> image.image_format)
|
||||
path = Path.join(image_file_root(), path)
|
||||
path = Path.join(image_thumb_prefix(image), "full.#{image.image_format}")
|
||||
|
||||
ExAws.request!(S3.download_file(bucket(), path, tempfile))
|
||||
|
||||
tempfile
|
||||
end
|
||||
|
||||
defp upload_image_file(%Image{image: path}, new_file) do
|
||||
path = Path.join(image_file_root(), path)
|
||||
defp upload_file(image, file, version_name) do
|
||||
path = Path.join(image_thumb_prefix(image), version_name)
|
||||
|
||||
new_file
|
||||
file
|
||||
|> S3.Upload.stream_file()
|
||||
|> S3.upload(bucket(), path, acl: :public_read)
|
||||
|> ExAws.request!()
|
||||
end
|
||||
|
||||
def image_thumb_dir(%Image{
|
||||
defp image_thumb_prefix(%Image{
|
||||
created_at: created_at,
|
||||
id: id,
|
||||
hidden_from_users: true,
|
||||
hidden_image_key: key
|
||||
}),
|
||||
do: Path.join([image_thumbnail_root(), time_identifier(created_at), "#{id}-#{key}"])
|
||||
do: Path.join([image_file_root(), time_identifier(created_at), "#{id}-#{key}"])
|
||||
|
||||
def 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 image_thumb_prefix(%Image{created_at: created_at, id: id}),
|
||||
do: Path.join([image_file_root(), time_identifier(created_at), to_string(id)])
|
||||
|
||||
defp time_identifier(time),
|
||||
do: Enum.join([time.year, time.month, time.day], "/")
|
||||
|
||||
defp image_file_root,
|
||||
do: Application.get_env(:philomena, :image_file_root)
|
||||
do: Application.fetch_env!(:philomena, :image_file_root)
|
||||
|
||||
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)
|
||||
|
||||
defp bucket() do
|
||||
Application.fetch_env!(:philomena, :s3_bucket)
|
||||
end
|
||||
defp bucket,
|
||||
do: Application.fetch_env!(:philomena, :s3_bucket)
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue