Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 61 additions & 18 deletions src/preloaded/arweave/dev_arweave_offset.erl
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,32 @@ parse(Key) ->

%% @doc Parses and applies a unit modifier to a base value, supporting both
%% the `kb` and `kib` unit formats.
unit(Binary) -> unit(0, Binary).
unit(Binary) ->
UnitWithoutOptionalB =
case Binary of
<<Prefix:(byte_size(Binary) - 1)/binary, "b">> -> Prefix;
_ -> Binary
end,
unit(0, UnitWithoutOptionalB).
unit(Complete, <<>>) -> Complete;
unit(Base, <<Int:8/integer, Rest/binary>>) when Int >= $0 andalso Int =< $9 ->
unit(Base * 10 + (Int - $0), Rest);
unit(Base, <<"b">>) -> Base;
unit(Base, <<"ki", _/binary>>) when Base > 0 -> unit(Base * 1024, <<"b">>);
unit(Base, <<"mi", _/binary>>) when Base > 0 -> unit(Base * 1024, <<"ki">>);
unit(Base, <<"gi", _/binary>>) when Base > 0 -> unit(Base * 1024, <<"mi">>);
unit(Base, <<"ti", _/binary>>) when Base > 0 -> unit(Base * 1024, <<"gi">>);
unit(Base, <<"pi", _/binary>>) when Base > 0 -> unit(Base * 1024, <<"ti">>);
unit(Base, <<"ei", _/binary>>) when Base > 0 -> unit(Base * 1024, <<"pi">>);
unit(Base, <<"zi", _/binary>>) when Base > 0 -> unit(Base * 1024, <<"ei">>);
unit(Base, <<"yi", _/binary>>) when Base > 0 -> unit(Base * 1024, <<"zi">>);
unit(Base, <<"k", _/binary>>) when Base > 0 -> unit(Base * 1000, <<"b">>);
unit(Base, <<"m", _/binary>>) when Base > 0 -> unit(Base * 1000, <<"k">>);
unit(Base, <<"g", _/binary>>) when Base > 0 -> unit(Base * 1000, <<"m">>);
unit(Base, <<"t", _/binary>>) when Base > 0 -> unit(Base * 1000, <<"g">>);
unit(Base, <<"p", _/binary>>) when Base > 0 -> unit(Base * 1000, <<"t">>);
unit(Base, <<"e", _/binary>>) when Base > 0 -> unit(Base * 1000, <<"p">>);
unit(Base, <<"z", _/binary>>) when Base > 0 -> unit(Base * 1000, <<"e">>);
unit(Base, <<"y", _/binary>>) when Base > 0 -> unit(Base * 1000, <<"z">>).
unit(Base, <<"ki">>) when Base > 0 -> unit(Base * 1024, <<>>);
unit(Base, <<"mi">>) when Base > 0 -> unit(Base * 1024, <<"ki">>);
unit(Base, <<"gi">>) when Base > 0 -> unit(Base * 1024, <<"mi">>);
unit(Base, <<"ti">>) when Base > 0 -> unit(Base * 1024, <<"gi">>);
unit(Base, <<"pi">>) when Base > 0 -> unit(Base * 1024, <<"ti">>);
unit(Base, <<"ei">>) when Base > 0 -> unit(Base * 1024, <<"pi">>);
unit(Base, <<"zi">>) when Base > 0 -> unit(Base * 1024, <<"ei">>);
unit(Base, <<"yi">>) when Base > 0 -> unit(Base * 1024, <<"zi">>);
unit(Base, <<"k">>) when Base > 0 -> unit(Base * 1000, <<>>);
unit(Base, <<"m">>) when Base > 0 -> unit(Base * 1000, <<"k">>);
unit(Base, <<"g">>) when Base > 0 -> unit(Base * 1000, <<"m">>);
unit(Base, <<"t">>) when Base > 0 -> unit(Base * 1000, <<"g">>);
unit(Base, <<"p">>) when Base > 0 -> unit(Base * 1000, <<"t">>);
unit(Base, <<"e">>) when Base > 0 -> unit(Base * 1000, <<"p">>);
unit(Base, <<"z">>) when Base > 0 -> unit(Base * 1000, <<"e">>);
unit(Base, <<"y">>) when Base > 0 -> unit(Base * 1000, <<"z">>).

%% @doc Load an ANS-104 item whose header begins at the given global offset.
%% When a length is supplied it is treated as the exact ANS-104 data length, so
Expand Down Expand Up @@ -354,6 +359,44 @@ parse_offset_test() ->
),
ok.

all_unit_forms_test() ->
?assertEqual({ok, 1, undefined}, parse(<<"1">>)),
?assertEqual({ok, 1, undefined}, parse(<<"1b">>)),
assert_unit_forms(
[
<<"k">>, <<"m">>, <<"g">>, <<"t">>,
<<"p">>, <<"e">>, <<"z">>, <<"y">>
],
1000
),
assert_unit_forms(
[
<<"ki">>, <<"mi">>, <<"gi">>, <<"ti">>,
<<"pi">>, <<"ei">>, <<"zi">>, <<"yi">>
],
1024
),
ok.

assert_unit_forms(Units, Multiple) ->
lists:foldl(
fun(Unit, Value) ->
Expected = Value * Multiple,
?assertEqual({ok, Expected, undefined}, parse(<<"1", Unit/binary>>)),
?assertEqual(
{ok, Expected, undefined},
parse(<<"1", Unit/binary, "b">>)
),
Expected
end,
1,
Units
).

partial_match_should_fail_test_parallel() ->
InvalidReference = <<"42tf43cjcfkwqydcjdk7ty2wbonkabw5acywyzk6shbd7x272zxq">>,
?assertEqual(error, parse(InvalidReference)).

offset_item_cases_test() ->
Opts = #{},
Png = #{ <<"content-type">> => <<"image/png">> },
Expand Down