Rainer

Rainer

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?

Showing Posts 1 to 5

kokolegorille

kokolegorille

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

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)

John-Goff

John-Goff

Your solution is basically the same as mine, but I have an additional check that the file extension is in the list of allowed file extensions, and then I also check that the magic bytes match the file extension given. It’s not a bulletproof solution but it’s “good enough”.

defmodule MyApp.FileValidation do
  @moduledoc """
  Checks if uploaded files are valid.

  Checks images for both filenames and magic bytes matching.
  """

  @extensions ~w(.jpg .jpeg .png)

  @doc """
  Checks if an image is valid.

  Only JPG/JPEG and PNG are valid files. Checks the file name of the uploaded file as well as the
  magic bytes of the image.
  """
  def image_valid?(file) do
    ext = Path.extname(file.file_name)

    Enum.member?(@extensions, ext) &&
      ((is_jpg?(file.path) && ext in ~w(.jpg .jpeg)) || (is_png?(file.path) && ext == ".png"))
  end

  @doc """
  Checks if a file is a JPG

  Checks the magic bytes. JPG magic bytes: 0xffd8
  """
  @spec is_jpg?(path :: String.t()) :: boolean
  def is_jpg?(path) do
    with {:ok, file_content} <- :file.open(path, [:read, :binary]),
         {:ok, <<255, 216>>} <- :file.read(file_content, 2) do
      true
    else
      _error ->
        false
    end
  end

  @doc """
  Checks if a file is a PNG

  Checks the magic bytes. PNG magic bytes: 0x89504e470d0a1a0a
  """
  @spec is_png?(path :: String.t()) :: boolean
  def is_png?(path) do
    with {:ok, file_content} <- :file.open(path, [:read, :binary]),
         {:ok, <<137, 80, 78, 71, 13, 10, 26, 10>>} <- :file.read(file_content, 8) do
      true
    else
      _error ->
        false
    end
  end
end
voughtdq

voughtdq

This is one of those things where I throw my hands up and go

System.cmd("/usr/bin/file", ["-i", file_path])

There’s just not really anything that does a better job. If I ever get the chance, I’d like to make it into a nif.

Rainer

Rainer OP

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:

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews