Change YAML to JSON

This commit is contained in:
Ekaterina Vaartis 2019-08-15 11:39:39 +03:00
parent b0ecd412f5
commit 2a94eca096
9 changed files with 58 additions and 54 deletions

View file

@ -146,12 +146,12 @@ defmodule Pleroma.Emoji do
defp load_pack(pack_dir, emoji_groups) do defp load_pack(pack_dir, emoji_groups) do
pack_name = Path.basename(pack_dir) pack_name = Path.basename(pack_dir)
pack_yaml = Path.join(pack_dir, "pack.yml") pack_file = Path.join(pack_dir, "pack.json")
if File.exists?(pack_yaml) do if File.exists?(pack_file) do
yaml = RelaxYaml.Decoder.read_from_file(pack_yaml) contents = Jason.decode!(File.read!(pack_file))
yaml["files"] contents["files"]
|> Enum.map(fn {name, rel_file} -> |> Enum.map(fn {name, rel_file} ->
filename = Path.join("/emoji/#{pack_name}", rel_file) filename = Path.join("/emoji/#{pack_name}", rel_file)
{name, filename, pack_name} {name, filename, pack_name}

View file

@ -26,14 +26,14 @@ defmodule Pleroma.Web.EmojiAPI.EmojiAPIController do
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)
# Filter to only use the pack.yml packs # Filter to only use the pack.json packs
File.dir?(dir_path) and File.exists?(Path.join(dir_path, "pack.yml")) File.dir?(dir_path) and File.exists?(Path.join(dir_path, "pack.json"))
end) end)
|> Enum.map(fn pack_name -> |> Enum.map(fn pack_name ->
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.yml") pack_file = Path.join(pack_path, "pack.json")
{pack_name, RelaxYaml.Decoder.read_from_file(pack_file)} {pack_name, Jason.decode!(File.read!(pack_file))}
end) end)
# Transform into a map of pack-name => pack-data # Transform into a map of pack-name => pack-data
# Check if all the files are in place and can be sent # Check if all the files are in place and can be sent
@ -72,7 +72,7 @@ defmodule Pleroma.Web.EmojiAPI.EmojiAPIController do
defp create_archive_and_cache(name, pack, pack_dir, md5) do defp create_archive_and_cache(name, pack, pack_dir, md5) do
files = files =
['pack.yml'] ++ ['pack.json'] ++
(pack["files"] |> Enum.map(fn {_, path} -> to_charlist(path) end)) (pack["files"] |> Enum.map(fn {_, path} -> to_charlist(path) end))
{: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)])
@ -82,8 +82,8 @@ defmodule Pleroma.Web.EmojiAPI.EmojiAPIController do
Cachex.put!( Cachex.put!(
:emoji_packs_cache, :emoji_packs_cache,
name, name,
# if pack.yml MD5 changes, the cache is not valid anymore # if pack.json MD5 changes, the cache is not valid anymore
%{pack_yml_md5: md5, pack_data: zip_result}, %{pack_json_md5: md5, pack_data: zip_result},
# Add a minute to cache time for every file in the pack # Add a minute to cache time for every file in the pack
ttl: cache_ms ttl: cache_ms
) )
@ -95,21 +95,21 @@ keeping it in cache for #{div(cache_ms, 1000)}s")
end end
defp make_archive(name, pack, pack_dir) do defp make_archive(name, pack, pack_dir) do
# Having a different pack.yml md5 invalidates cache # Having a different pack.json md5 invalidates cache
pack_yml_md5 = :crypto.hash(:md5, File.read!(Path.join(pack_dir, "pack.yml"))) pack_file_md5 = :crypto.hash(:md5, File.read!(Path.join(pack_dir, "pack.json")))
maybe_cached_pack = Cachex.get!(:emoji_packs_cache, name) maybe_cached_pack = Cachex.get!(:emoji_packs_cache, name)
zip_result = zip_result =
if is_nil(maybe_cached_pack) do if is_nil(maybe_cached_pack) do
create_archive_and_cache(name, pack, pack_dir, pack_yml_md5) create_archive_and_cache(name, pack, pack_dir, pack_file_md5)
else else
if maybe_cached_pack[:pack_yml_md5] == pack_yml_md5 do if maybe_cached_pack[:pack_file_md5] == pack_file_md5 do
Logger.debug("Using cache for the '#{name}' shared emoji pack") Logger.debug("Using cache for the '#{name}' shared emoji pack")
maybe_cached_pack[:pack_data] maybe_cached_pack[:pack_data]
else else
create_archive_and_cache(name, pack, pack_dir, pack_yml_md5) create_archive_and_cache(name, pack, pack_dir, pack_file_md5)
end end
end end
@ -118,10 +118,10 @@ keeping it in cache for #{div(cache_ms, 1000)}s")
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_yaml = Path.join(pack_dir, "pack.yml") pack_file = Path.join(pack_dir, "pack.json")
if File.exists?(pack_yaml) do if File.exists?(pack_file) do
pack = RelaxYaml.Decoder.read_from_file(pack_yaml) pack = Jason.decode!(File.read!(pack_file))
if can_download?(pack, pack_dir) do if can_download?(pack, pack_dir) do
zip_result = make_archive(name, pack, pack_dir) zip_result = make_archive(name, pack, pack_dir)
@ -185,17 +185,17 @@ keeping it in cache for #{div(cache_ms, 1000)}s")
File.mkdir_p!(pack_dir) File.mkdir_p!(pack_dir)
files = files =
['pack.yml'] ++ ['pack.json'] ++
(pfiles |> Enum.map(fn {_, path} -> to_charlist(path) end)) (pfiles |> Enum.map(fn {_, path} -> to_charlist(path) end))
{:ok, _} = :zip.unzip(emoji_archive, cwd: to_charlist(pack_dir), file_list: files) {:ok, _} = :zip.unzip(emoji_archive, cwd: to_charlist(pack_dir), file_list: files)
# Fallback URL might not contain a pack.yml file. Put on we have if there's none # Fallback URL might not contain a pack.json file. Put on we have if there's none
if pinfo[:fallback] do if pinfo[:fallback] do
yaml_path = Path.join(pack_dir, "pack.yml") pack_file_path = Path.join(pack_dir, "pack.json")
unless File.exists?(yaml_path) do unless File.exists?(pack_file_path) do
File.write!(yaml_path, RelaxYaml.Encoder.encode(full_pack, [])) File.write!(pack_file_path, Jason.encode!(full_pack))
end end
end end

View file

@ -157,7 +157,6 @@ defmodule Pleroma.Mixfile do
{:ex_rated, "~> 1.3"}, {:ex_rated, "~> 1.3"},
{:ex_const, "~> 0.2"}, {:ex_const, "~> 0.2"},
{:plug_static_index_html, "~> 1.0.0"}, {:plug_static_index_html, "~> 1.0.0"},
{:relax_yaml, "~> 0.1"},
{:excoveralls, "~> 0.11.1", only: :test}, {:excoveralls, "~> 0.11.1", only: :test},
{:mox, "~> 0.5", only: :test} {:mox, "~> 0.5", only: :test}
] ++ oauth_deps() ] ++ oauth_deps()

View file

@ -84,7 +84,6 @@
"quantum": {:hex, :quantum, "2.3.4", "72a0e8855e2adc101459eac8454787cb74ab4169de6ca50f670e72142d4960e9", [:mix], [{:calendar, "~> 0.17", [hex: :calendar, repo: "hexpm", optional: true]}, {:crontab, "~> 1.1", [hex: :crontab, repo: "hexpm", optional: false]}, {:gen_stage, "~> 0.12", [hex: :gen_stage, repo: "hexpm", optional: false]}, {:swarm, "~> 3.3", [hex: :swarm, repo: "hexpm", optional: false]}, {:timex, "~> 3.1", [hex: :timex, repo: "hexpm", optional: true]}], "hexpm"}, "quantum": {:hex, :quantum, "2.3.4", "72a0e8855e2adc101459eac8454787cb74ab4169de6ca50f670e72142d4960e9", [:mix], [{:calendar, "~> 0.17", [hex: :calendar, repo: "hexpm", optional: true]}, {:crontab, "~> 1.1", [hex: :crontab, repo: "hexpm", optional: false]}, {:gen_stage, "~> 0.12", [hex: :gen_stage, repo: "hexpm", optional: false]}, {:swarm, "~> 3.3", [hex: :swarm, repo: "hexpm", optional: false]}, {:timex, "~> 3.1", [hex: :timex, repo: "hexpm", optional: true]}], "hexpm"},
"ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm"}, "ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm"},
"recon": {:git, "https://github.com/ferd/recon.git", "75d70c7c08926d2f24f1ee6de14ee50fe8a52763", [tag: "2.4.0"]}, "recon": {:git, "https://github.com/ferd/recon.git", "75d70c7c08926d2f24f1ee6de14ee50fe8a52763", [tag: "2.4.0"]},
"relax_yaml": {:hex, :relax_yaml, "0.1.4", "99e55ae80b3bd1135f4288e1ba77b816ad7de05bcb4618a1a9f983ce7c89ff32", [:mix], [{:yamerl, "~> 0.4.0", [hex: :yamerl, repo: "hexpm", optional: false]}], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.4", "f0eafff810d2041e93f915ef59899c923f4568f4585904d010387ed74988e77b", [:make, :mix, :rebar3], [], "hexpm"}, "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.4", "f0eafff810d2041e93f915ef59899c923f4568f4585904d010387ed74988e77b", [:make, :mix, :rebar3], [], "hexpm"},
"swarm": {:hex, :swarm, "3.4.0", "64f8b30055d74640d2186c66354b33b999438692a91be275bb89cdc7e401f448", [:mix], [{:gen_state_machine, "~> 2.0", [hex: :gen_state_machine, repo: "hexpm", optional: false]}, {:libring, "~> 1.0", [hex: :libring, repo: "hexpm", optional: false]}], "hexpm"}, "swarm": {:hex, :swarm, "3.4.0", "64f8b30055d74640d2186c66354b33b999438692a91be275bb89cdc7e401f448", [:mix], [{:gen_state_machine, "~> 2.0", [hex: :gen_state_machine, repo: "hexpm", optional: false]}, {:libring, "~> 1.0", [hex: :libring, repo: "hexpm", optional: false]}], "hexpm"},
"sweet_xml": {:hex, :sweet_xml, "0.6.6", "fc3e91ec5dd7c787b6195757fbcf0abc670cee1e4172687b45183032221b66b8", [:mix], [], "hexpm"}, "sweet_xml": {:hex, :sweet_xml, "0.6.6", "fc3e91ec5dd7c787b6195757fbcf0abc670cee1e4172687b45183032221b66b8", [:mix], [], "hexpm"},

View file

@ -0,0 +1,16 @@
{
"pack": {
"license": "Test license",
"homepage": "https://pleroma.social",
"description": "Test description",
"fallblack-src": "https://example.com",
"fallback-src-sha256": "65CDCCBCA9388A68023519F997367783BE69ED42864398CAC568E56F65CE0E75",
"share-files": true
},
"files": {
"blank": "blank.png"
}
}

View file

@ -1,13 +0,0 @@
pack:
license: Test license
homepage: https://pleroma.social
description: Test description
fallblack-src: https://example.com
# SHA256 of the fallback-src
fallback-src-sha256: 65CDCCBCA9388A68023519F997367783BE69ED42864398CAC568E56F65CE0E75
share-files: true
files:
blank: blank.png

View file

@ -0,0 +1,16 @@
{
"pack": {
"license": "Test license",
"homepage": "https://pleroma.social",
"description": "Test description",
"fallblack-src": "https://example.com",
"fallback-src-sha256": "65CDCCBCA9388A68023519F997367783BE69ED42864398CAC568E56F65CE0E75",
"share-files": false
},
"files": {
"blank": "blank.png"
}
}

View file

@ -1,13 +0,0 @@
pack:
license: Test license
homepage: https://pleroma.social
description: Test description
fallblack-src: https://example.com
# SHA256 of the fallback-src
fallback-src-sha256: 65CDCCBCA9388A68023519F997367783BE69ED42864398CAC568E56F65CE0E75
share-files: false
files:
blank: blank.png

View file

@ -38,7 +38,7 @@ defmodule Pleroma.Web.EmojiAPI.EmojiAPIControllerTest do
{:ok, arch} = :zip.unzip(resp, [:memory]) {:ok, arch} = :zip.unzip(resp, [:memory])
assert Enum.find(arch, fn {n, _} -> n == 'pack.yml' end) assert Enum.find(arch, fn {n, _} -> n == 'pack.json' end)
assert Enum.find(arch, fn {n, _} -> n == 'blank.png' end) assert Enum.find(arch, fn {n, _} -> n == 'blank.png' end)
end end
@ -92,7 +92,7 @@ defmodule Pleroma.Web.EmojiAPI.EmojiAPIControllerTest do
) )
|> text_response(200) == "ok" |> text_response(200) == "ok"
assert File.exists?("test/instance_static/emoji/test_pack2/pack.yml") assert File.exists?("test/instance_static/emoji/test_pack2/pack.json")
assert File.exists?("test/instance_static/emoji/test_pack2/blank.png") assert File.exists?("test/instance_static/emoji/test_pack2/blank.png")
assert conn assert conn