Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env ruby
# gem install ruby-filemagic
# please note that this program works only on Unix based systems and Linux.
# It uses the "file command" library (and information) in a native code to validate file content (rather then extension).
# Important note: It only read the *header* of the files rather the whole, content so we might still have malicious content ... :(
require 'rubygems'
require 'filemagic'
# I use it to detect if the audio files are valid for Asterisk
# This supports only wav and mp3 at the moment. I can not detect gsm files it returns "data", so no support will be added.
def valid_audio?(file)
fm = FileMagic.new
magic = fm.file(file).upcase
fm.close # free memory, specially if we are using a daemon or webapp ...
# wav file
return true if ((magic.include? 'WAVE AUDIO' ) && (magic.include? 'MONO') && (magic.include? '8000 HZ')) ||
# mp3
((magic.include? 'MPEG') && (magic.include? 'LAYER III') && (magic.include? '8 KHZ'))
false
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.