LCOV - code coverage report
Current view: top level - edhttp - mime_type.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 7 0.0 %
Date: 2022-07-09 10:44:38 Functions: 0 1 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2011-2022  Made to Order Software Corp.  All Rights Reserved
       2             : //
       3             : // https://snapwebsites.org/project/edhttp
       4             : // contact@m2osw.com
       5             : //
       6             : // This program is free software: you can redistribute it and/or modify
       7             : // it under the terms of the GNU General Public License as published by
       8             : // the Free Software Foundation, either version 3 of the License, or
       9             : // (at your option) any later version.
      10             : //
      11             : // This program is distributed in the hope that it will be useful,
      12             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             : // GNU General Public License for more details.
      15             : //
      16             : // You should have received a copy of the GNU General Public License
      17             : // along with this program.  If not, see <https://www.gnu.org/licenses/>.
      18             : 
      19             : // self
      20             : //
      21             : #include    "edhttp/mime_type.h"
      22             : 
      23             : #include    "edhttp/exception.h"
      24             : 
      25             : 
      26             : // C lib
      27             : //
      28             : #include    <magic.h>
      29             : 
      30             : 
      31             : // last include
      32             : //
      33             : #include    <snapdev/poison.h>
      34             : 
      35             : 
      36             : 
      37             : namespace edhttp
      38             : {
      39             : 
      40             : namespace
      41             : {
      42             : magic_t         g_magic = nullptr;
      43             : }
      44             : 
      45             : 
      46             : /** \brief Generate a MIME type from a buffer.
      47             :  *
      48             :  * This function determines the MIME type of a buffer (std::string)
      49             :  * using the magic library. The magic library is likely to
      50             :  * understand all the files that a user is to upload on a
      51             :  * website (within reason, of course).
      52             :  *
      53             :  * This function runs against the specified buffer (\p data) from
      54             :  * memory.
      55             :  *
      56             :  * The function returns the computed MIME type such as text/html or
      57             :  * image/png.
      58             :  *
      59             :  * \note
      60             :  * Compressed files get their type determined after decompression.
      61             :  *
      62             :  * \exception mime_type_no_magic
      63             :  * The function generates an exception if it cannot access the
      64             :  * magic library (the magic_open() function fails). If the magic
      65             :  * library is available but the type of the buffer can't be
      66             :  * determine, then a default MIME type is returned.
      67             :  *
      68             :  * \param[in] data  The buffer to be transformed in a MIME type.
      69             :  *
      70             :  * \return The MIME type of the input buffer.
      71             :  */
      72           0 : std::string get_mime_type(std::string const & data)
      73             : {
      74           0 :     if(g_magic == nullptr)
      75             :     {
      76           0 :         g_magic = magic_open(MAGIC_COMPRESS | MAGIC_MIME);
      77           0 :         if(g_magic == nullptr)
      78             :         {
      79           0 :             throw mime_type_no_magic("Magic MIME type cannot be opened (magic_open() failed)");
      80             :         }
      81             : 
      82             :         // load the default magic database
      83             :         //
      84           0 :         magic_load(g_magic, nullptr);
      85             :     }
      86             : 
      87           0 :     return magic_buffer(g_magic, data.data(), data.length());
      88             : }
      89             : 
      90             : 
      91             : 
      92             : } // namespace edhttp
      93             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13