LCOV - code coverage report
Current view: top level - snapwebsites - snap_magic.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 7 0.0 %
Date: 2019-12-15 17:13:15 Functions: 0 1 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Snap Websites Servers -- check the MIME type of a file using the magic database
       2             : // Copyright (c) 2013-2019  Made to Order Software Corp.  All Rights Reserved
       3             : //
       4             : // This program is free software; you can redistribute it and/or modify
       5             : // it under the terms of the GNU General Public License as published by
       6             : // the Free Software Foundation; either version 2 of the License, or
       7             : // (at your option) any later version.
       8             : //
       9             : // This program is distributed in the hope that it will be useful,
      10             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             : // GNU General Public License for more details.
      13             : //
      14             : // You should have received a copy of the GNU General Public License
      15             : // along with this program; if not, write to the Free Software
      16             : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
      17             : 
      18             : 
      19             : // self
      20             : //
      21             : #include "snapwebsites/snap_magic.h"
      22             : 
      23             : 
      24             : // C lib
      25             : //
      26             : #include <magic.h>
      27             : 
      28             : 
      29             : // last include
      30             : //
      31             : #include <snapdev/poison.h>
      32             : 
      33             : 
      34             : 
      35             : 
      36             : 
      37             : namespace snap
      38             : {
      39             : 
      40             : namespace
      41             : {
      42             : magic_t         g_magic = NULL;
      43             : }
      44             : 
      45             : 
      46             : /** \brief Generate a MIME type from a QByteArray.
      47             :  *
      48             :  * This static function transforms the content of a QByteArray in a
      49             :  * MIME type using the magic library. The magic library is likely to
      50             :  * understand all the files that a user is to upload on a Snap!
      51             :  * website (within reason, of course.)
      52             :  *
      53             :  * This function runs against the specified buffer (\p data) from
      54             :  * memory since in Snap! we do pretty much everything in memory.
      55             :  *
      56             :  * The function returns computer MIME types such as text/html or
      57             :  * image/png.
      58             :  *
      59             :  * \note
      60             :  * Compressed files get their type determined after decompression.
      61             :  *
      62             :  * \exception snap_magic_exception_no_magic
      63             :  * The function generates an exception if it cannot access the
      64             :  * magic library (the magic_open() function fails.)
      65             :  *
      66             :  * \param[in] data  The buffer to be transformed in a MIME type.
      67             :  */
      68           0 : QString get_mime_type(const QByteArray& data)
      69             : {
      70           0 :     if(g_magic == NULL)
      71             :     {
      72           0 :         g_magic = magic_open(MAGIC_COMPRESS | MAGIC_MIME);
      73           0 :         if(g_magic == NULL)
      74             :         {
      75           0 :             throw snap_magic_exception_no_magic("Magic MIME type cannot be opened (magic_open() failed)");
      76             :         }
      77             :         // load the default magic database
      78           0 :         magic_load(g_magic, NULL);
      79             :     }
      80             : 
      81           0 :     return magic_buffer(g_magic, data.data(), data.length());
      82             : }
      83             : 
      84             : }
      85             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13