Change YAML to JSON
This commit is contained in:
parent
b0ecd412f5
commit
2a94eca096
9 changed files with 58 additions and 54 deletions
|
@ -146,12 +146,12 @@ defp load do
|
|||
defp load_pack(pack_dir, emoji_groups) do
|
||||
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
|
||||
yaml = RelaxYaml.Decoder.read_from_file(pack_yaml)
|
||||
if File.exists?(pack_file) do
|
||||
contents = Jason.decode!(File.read!(pack_file))
|
||||
|
||||
yaml["files"]
|
||||
contents["files"]
|
||||
|> Enum.map(fn {name, rel_file} ->
|
||||
filename = Path.join("/emoji/#{pack_name}", rel_file)
|
||||
{name, filename, pack_name}
|
||||
|
|
|
@ -26,14 +26,14 @@ def list_packs(conn, _params) do
|
|||
results
|
||||
|> Enum.filter(fn file ->
|
||||
dir_path = Path.join(@emoji_dir_path, file)
|
||||
# Filter to only use the pack.yml packs
|
||||
File.dir?(dir_path) and File.exists?(Path.join(dir_path, "pack.yml"))
|
||||
# Filter to only use the pack.json packs
|
||||
File.dir?(dir_path) and File.exists?(Path.join(dir_path, "pack.json"))
|
||||
end)
|
||||
|> Enum.map(fn 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)
|
||||
# Transform into a map of pack-name => pack-data
|
||||
# Check if all the files are in place and can be sent
|
||||
|
@ -72,7 +72,7 @@ defp can_download?(pack, pack_path) do
|
|||
|
||||
defp create_archive_and_cache(name, pack, pack_dir, md5) do
|
||||
files =
|
||||
['pack.yml'] ++
|
||||
['pack.json'] ++
|
||||
(pack["files"] |> Enum.map(fn {_, path} -> to_charlist(path) end))
|
||||
|
||||
{:ok, {_, zip_result}} = :zip.zip('#{name}.zip', files, [:memory, cwd: to_charlist(pack_dir)])
|
||||
|
@ -82,8 +82,8 @@ defp create_archive_and_cache(name, pack, pack_dir, md5) do
|
|||
Cachex.put!(
|
||||
:emoji_packs_cache,
|
||||
name,
|
||||
# if pack.yml MD5 changes, the cache is not valid anymore
|
||||
%{pack_yml_md5: md5, pack_data: zip_result},
|
||||
# if pack.json MD5 changes, the cache is not valid anymore
|
||||
%{pack_json_md5: md5, pack_data: zip_result},
|
||||
# Add a minute to cache time for every file in the pack
|
||||
ttl: cache_ms
|
||||
)
|
||||
|
@ -95,21 +95,21 @@ defp create_archive_and_cache(name, pack, pack_dir, md5) do
|
|||
end
|
||||
|
||||
defp make_archive(name, pack, pack_dir) do
|
||||
# Having a different pack.yml md5 invalidates cache
|
||||
pack_yml_md5 = :crypto.hash(:md5, File.read!(Path.join(pack_dir, "pack.yml")))
|
||||
# Having a different pack.json md5 invalidates cache
|
||||
pack_file_md5 = :crypto.hash(:md5, File.read!(Path.join(pack_dir, "pack.json")))
|
||||
|
||||
maybe_cached_pack = Cachex.get!(:emoji_packs_cache, name)
|
||||
|
||||
zip_result =
|
||||
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
|
||||
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")
|
||||
|
||||
maybe_cached_pack[:pack_data]
|
||||
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
|
||||
|
||||
|
@ -118,10 +118,10 @@ defp make_archive(name, pack, pack_dir) do
|
|||
|
||||
def download_shared(conn, %{"name" => name}) do
|
||||
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
|
||||
pack = RelaxYaml.Decoder.read_from_file(pack_yaml)
|
||||
if File.exists?(pack_file) do
|
||||
pack = Jason.decode!(File.read!(pack_file))
|
||||
|
||||
if can_download?(pack, pack_dir) do
|
||||
zip_result = make_archive(name, pack, pack_dir)
|
||||
|
@ -185,17 +185,17 @@ def download_from(conn, %{"instance_address" => address, "pack_name" => name} =
|
|||
File.mkdir_p!(pack_dir)
|
||||
|
||||
files =
|
||||
['pack.yml'] ++
|
||||
['pack.json'] ++
|
||||
(pfiles |> Enum.map(fn {_, path} -> to_charlist(path) end))
|
||||
|
||||
{: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
|
||||
yaml_path = Path.join(pack_dir, "pack.yml")
|
||||
pack_file_path = Path.join(pack_dir, "pack.json")
|
||||
|
||||
unless File.exists?(yaml_path) do
|
||||
File.write!(yaml_path, RelaxYaml.Encoder.encode(full_pack, []))
|
||||
unless File.exists?(pack_file_path) do
|
||||
File.write!(pack_file_path, Jason.encode!(full_pack))
|
||||
end
|
||||
end
|
||||
|
||||
|
|
1
mix.exs
1
mix.exs
|
@ -157,7 +157,6 @@ defp deps do
|
|||
{:ex_rated, "~> 1.3"},
|
||||
{:ex_const, "~> 0.2"},
|
||||
{:plug_static_index_html, "~> 1.0.0"},
|
||||
{:relax_yaml, "~> 0.1"},
|
||||
{:excoveralls, "~> 0.11.1", only: :test},
|
||||
{:mox, "~> 0.5", only: :test}
|
||||
] ++ oauth_deps()
|
||||
|
|
1
mix.lock
1
mix.lock
|
@ -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"},
|
||||
"ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm"},
|
||||
"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"},
|
||||
"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"},
|
||||
|
|
16
test/instance_static/emoji/test_pack/pack.json
Normal file
16
test/instance_static/emoji/test_pack/pack.json
Normal 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"
|
||||
}
|
||||
}
|
|
@ -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
|
16
test/instance_static/emoji/test_pack_nonshared/pack.json
Normal file
16
test/instance_static/emoji/test_pack_nonshared/pack.json
Normal 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"
|
||||
}
|
||||
}
|
|
@ -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
|
|
@ -38,7 +38,7 @@ test "downloading a shared pack from download_shared" do
|
|||
|
||||
{: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)
|
||||
end
|
||||
|
||||
|
@ -92,7 +92,7 @@ test "downloading a shared pack from another instance via download_from, deletin
|
|||
)
|
||||
|> 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 conn
|
||||
|
|
Loading…
Reference in a new issue