Update pack.ex

This commit is contained in:
niggy 2023-09-04 08:47:01 +00:00
parent 9db714fe01
commit 0eeb8ea74e
No known key found for this signature in database

View file

@ -28,6 +28,8 @@ defmodule Pleroma.Emoji.Pack do
@spec create(String.t()) :: {:ok, t()} | {:error, File.posix()} | {:error, :empty_values}
def create(name) do
name = safe_path(name)
with :ok <- validate_not_empty([name]),
dir <- Path.join(emoji_path(), name),
:ok <- File.mkdir(dir) do
@ -472,6 +474,7 @@ defp validate_not_empty(list) do
end
defp save_file(%Plug.Upload{path: upload_path}, pack, filename) do
filename = safe_path(filename)
file_path = Path.join(pack.path, filename)
create_subdirs(file_path)
@ -491,6 +494,8 @@ defp delete_emoji(pack, shortcode) do
end
defp rename_file(pack, filename, new_filename) do
filename = safe_path(filename)
new_filename = safe_path(new_filename)
old_path = Path.join(pack.path, filename)
new_path = Path.join(pack.path, new_filename)
create_subdirs(new_path)
@ -652,4 +657,13 @@ defp validate_has_all_files(pack, zip) do
|> if(do: :ok, else: {:error, :incomplete})
end
end
defp safe_path(path) do
elems = Path.split(path) |> Enum.reject(fn x -> x == ".." end)
case length(elems) do
x when x < 2 -> Enum.join(elems)
_ -> Path.join(elems)
end
end
end