Current section
4 Versions
Jump to
Current section
4 Versions
Compare versions
9
files changed
+81
additions
-98
deletions
| @@ -7,8 +7,8 @@ This module is community-made and is **NOT an official Google project**. | |
| 7 7 | ## Prerequisites |
| 8 8 | |
| 9 9 | - Elixir 1.18+ |
| 10 | - - Erlang/OTP 26+ |
| 11 | - - Rust 1.92.0+ |
| 10 | + - Erlang/OTP 28+ |
| 11 | + - Rust 1.96.0+ |
| 12 12 | |
| 13 13 | ## Installation |
| @@ -1,21 +1,20 @@ | |
| 1 | - {<<"links">>,[{<<"SourceHut">>,<<"https://git.sr.ht/~err931/magika_ex">>}]}. |
| 1 | + {<<"links">>,[{<<"GitHub">>,<<"https://github.com/err931/magika_ex">>}]}. |
| 2 2 | {<<"name">>,<<"magika_ex">>}. |
| 3 | - {<<"version">>,<<"0.1.2">>}. |
| 3 | + {<<"version">>,<<"0.1.3">>}. |
| 4 4 | {<<"description">>,<<"Elixir NIF wrapper for google/magika">>}. |
| 5 5 | {<<"elixir">>,<<"~> 1.18">>}. |
| 6 6 | {<<"app">>,<<"magika_ex">>}. |
| 7 7 | {<<"licenses">>,[<<"Apache-2.0">>]}. |
| 8 8 | {<<"files">>, |
| 9 | - [<<"lib">>,<<"lib/magika_ex.ex">>,<<"lib/magika_ex">>, |
| 10 | - <<"lib/magika_ex/application.ex">>,<<"lib/magika_ex/native.ex">>, |
| 11 | - <<"native">>,<<"native/magika_nif">>,<<"native/magika_nif/Cargo.toml">>, |
| 12 | - <<"native/magika_nif/README.md">>,<<"native/magika_nif/src">>, |
| 13 | - <<"native/magika_nif/src/lib.rs">>,<<".formatter.exs">>,<<"mix.exs">>, |
| 14 | - <<"README.md">>,<<"LICENSE">>]}. |
| 9 | + [<<"lib">>,<<"lib/magika_ex">>,<<"lib/magika_ex/native.ex">>, |
| 10 | + <<"lib/magika_ex/application.ex">>,<<"lib/magika_ex.ex">>,<<"native">>, |
| 11 | + <<"native/magika_nif">>,<<"native/magika_nif/Cargo.toml">>, |
| 12 | + <<"native/magika_nif/src">>,<<"native/magika_nif/src/lib.rs">>, |
| 13 | + <<".formatter.exs">>,<<"mix.exs">>,<<"README.md">>,<<"LICENSE">>]}. |
| 15 14 | {<<"requirements">>, |
| 16 15 | [[{<<"name">>,<<"rustler">>}, |
| 17 16 | {<<"app">>,<<"rustler">>}, |
| 18 17 | {<<"optional">>,false}, |
| 19 | - {<<"requirement">>,<<"~> 0.37.1">>}, |
| 18 | + {<<"requirement">>,<<"~> 0.38.0">>}, |
| 20 19 | {<<"repository">>,<<"hexpm">>}]]}. |
| 21 20 | {<<"build_tools">>,[<<"mix">>]}. |
| @@ -7,16 +7,14 @@ defmodule MagikaEx do | |
| 7 7 | classify file contents in an efficient and convenient way. |
| 8 8 | """ |
| 9 9 | |
| 10 | - use GenServer |
| 11 | - |
| 12 10 | alias MagikaEx.Native |
| 13 11 | |
| 14 | - @type option :: GenServer.option() |
| 15 | - |
| 16 | - @spec start_link([option()]) :: GenServer.on_start() |
| 17 | - def start_link(opts \\ []) do |
| 18 | - opts = Keyword.put_new(opts, :name, __MODULE__) |
| 19 | - GenServer.start_link(__MODULE__, :ok, opts) |
| 12 | + @doc """ |
| 13 | + Returns the name of the Magika model currently in use. |
| 14 | + """ |
| 15 | + @spec model_name() :: String.t() |
| 16 | + def model_name do |
| 17 | + Native.model_name() |
| 20 18 | end |
| 21 19 | |
| 22 20 | @doc """ |
| @@ -24,8 +22,8 @@ defmodule MagikaEx do | |
| 24 22 | """ |
| 25 23 | @spec identify_bytes(binary()) :: {:ok, MagikaEx.Result.t()} | {:error, term} |
| 26 24 | def identify_bytes(data) when is_binary(data) do |
| 27 | - resource = GenServer.call(__MODULE__, :get_resource) |
| 28 | - {:ok, Native.identify_bytes(resource, data)} |
| 25 | + resource = :persistent_term.get({__MODULE__, :resource}) |
| 26 | + Native.identify_bytes(resource, data) |
| 29 27 | end |
| 30 28 | |
| 31 29 | @doc """ |
| @@ -33,23 +31,13 @@ defmodule MagikaEx do | |
| 33 31 | """ |
| 34 32 | @spec identify_path(String.t()) :: {:ok, MagikaEx.Result.t()} | {:error, term} |
| 35 33 | def identify_path(path) when is_binary(path) do |
| 36 | - with {:ok, binary} <- File.read(path) do |
| 37 | - identify_bytes(binary) |
| 34 | + if File.exists?(path) do |
| 35 | + resource = :persistent_term.get({__MODULE__, :resource}) |
| 36 | + Native.identify_path(resource, path) |
| 37 | + else |
| 38 | + {:error, :enoent} |
| 38 39 | end |
| 39 40 | end |
| 40 | - |
| 41 | - @impl true |
| 42 | - def init(:ok) do |
| 43 | - case Native.new() do |
| 44 | - resource when is_reference(resource) -> |
| 45 | - {:ok, resource} |
| 46 | - end |
| 47 | - end |
| 48 | - |
| 49 | - @impl true |
| 50 | - def handle_call(:get_resource, _from, resource) do |
| 51 | - {:reply, resource, resource} |
| 52 | - end |
| 53 41 | end |
| 54 42 | |
| 55 43 | defmodule MagikaEx.Result do |
| @@ -5,11 +5,9 @@ defmodule MagikaEx.Application do | |
| 5 5 | |
| 6 6 | @impl true |
| 7 7 | def start(_type, _args) do |
| 8 | - children = [ |
| 9 | - {MagikaEx, name: MagikaEx} |
| 10 | - ] |
| 8 | + {:ok, resource} = MagikaEx.Native.new() |
| 9 | + :persistent_term.put({MagikaEx, :resource}, resource) |
| 11 10 | |
| 12 | - opts = [strategy: :one_for_one, name: MagikaEx.Supervisor] |
| 13 | - Supervisor.start_link(children, opts) |
| 11 | + Supervisor.start_link([], strategy: :one_for_one, name: MagikaEx.Supervisor) |
| 14 12 | end |
| 15 13 | end |
| @@ -4,5 +4,7 @@ defmodule MagikaEx.Native do | |
| 4 4 | use Rustler, otp_app: :magika_ex, crate: "magika_nif" |
| 5 5 | |
| 6 6 | def new, do: :erlang.nif_error(:nif_not_loaded) |
| 7 | + def model_name, do: :erlang.nif_error(:nif_not_loaded) |
| 7 8 | def identify_bytes(_resource, _data), do: :erlang.nif_error(:nif_not_loaded) |
| 9 | + def identify_path(_resource, _path), do: :erlang.nif_error(:nif_not_loaded) |
| 8 10 | end |
Loading more files…