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
51
52
53
54 public final class FileSystemUtil
55 {
56 private static final String DEFAULT_MIME_TYPE = "application/octet-stream";
57 private static final String FILE_SEPARATOR = File.separator;
58 private static final String FILE_MIME_TYPE = "WEB-INF" + FILE_SEPARATOR + "conf" + FILE_SEPARATOR + "mime.types";
59
60
61
62
63 private FileSystemUtil( )
64 {
65 }
66
67
68
69
70
71
72
73
74
75 public static List<File> getSubDirectories( String strRoot, String strDirectoryRelativePath )
76 throws DirectoryNotFoundException
77 {
78
79 ArrayList<File> listFiles = new ArrayList<File>( );
80
81
82 String strDirectory = strRoot + strDirectoryRelativePath;
83
84
85 File fDirectory = new File( strDirectory );
86
87 if ( !fDirectory.exists( ) )
88 {
89 throw new DirectoryNotFoundException( );
90 }
91
92 File[] files = fDirectory.listFiles( );
93
94 for ( int i = 0; i < files.length; i++ )
95 {
96 File file = files[i];
97
98 if ( file.isDirectory( ) )
99 {
100 listFiles.add( file );
101 }
102 }
103
104 return listFiles;
105 }
106
107
108
109
110
111
112
113
114
115 public static List<File> getFiles( String strRoot, String strDirectoryRelativePath )
116 throws DirectoryNotFoundException
117 {
118
119 TreeSet<File> set = new TreeSet<File>( new FileNameComparator( ) );
120
121
122 String strDirectory = strRoot + strDirectoryRelativePath;
123
124
125 File fDirectory = new File( strDirectory );
126
127 if ( !fDirectory.exists( ) )
128 {
129 throw new DirectoryNotFoundException( );
130 }
131
132 File[] files = fDirectory.listFiles( );
133
134 for ( int i = 0; i < files.length; i++ )
135 {
136 File file = files[i];
137
138 if ( file.isFile( ) )
139 {
140 set.add( file );
141 }
142 }
143
144
145 return new ArrayList<File>( set );
146 }
147
148
149
150
151
152
153 public static String getMIMEType( String strFilename )
154 {
155 try
156 {
157 MimetypesFileTypeMap mimeTypeMap = new MimetypesFileTypeMap( AppPathService.getWebAppPath( ) +
158 File.separator + FILE_MIME_TYPE );
159
160 return mimeTypeMap.getContentType( strFilename.toLowerCase( ) );
161 }
162 catch ( IOException e )
163 {
164 AppLogService.error( e );
165
166 return DEFAULT_MIME_TYPE;
167 }
168 }
169 }