From 65bf0c0c8a5543a235b3969dbbc09872c69a515c Mon Sep 17 00:00:00 2001 From: Chris McCord Date: Sun, 31 Oct 2021 21:20:04 -0400 Subject: [PATCH] Uploads WIP --- lib/live_beats/id3.ex | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 lib/live_beats/id3.ex diff --git a/lib/live_beats/id3.ex b/lib/live_beats/id3.ex new file mode 100644 index 0000000..d78f99b --- /dev/null +++ b/lib/live_beats/id3.ex @@ -0,0 +1,38 @@ +defmodule LiveBeats.ID3 do + alias LiveBeats.ID3 + + defstruct title: nil, + artist: nil, + album: nil, + year: nil + + def parse(path) do + binary = File.read!(path) + size = byte_size(binary) - 128 + <<_::binary-size(size), id3_tag::binary>> = binary + + case id3_tag do + << + "TAG", + title::binary-size(30), + artist::binary-size(30), + album::binary-size(30), + year::binary-size(4), + _comment::binary-size(30), + _rest::binary + >> -> + {:ok, + %ID3{ + title: strip(title), + artist: strip(artist), + album: strip(album), + year: year + }} + + _invalid -> + {:error, :invalid} + end + end + + defp strip(binary), do: String.trim_trailing(binary, <<0>>) +end