Skip to content

Instantly share code, notes, and snippets.

@ik5
Created July 16, 2010 09:24
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ik5/478182 to your computer and use it in GitHub Desktop.
#!/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