1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.util.filesystem;
35
36 import fr.paris.lutece.portal.service.util.AppLogService;
37 import fr.paris.lutece.portal.service.util.AppPathService;
38
39
40 import java.io.File;
41 import java.io.IOException;
42
43
44 import java.util.ArrayList;
45 import java.util.List;
46 import java.util.TreeSet;
47
48 import javax.activation.MimetypesFileTypeMap;
49
50 import org.apache.commons.lang3.ArrayUtils;
51
52
53
54
55 public final class FileSystemUtil
56 {
57 private static final String DEFAULT_MIME_TYPE = "application/octet-stream";
58 private static final String FILE_SEPARATOR = File.separator;
59 private static final String FILE_MIME_TYPE = "WEB-INF" + FILE_SEPARATOR + "conf" + FILE_SEPARATOR + "mime.types";
60
61
62
63
64 private FileSystemUtil( )
65 {
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79 public static List<File> getSubDirectories( String strRoot, String strDirectoryRelativePath ) throws DirectoryNotFoundException
80 {
81
82 ArrayList<File> listFiles = new ArrayList<>( );
83
84
85 String strDirectory = strRoot + strDirectoryRelativePath;
86
87
88 File fDirectory = new File( strDirectory );
89
90 if ( !fDirectory.exists( ) )
91 {
92 throw new DirectoryNotFoundException( );
93 }
94
95 File [ ] files = fDirectory.listFiles( );
96 if ( ArrayUtils.isEmpty( files ) )
97 {
98 return listFiles;
99 }
100
101 for ( int i = 0; i < files.length; i++ )
102 {
103 File file = files [i];
104
105 if ( file.isDirectory( ) )
106 {
107 listFiles.add( file );
108 }
109 }
110
111 return listFiles;
112 }
113
114
115
116
117
118
119
120
121
122
123
124
125 public static List<File> getFiles( String strRoot, String strDirectoryRelativePath ) throws DirectoryNotFoundException
126 {
127
128 TreeSet<File> set = new TreeSet<>( new FileNameComparator( ) );
129
130
131 String strDirectory = strRoot + strDirectoryRelativePath;
132
133
134 File fDirectory = new File( strDirectory );
135
136 if ( !fDirectory.exists( ) )
137 {
138 throw new DirectoryNotFoundException( );
139 }
140
141 File [ ] files = fDirectory.listFiles( );
142 if ( ArrayUtils.isEmpty( files ) )
143 {
144 return new ArrayList<>( set );
145 }
146
147 for ( int i = 0; i < files.length; i++ )
148 {
149 File file = files [i];
150
151 if ( file.isFile( ) )
152 {
153 set.add( file );
154 }
155 }
156
157
158 return new ArrayList<>( set );
159 }
160
161
162
163
164
165
166
167
168 public static String getMIMEType( String strFilename )
169 {
170 try
171 {
172 MimetypesFileTypeMap mimeTypeMap = new MimetypesFileTypeMap( AppPathService.getWebAppPath( ) + File.separator + FILE_MIME_TYPE );
173
174 return mimeTypeMap.getContentType( strFilename.toLowerCase( ) );
175 }
176 catch( IOException e )
177 {
178 AppLogService.error( e.getMessage( ), e );
179
180 return DEFAULT_MIME_TYPE;
181 }
182 }
183 }