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.file;
35
36 import java.io.File;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.util.zip.ZipEntry;
42 import java.util.zip.ZipOutputStream;
43
44 import org.apache.commons.lang3.StringUtils;
45
46 import fr.paris.lutece.portal.service.util.AppLogService;
47 import fr.paris.lutece.portal.service.util.AppPropertiesService;
48 import fr.paris.lutece.util.string.StringUtil;
49
50
51
52
53 public final class FileUtil
54 {
55 public static final String CONSTANT_MIME_TYPE_ZIP = "application/zip";
56 public static final String CONSTANT_MIME_TYPE_CSV = "application/csv";
57 public static final String EXTENSION_ZIP = ".zip";
58 public static final String EXTENSION_CSV = ".csv";
59 private static final String PROPERTY_ALLOWED_IMAGES_EXTENSIONS = "lutece.files.allowedImagesExtentions";
60 private static final String PROPERTY_ALLOWED_HTML_EXTENSIONS = "lutece.files.allowedHtmlExtentions";
61 private static final String DEFAULT_IMAGES_EXTENSION = "webp,png,jpg,jpeg,svg";
62 private static final String DEFAULT_HTML_EXTENSION = "html,htm,xhtml";
63 private static final String FREEMARKER_EXTENSION = "ftl";
64 private static final String CONSTANT_POINT = ".";
65 private static final String CONSTANT_COMMA = ",";
66
67
68
69
70 private FileUtil( )
71 {
72 }
73
74
75
76
77
78
79
80
81 public static boolean hasImageExtension( String strImageFileName )
82 {
83 String strImagesExtentions = AppPropertiesService.getProperty( PROPERTY_ALLOWED_IMAGES_EXTENSIONS, DEFAULT_IMAGES_EXTENSION );
84
85 return hasExtension( strImageFileName, strImagesExtentions );
86 }
87
88
89
90
91
92
93
94
95 public static boolean hasHtmlExtension( String strFileName )
96 {
97 String strImagesExtentions = AppPropertiesService.getProperty( PROPERTY_ALLOWED_HTML_EXTENSIONS, DEFAULT_HTML_EXTENSION );
98
99 return hasExtension( strFileName, strImagesExtentions );
100 }
101
102
103
104
105
106
107
108
109 public static boolean hasFreemarkerExtension( String strFileName )
110 {
111 return hasExtension( strFileName, FREEMARKER_EXTENSION );
112 }
113
114
115
116
117
118
119
120
121
122
123 private static boolean hasExtension( String strFileName, String strAllowedExtensions )
124 {
125 int nIndex = strFileName.lastIndexOf( CONSTANT_POINT );
126
127 if ( ( nIndex >= 0 ) && ( strFileName.length( ) > ( nIndex + 2 ) ) )
128 {
129 String strExtension = strFileName.substring( nIndex + 1 );
130
131 if ( StringUtils.isNotEmpty( strExtension ) )
132 {
133 for ( String strAllowedExtention : strAllowedExtensions.split( CONSTANT_COMMA ) )
134 {
135 if ( StringUtils.equalsIgnoreCase( strExtension, strAllowedExtention ) )
136 {
137 return true;
138 }
139 }
140 }
141 }
142
143 return false;
144 }
145
146
147
148
149
150
151
152
153
154
155 public static void zipFiles( Path zipFile, Path... paths ) throws IOException
156 {
157 if ( zipFile.toFile( ).exists( ) )
158 {
159 deleteFile( zipFile.toFile( ) );
160 }
161
162 try ( ZipOutputStream zos = new ZipOutputStream( Files.newOutputStream( zipFile ) ) )
163 {
164 for ( Path file : paths )
165 {
166 addEntryToZip( zos, file );
167 }
168 }
169 }
170
171 private static void addEntryToZip( ZipOutputStream zos, Path file ) throws IOException
172 {
173 try ( InputStream fis = Files.newInputStream( file ) )
174 {
175 ZipEntry zipEntry = new ZipEntry( file.toFile( ).getName( ) );
176 zos.putNextEntry( zipEntry );
177
178 byte [ ] bytes = new byte [ 1024];
179 int length;
180 while ( ( length = fis.read( bytes ) ) >= 0 )
181 {
182 zos.write( bytes, 0, length );
183 }
184 zos.closeEntry( );
185 }
186 }
187
188
189
190
191
192
193
194
195
196
197 public static final String normalizeFileName( String string )
198 {
199 return StringUtil.replaceAccent( string ).replace( ' ', '_' ).replaceAll( "[^a-zA-Z0-9_]+", "" ).toLowerCase( );
200 }
201
202
203
204
205
206
207
208 public static void deleteFile( File file )
209 {
210 if ( file == null )
211 {
212 AppLogService.error( "Error deleting file, file null" );
213 return;
214 }
215 try
216 {
217 if ( file.exists( ) )
218 {
219 Files.delete( file.toPath( ) );
220 }
221 }
222 catch( IOException e )
223 {
224 AppLogService.error( "Error deleting file", e );
225 }
226 }
227
228 }