FileSystemUtil.java

  1. /*
  2.  * Copyright (c) 2002-2022, City of Paris
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  *  1. Redistributions of source code must retain the above copyright notice
  10.  *     and the following disclaimer.
  11.  *
  12.  *  2. Redistributions in binary form must reproduce the above copyright notice
  13.  *     and the following disclaimer in the documentation and/or other materials
  14.  *     provided with the distribution.
  15.  *
  16.  *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
  17.  *     contributors may be used to endorse or promote products derived from
  18.  *     this software without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
  24.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30.  * POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  * License 1.0
  33.  */
  34. package fr.paris.lutece.util.filesystem;

  35. import fr.paris.lutece.portal.service.util.AppLogService;
  36. import fr.paris.lutece.portal.service.util.AppPathService;

  37. // Java IO
  38. import java.io.File;
  39. import java.io.IOException;

  40. // Java Util
  41. import java.util.ArrayList;
  42. import java.util.List;
  43. import java.util.TreeSet;

  44. import javax.activation.MimetypesFileTypeMap;

  45. import org.apache.commons.lang3.ArrayUtils;

  46. /**
  47.  * This Service is used to manipulate Files and Directories in the File System.
  48.  */
  49. public final class FileSystemUtil
  50. {
  51.     private static final String DEFAULT_MIME_TYPE = "application/octet-stream";
  52.     private static final String FILE_SEPARATOR = File.separator;
  53.     private static final String FILE_MIME_TYPE = "WEB-INF" + FILE_SEPARATOR + "conf" + FILE_SEPARATOR + "mime.types";

  54.     /**
  55.      * Private constructor
  56.      */
  57.     private FileSystemUtil( )
  58.     {
  59.     }

  60.     /**
  61.      * Returns the sub-directories of a directory.
  62.      *
  63.      * @param strRoot
  64.      *            the root directory.
  65.      * @param strDirectoryRelativePath
  66.      *            the parent directory.
  67.      * @throws DirectoryNotFoundException
  68.      *             if the directory does not exist.
  69.      * @return The list of sub-directories.
  70.      */
  71.     public static List<File> getSubDirectories( String strRoot, String strDirectoryRelativePath ) throws DirectoryNotFoundException
  72.     {
  73.         // Files' list init
  74.         ArrayList<File> listFiles = new ArrayList<>( );

  75.         // Directory path
  76.         String strDirectory = strRoot + strDirectoryRelativePath;

  77.         // Directory
  78.         File fDirectory = new File( strDirectory );

  79.         if ( !fDirectory.exists( ) )
  80.         {
  81.             throw new DirectoryNotFoundException( );
  82.         }

  83.         File [ ] files = fDirectory.listFiles( );
  84.         if ( ArrayUtils.isEmpty( files ) )
  85.         {
  86.             return listFiles;
  87.         }

  88.         for ( int i = 0; i < files.length; i++ )
  89.         {
  90.             File file = files [i];

  91.             if ( file.isDirectory( ) )
  92.             {
  93.                 listFiles.add( file );
  94.             }
  95.         }

  96.         return listFiles;
  97.     }

  98.     /**
  99.      * Returns the files of a directory, alphabetically ordered.
  100.      *
  101.      * @param strRoot
  102.      *            The root directory
  103.      * @param strDirectoryRelativePath
  104.      *            The directory's path relative to the root directory.
  105.      * @throws DirectoryNotFoundException
  106.      *             if the directory does not exist.
  107.      * @return The list of files.
  108.      */
  109.     public static List<File> getFiles( String strRoot, String strDirectoryRelativePath ) throws DirectoryNotFoundException
  110.     {
  111.         // Use a treeset to order files with a comparator
  112.         TreeSet<File> set = new TreeSet<>( new FileNameComparator( ) );

  113.         // Directory path
  114.         String strDirectory = strRoot + strDirectoryRelativePath;

  115.         // Directory
  116.         File fDirectory = new File( strDirectory );

  117.         if ( !fDirectory.exists( ) )
  118.         {
  119.             throw new DirectoryNotFoundException( );
  120.         }

  121.         File [ ] files = fDirectory.listFiles( );
  122.         if ( ArrayUtils.isEmpty( files ) )
  123.         {
  124.             return new ArrayList<>( set );
  125.         }

  126.         for ( int i = 0; i < files.length; i++ )
  127.         {
  128.             File file = files [i];

  129.             if ( file.isFile( ) )
  130.             {
  131.                 set.add( file );
  132.             }
  133.         }

  134.         // Convert into a list to preserve the order
  135.         return new ArrayList<>( set );
  136.     }

  137.     /**
  138.      * Return the mimetype of the file depending of his extension and the mime.types file
  139.      *
  140.      * @param strFilename
  141.      *            the file name
  142.      * @return the file mime type
  143.      */
  144.     public static String getMIMEType( String strFilename )
  145.     {
  146.         try
  147.         {
  148.             MimetypesFileTypeMap mimeTypeMap = new MimetypesFileTypeMap( AppPathService.getWebAppPath( ) + File.separator + FILE_MIME_TYPE );

  149.             return mimeTypeMap.getContentType( strFilename.toLowerCase( ) );
  150.         }
  151.         catch( IOException e )
  152.         {
  153.             AppLogService.error( e.getMessage( ), e );

  154.             return DEFAULT_MIME_TYPE;
  155.         }
  156.     }
  157. }