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?
Trending in Questions
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
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
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
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
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kokolegorille
Maybe this package can help, if You are on Linux.
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
withstatement. Closing the file I’d put either in anafterblock, or possibly even not bother (when the calling process dies, the file descriptor will be reclaimed)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”.
voughtdq
This is one of those things where I throw my hands up and go
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
Thanks for the replies
I didn’t know about the with statement until now
Haven’t thought about such a simple solution, also a good idea
I’ll try to improve my function with your suggestions