Change YAML to JSON
authorEkaterina Vaartis <vaartis@cock.li>
Thu, 15 Aug 2019 08:39:39 +0000 (11:39 +0300)
committerEkaterina Vaartis <vaartis@cock.li>
Wed, 18 Sep 2019 21:16:33 +0000 (00:16 +0300)
lib/pleroma/emoji.ex
lib/pleroma/web/emoji_api/emoji_api_controller.ex
mix.exs
mix.lock
test/instance_static/emoji/test_pack/pack.json [new file with mode: 0644]
test/instance_static/emoji/test_pack/pack.yml [deleted file]
test/instance_static/emoji/test_pack_nonshared/pack.json [new file with mode: 0644]
test/instance_static/emoji/test_pack_nonshared/pack.yml [deleted file]
test/web/emoji_api_controller_test.exs

index f56b26da25f093bb4643b425bd8efb8c049cd606..170a7d0980d38fb9925bb905e5d26b7f02636fdc 100644 (file)
@@ -146,12 +146,12 @@ defmodule Pleroma.Emoji 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}
index 49d970277185b3e6ab09f4d4c2a3d933d3513892..aedc70372137c839ef5b5ae94027d489050b7902 100644 (file)
@@ -26,14 +26,14 @@ defmodule Pleroma.Web.EmojiAPI.EmojiAPIController 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 @@ defmodule Pleroma.Web.EmojiAPI.EmojiAPIController 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 @@ defmodule Pleroma.Web.EmojiAPI.EmojiAPIController 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 @@ keeping it in cache for #{div(cache_ms, 1000)}s")
   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 @@ keeping it in cache for #{div(cache_ms, 1000)}s")
 
   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 @@ keeping it in cache for #{div(cache_ms, 1000)}s")
           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
 
diff --git a/mix.exs b/mix.exs
index e8356d5647bf1d461063bc43d26700bad57be1ae..f2635da2427feacc994c00d08ea7400d12f86f97 100644 (file)
--- a/mix.exs
+++ b/mix.exs
@@ -157,7 +157,6 @@ defmodule Pleroma.Mixfile 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()
index 8852b5f65c2e471dec4da8049e4997ec67464b8e..d27041b966ac1fd374c834e31d29c614d1f982b3 100644 (file)
--- a/mix.lock
+++ b/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"},
diff --git a/test/instance_static/emoji/test_pack/pack.json b/test/instance_static/emoji/test_pack/pack.json
new file mode 100644 (file)
index 0000000..1b260f0
--- /dev/null
@@ -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"
+    }
+}
diff --git a/test/instance_static/emoji/test_pack/pack.yml b/test/instance_static/emoji/test_pack/pack.yml
deleted file mode 100644 (file)
index 851b06d..0000000
+++ /dev/null
@@ -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
diff --git a/test/instance_static/emoji/test_pack_nonshared/pack.json b/test/instance_static/emoji/test_pack_nonshared/pack.json
new file mode 100644 (file)
index 0000000..b49b1ef
--- /dev/null
@@ -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"
+    }
+}
diff --git a/test/instance_static/emoji/test_pack_nonshared/pack.yml b/test/instance_static/emoji/test_pack_nonshared/pack.yml
deleted file mode 100644 (file)
index 45c3404..0000000
+++ /dev/null
@@ -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
index 13a34d38d201caa27b3381fef85f826d467345aa..bf56c1516af35c01d1bb397b6e51b5c363f7649f 100644 (file)
@@ -38,7 +38,7 @@ defmodule Pleroma.Web.EmojiAPI.EmojiAPIControllerTest 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 @@ defmodule Pleroma.Web.EmojiAPI.EmojiAPIControllerTest do
            )
            |> 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