forked from AkkomaGang/akkoma
Move emoji_dir_path & cache_seconds_per_file
This commit is contained in:
parent
cdbe7cd37a
commit
f21dbbc021
1 changed files with 26 additions and 25 deletions
|
@ -3,12 +3,12 @@ defmodule Pleroma.Web.PleromaAPI.EmojiAPIController do
|
||||||
|
|
||||||
require Logger
|
require Logger
|
||||||
|
|
||||||
@emoji_dir_path Path.join(
|
def emoji_dir_path() do
|
||||||
|
Path.join(
|
||||||
Pleroma.Config.get!([:instance, :static_dir]),
|
Pleroma.Config.get!([:instance, :static_dir]),
|
||||||
"emoji"
|
"emoji"
|
||||||
)
|
)
|
||||||
|
end
|
||||||
@cache_seconds_per_file Pleroma.Config.get!([:emoji, :shared_pack_cache_seconds_per_file])
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Lists the packs available on the instance as JSON.
|
Lists the packs available on the instance as JSON.
|
||||||
|
@ -19,8 +19,8 @@ defmodule Pleroma.Web.PleromaAPI.EmojiAPIController do
|
||||||
def list_packs(conn, _params) do
|
def list_packs(conn, _params) do
|
||||||
# Create the directory first if it does not exist. This is probably the first request made
|
# Create the directory first if it does not exist. This is probably the first request made
|
||||||
# with the API so it should be sufficient
|
# with the API so it should be sufficient
|
||||||
with {:create_dir, :ok} <- {:create_dir, File.mkdir_p(@emoji_dir_path)},
|
with {:create_dir, :ok} <- {:create_dir, File.mkdir_p(emoji_dir_path())},
|
||||||
{:ls, {:ok, results}} <- {:ls, File.ls(@emoji_dir_path)} do
|
{:ls, {:ok, results}} <- {:ls, File.ls(emoji_dir_path())} do
|
||||||
pack_infos =
|
pack_infos =
|
||||||
results
|
results
|
||||||
|> Enum.filter(&has_pack_json?/1)
|
|> Enum.filter(&has_pack_json?/1)
|
||||||
|
@ -35,33 +35,33 @@ def list_packs(conn, _params) do
|
||||||
{:create_dir, {:error, e}} ->
|
{:create_dir, {:error, e}} ->
|
||||||
conn
|
conn
|
||||||
|> put_status(:internal_server_error)
|
|> put_status(:internal_server_error)
|
||||||
|> json(%{error: "Failed to create the emoji pack directory at #{@emoji_dir_path}: #{e}"})
|
|> json(%{error: "Failed to create the emoji pack directory at #{emoji_dir_path()}: #{e}"})
|
||||||
|
|
||||||
{:ls, {:error, e}} ->
|
{:ls, {:error, e}} ->
|
||||||
conn
|
conn
|
||||||
|> put_status(:internal_server_error)
|
|> put_status(:internal_server_error)
|
||||||
|> json(%{
|
|> json(%{
|
||||||
error:
|
error:
|
||||||
"Failed to get the contents of the emoji pack directory at #{@emoji_dir_path}: #{e}"
|
"Failed to get the contents of the emoji pack directory at #{emoji_dir_path()}: #{e}"
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp has_pack_json?(file) do
|
defp has_pack_json?(file) do
|
||||||
dir_path = Path.join(@emoji_dir_path, file)
|
dir_path = Path.join(emoji_dir_path(), file)
|
||||||
# Filter to only use the pack.json packs
|
# Filter to only use the pack.json packs
|
||||||
File.dir?(dir_path) and File.exists?(Path.join(dir_path, "pack.json"))
|
File.dir?(dir_path) and File.exists?(Path.join(dir_path, "pack.json"))
|
||||||
end
|
end
|
||||||
|
|
||||||
defp load_pack(pack_name) do
|
defp load_pack(pack_name) do
|
||||||
pack_path = Path.join(@emoji_dir_path, pack_name)
|
pack_path = Path.join(emoji_dir_path(), pack_name)
|
||||||
pack_file = Path.join(pack_path, "pack.json")
|
pack_file = Path.join(pack_path, "pack.json")
|
||||||
|
|
||||||
{pack_name, Jason.decode!(File.read!(pack_file))}
|
{pack_name, Jason.decode!(File.read!(pack_file))}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp validate_pack({name, pack}) do
|
defp validate_pack({name, pack}) do
|
||||||
pack_path = Path.join(@emoji_dir_path, name)
|
pack_path = Path.join(emoji_dir_path(), name)
|
||||||
|
|
||||||
if can_download?(pack, pack_path) do
|
if can_download?(pack, pack_path) do
|
||||||
archive_for_sha = make_archive(name, pack, pack_path)
|
archive_for_sha = make_archive(name, pack, pack_path)
|
||||||
|
@ -95,7 +95,8 @@ defp create_archive_and_cache(name, pack, pack_dir, md5) do
|
||||||
|
|
||||||
{:ok, {_, zip_result}} = :zip.zip('#{name}.zip', files, [:memory, cwd: to_charlist(pack_dir)])
|
{:ok, {_, zip_result}} = :zip.zip('#{name}.zip', files, [:memory, cwd: to_charlist(pack_dir)])
|
||||||
|
|
||||||
cache_ms = :timer.seconds(@cache_seconds_per_file * Enum.count(files))
|
cache_seconds_per_file = Pleroma.Config.get!([:emoji, :shared_pack_cache_seconds_per_file])
|
||||||
|
cache_ms = :timer.seconds(cache_seconds_per_file * Enum.count(files))
|
||||||
|
|
||||||
Cachex.put!(
|
Cachex.put!(
|
||||||
:emoji_packs_cache,
|
:emoji_packs_cache,
|
||||||
|
@ -131,7 +132,7 @@ defp make_archive(name, pack, pack_dir) do
|
||||||
to download packs that the instance shares.
|
to download packs that the instance shares.
|
||||||
"""
|
"""
|
||||||
def download_shared(conn, %{"name" => name}) do
|
def download_shared(conn, %{"name" => name}) do
|
||||||
pack_dir = Path.join(@emoji_dir_path, name)
|
pack_dir = Path.join(emoji_dir_path(), name)
|
||||||
pack_file = Path.join(pack_dir, "pack.json")
|
pack_file = Path.join(pack_dir, "pack.json")
|
||||||
|
|
||||||
with {_, true} <- {:exists?, File.exists?(pack_file)},
|
with {_, true} <- {:exists?, File.exists?(pack_file)},
|
||||||
|
@ -211,7 +212,7 @@ def download_from(conn, %{"instance_address" => address, "pack_name" => name} =
|
||||||
%{body: emoji_archive} <- Tesla.get!(uri),
|
%{body: emoji_archive} <- Tesla.get!(uri),
|
||||||
{_, true} <- {:checksum, Base.decode16!(sha) == :crypto.hash(:sha256, emoji_archive)} do
|
{_, true} <- {:checksum, Base.decode16!(sha) == :crypto.hash(:sha256, emoji_archive)} do
|
||||||
local_name = data["as"] || name
|
local_name = data["as"] || name
|
||||||
pack_dir = Path.join(@emoji_dir_path, local_name)
|
pack_dir = Path.join(emoji_dir_path(), local_name)
|
||||||
File.mkdir_p!(pack_dir)
|
File.mkdir_p!(pack_dir)
|
||||||
|
|
||||||
files = Enum.map(full_pack["files"], fn {_, path} -> to_charlist(path) end)
|
files = Enum.map(full_pack["files"], fn {_, path} -> to_charlist(path) end)
|
||||||
|
@ -249,7 +250,7 @@ def download_from(conn, %{"instance_address" => address, "pack_name" => name} =
|
||||||
Creates an empty pack named `name` which then can be updated via the admin UI.
|
Creates an empty pack named `name` which then can be updated via the admin UI.
|
||||||
"""
|
"""
|
||||||
def create(conn, %{"name" => name}) do
|
def create(conn, %{"name" => name}) do
|
||||||
pack_dir = Path.join(@emoji_dir_path, name)
|
pack_dir = Path.join(emoji_dir_path(), name)
|
||||||
|
|
||||||
if not File.exists?(pack_dir) do
|
if not File.exists?(pack_dir) do
|
||||||
File.mkdir_p!(pack_dir)
|
File.mkdir_p!(pack_dir)
|
||||||
|
@ -273,7 +274,7 @@ def create(conn, %{"name" => name}) do
|
||||||
Deletes the pack `name` and all it's files.
|
Deletes the pack `name` and all it's files.
|
||||||
"""
|
"""
|
||||||
def delete(conn, %{"name" => name}) do
|
def delete(conn, %{"name" => name}) do
|
||||||
pack_dir = Path.join(@emoji_dir_path, name)
|
pack_dir = Path.join(emoji_dir_path(), name)
|
||||||
|
|
||||||
case File.rm_rf(pack_dir) do
|
case File.rm_rf(pack_dir) do
|
||||||
{:ok, _} ->
|
{:ok, _} ->
|
||||||
|
@ -292,7 +293,7 @@ def delete(conn, %{"name" => name}) do
|
||||||
`new_data` is the new metadata for the pack, that will replace the old metadata.
|
`new_data` is the new metadata for the pack, that will replace the old metadata.
|
||||||
"""
|
"""
|
||||||
def update_metadata(conn, %{"pack_name" => name, "new_data" => new_data}) do
|
def update_metadata(conn, %{"pack_name" => name, "new_data" => new_data}) do
|
||||||
pack_file_p = Path.join([@emoji_dir_path, name, "pack.json"])
|
pack_file_p = Path.join([emoji_dir_path(), name, "pack.json"])
|
||||||
|
|
||||||
full_pack = Jason.decode!(File.read!(pack_file_p))
|
full_pack = Jason.decode!(File.read!(pack_file_p))
|
||||||
|
|
||||||
|
@ -376,7 +377,7 @@ def update_file(
|
||||||
conn,
|
conn,
|
||||||
%{"pack_name" => pack_name, "action" => "add", "shortcode" => shortcode} = params
|
%{"pack_name" => pack_name, "action" => "add", "shortcode" => shortcode} = params
|
||||||
) do
|
) do
|
||||||
pack_dir = Path.join(@emoji_dir_path, pack_name)
|
pack_dir = Path.join(emoji_dir_path(), pack_name)
|
||||||
pack_file_p = Path.join(pack_dir, "pack.json")
|
pack_file_p = Path.join(pack_dir, "pack.json")
|
||||||
|
|
||||||
full_pack = Jason.decode!(File.read!(pack_file_p))
|
full_pack = Jason.decode!(File.read!(pack_file_p))
|
||||||
|
@ -424,7 +425,7 @@ def update_file(conn, %{
|
||||||
"action" => "remove",
|
"action" => "remove",
|
||||||
"shortcode" => shortcode
|
"shortcode" => shortcode
|
||||||
}) do
|
}) do
|
||||||
pack_dir = Path.join(@emoji_dir_path, pack_name)
|
pack_dir = Path.join(emoji_dir_path(), pack_name)
|
||||||
pack_file_p = Path.join(pack_dir, "pack.json")
|
pack_file_p = Path.join(pack_dir, "pack.json")
|
||||||
|
|
||||||
full_pack = Jason.decode!(File.read!(pack_file_p))
|
full_pack = Jason.decode!(File.read!(pack_file_p))
|
||||||
|
@ -459,7 +460,7 @@ def update_file(
|
||||||
conn,
|
conn,
|
||||||
%{"pack_name" => pack_name, "action" => "update", "shortcode" => shortcode} = params
|
%{"pack_name" => pack_name, "action" => "update", "shortcode" => shortcode} = params
|
||||||
) do
|
) do
|
||||||
pack_dir = Path.join(@emoji_dir_path, pack_name)
|
pack_dir = Path.join(emoji_dir_path(), pack_name)
|
||||||
pack_file_p = Path.join(pack_dir, "pack.json")
|
pack_file_p = Path.join(pack_dir, "pack.json")
|
||||||
|
|
||||||
full_pack = Jason.decode!(File.read!(pack_file_p))
|
full_pack = Jason.decode!(File.read!(pack_file_p))
|
||||||
|
@ -529,11 +530,11 @@ def update_file(conn, %{"action" => action}) do
|
||||||
assumed to be emojis and stored in the new `pack.json` file.
|
assumed to be emojis and stored in the new `pack.json` file.
|
||||||
"""
|
"""
|
||||||
def import_from_fs(conn, _params) do
|
def import_from_fs(conn, _params) do
|
||||||
with {:ok, results} <- File.ls(@emoji_dir_path) do
|
with {:ok, results} <- File.ls(emoji_dir_path()) do
|
||||||
imported_pack_names =
|
imported_pack_names =
|
||||||
results
|
results
|
||||||
|> Enum.filter(fn file ->
|
|> Enum.filter(fn file ->
|
||||||
dir_path = Path.join(@emoji_dir_path, file)
|
dir_path = Path.join(emoji_dir_path(), file)
|
||||||
# Find the directories that do NOT have pack.json
|
# Find the directories that do NOT have pack.json
|
||||||
File.dir?(dir_path) and not File.exists?(Path.join(dir_path, "pack.json"))
|
File.dir?(dir_path) and not File.exists?(Path.join(dir_path, "pack.json"))
|
||||||
end)
|
end)
|
||||||
|
@ -549,7 +550,7 @@ def import_from_fs(conn, _params) do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp write_pack_json_contents(dir) do
|
defp write_pack_json_contents(dir) do
|
||||||
dir_path = Path.join(@emoji_dir_path, dir)
|
dir_path = Path.join(emoji_dir_path(), dir)
|
||||||
emoji_txt_path = Path.join(dir_path, "emoji.txt")
|
emoji_txt_path = Path.join(dir_path, "emoji.txt")
|
||||||
|
|
||||||
files_for_pack = files_for_pack(emoji_txt_path, dir_path)
|
files_for_pack = files_for_pack(emoji_txt_path, dir_path)
|
||||||
|
|
Loading…
Reference in a new issue