Rainer

Rainer

Elegant way to check filetypes?

Hi
Recently I wanted to check if a File is a Picture, and I don’t like checking just the extension.
So with the help of this:


I came to the following solution:
  def is_picture(file) do
    {_, content} = :file.open(file, [:read, :binary])
    fileHeader = :file.read(content, 8)
    :file.close(content)
    case fileHeader do
      {:ok, <<0xFF, 0xD8, _, _, _, _, _, _>>} -> true #jpg
      {:ok, <<0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A>>} -> true #png
      {:ok, <<0x47, 0x49, 0x46, 0x38, 0x37, 0x61, _, _>>} -> true #gif standard
      {:ok, <<0x47, 0x49, 0x46, 0x38, 0x39, 0x61, _, _>>} -> true #gif animated
      _ -> false
    end
  end

As I’m still not experienced with Elixir, I wonder how would you write such a function in an elegant way?

Most Liked

ityonemo

ityonemo

Assuming you don’t care about false positives and strictly don’t care about corrupted files, looks good. I would advocate using elixir File module (so you don’t have to open, you can go straight to read) instead of erlang :file module and maybe consider putting it in a with statement. Closing the file I’d put either in an after block, or possibly even not bother (when the calling process dies, the file descriptor will be reclaimed)

kokolegorille

kokolegorille

Maybe this package can help, if You are on Linux.

Rainer

Rainer

Thanks for the replies :slight_smile:

I didn’t know about the with statement until now :smiley:

Haven’t thought about such a simple solution, also a good idea :stuck_out_tongue:

I’ll try to improve my function with your suggestions :+1:t3:

Where Next?

Popular in Questions Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New

Other popular topics Top

William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement