33
33
*/
34
34
package fr .paris .lutece .plugins .genericattributes .service .file ;
35
35
36
+ import java .util .HashMap ;
37
+ import java .util .List ;
38
+ import java .util .Map ;
39
+ import java .util .function .Function ;
40
+
41
+ import org .apache .commons .fileupload .FileItem ;
42
+ import org .apache .commons .lang3 .StringUtils ;
43
+
36
44
import fr .paris .lutece .portal .business .file .File ;
37
45
import fr .paris .lutece .portal .service .file .FileService ;
38
46
import fr .paris .lutece .portal .service .file .FileServiceException ;
39
47
import fr .paris .lutece .portal .service .file .IFileStoreServiceProvider ;
40
48
import fr .paris .lutece .portal .service .util .AppLogService ;
49
+ import fr .paris .lutece .portal .service .util .AppPropertiesService ;
41
50
42
51
public class GenericAttributeFileService
43
52
{
44
- private static final GenericAttributeFileService _instance = new GenericAttributeFileService ( ) ;
45
- private IFileStoreServiceProvider _fileStoreServiceProvider ;
53
+ private static final String PROPERTY_FILESTORESERVICE_PREFIX = "genericattributes.filestoreservice" ;
54
+ private static final String PROPERTY_FILESTORESERVICE_DEFAULT_SUFFIX = "default" ;
46
55
56
+ private static final GenericAttributeFileService _instance = new GenericAttributeFileService ( );
57
+ private static final Map <String ,String > _entryTypeFileServices = new HashMap <>();
58
+
59
+ /**
60
+ * Constructor
61
+ */
47
62
private GenericAttributeFileService ( )
48
63
{
64
+ List <String > keyList = AppPropertiesService .getKeys ( PROPERTY_FILESTORESERVICE_PREFIX );
65
+
66
+ // init specific entryType fileStoreService names if exists
67
+ keyList .stream ().forEach ( s -> {
68
+ if ( !StringUtils .isAllBlank ( AppPropertiesService .getProperty (s ) ) )
69
+ {
70
+ _entryTypeFileServices .put (s , AppPropertiesService .getProperty (s ));
71
+ }
72
+ } );
73
+ }
74
+
75
+ /**
76
+ * get FileStoreServiceProvider name for entry type :
77
+ * - returns the entry type FileService (if set)
78
+ * - otherwise, returns the GenAttr default FileService (if set)
79
+ * - otherwise, returns the lutece default FileService
80
+ *
81
+ * @param strEntryType
82
+ * @return the name of the FileStoreServiceProvider
83
+ */
84
+ public String getFileStoreProviderName ( String strEntryType )
85
+ {
86
+ if (strEntryType != null )
87
+ {
88
+ if ( _entryTypeFileServices .containsKey ( strEntryType ))
89
+ {
90
+ return _entryTypeFileServices .get ( strEntryType );
91
+ }
92
+ }
93
+
94
+ if ( _entryTypeFileServices .containsKey ( PROPERTY_FILESTORESERVICE_DEFAULT_SUFFIX ))
95
+ {
96
+ return _entryTypeFileServices .get ( PROPERTY_FILESTORESERVICE_DEFAULT_SUFFIX );
97
+ }
98
+
99
+ return FileService .getInstance ( ).getFileStoreServiceProvider ( ).getName ( );
100
+ }
101
+
102
+ /**
103
+ * get FileStoreServiceProvider name :
104
+ * - returns the GenAttr default FileService (if set)
105
+ * - otherwise, returns the lutece default FileService
106
+ *
107
+ * Use getFileStoreProviderName( String strEntryType ) to get entryType specific File service
108
+ *
109
+ * @return the name of the FileStoreServiceProvider
110
+ */
111
+ public String getFileStoreProviderName ( )
112
+ {
113
+ return getFileStoreProviderName ( null );
49
114
}
50
115
116
+ /**
117
+ * get instance of service
118
+ *
119
+ * @return the instance
120
+ */
51
121
public static GenericAttributeFileService getInstance ( )
52
122
{
53
123
return _instance ;
@@ -56,16 +126,30 @@ public static GenericAttributeFileService getInstance( )
56
126
/**
57
127
* Save a file
58
128
*
59
- * @param file
60
- * The file
129
+ * @param file The lutece file in default generic file Service
61
130
* @return The key of the file
62
131
*/
63
- public String save ( File file )
132
+ public String save ( File file )
133
+ {
134
+ return save ( file , null );
135
+ }
136
+ /**
137
+ * Save a file
138
+ *
139
+ * @param file The lutece file
140
+ * @param strEntryType the entry type
141
+ * @return The key of the file
142
+ */
143
+ public String save ( File file , String strEntryType )
64
144
{
65
145
try
66
146
{
67
- _fileStoreServiceProvider = FileService .getInstance ( ).getFileStoreServiceProvider ( file .getOrigin ( ) );
68
- return _fileStoreServiceProvider .storeFile ( file );
147
+ if ( file .getOrigin ( ) == null )
148
+ {
149
+ file .setOrigin ( getFileStoreProviderName ( strEntryType ) );
150
+ }
151
+
152
+ return FileService .getInstance ( ).getFileStoreServiceProvider ( file .getOrigin ( ) ).storeFile ( file );
69
153
}
70
154
catch ( FileServiceException e )
71
155
{
@@ -74,6 +158,26 @@ public String save( File file )
74
158
}
75
159
}
76
160
161
+ /**
162
+ * Save a file
163
+ *
164
+ * @param file The fileItem
165
+ * @param strEntryType the entry type
166
+ * @return The key of the file
167
+ */
168
+ public String save ( FileItem file , String strEntryType )
169
+ {
170
+ try
171
+ {
172
+ return FileService .getInstance ( ).getFileStoreServiceProvider ( getFileStoreProviderName ( strEntryType ) ).storeFileItem ( file );
173
+ }
174
+ catch ( FileServiceException e )
175
+ {
176
+ AppLogService .error ( e );
177
+ return null ;
178
+ }
179
+ }
180
+
77
181
/**
78
182
* Load a file
79
183
*
@@ -85,8 +189,12 @@ public File load( String strKey, String strOrigin )
85
189
{
86
190
try
87
191
{
88
- _fileStoreServiceProvider = FileService .getInstance ( ).getFileStoreServiceProvider ( strOrigin );
89
- return _fileStoreServiceProvider .getFile ( strKey );
192
+ if ( StringUtils .isEmpty ( strOrigin ) )
193
+ {
194
+ strOrigin = getFileStoreProviderName ( ) ;
195
+ }
196
+
197
+ return FileService .getInstance ( ).getFileStoreServiceProvider ( strOrigin ).getFile ( strKey );
90
198
}
91
199
catch ( FileServiceException e )
92
200
{
@@ -105,18 +213,17 @@ public void delete( String strKey, String strOrigin )
105
213
{
106
214
try
107
215
{
108
- _fileStoreServiceProvider = FileService .getInstance ( ).getFileStoreServiceProvider ( strOrigin );
109
- _fileStoreServiceProvider .delete ( strKey );
216
+ if ( StringUtils .isEmpty ( strOrigin ) )
217
+ {
218
+ strOrigin = getFileStoreProviderName ( );
219
+ }
220
+
221
+ FileService .getInstance ( ).getFileStoreServiceProvider ( strOrigin ).delete ( strKey );
110
222
}
111
223
catch ( FileServiceException e )
112
224
{
113
225
AppLogService .error ( e );
114
226
}
115
227
}
116
228
117
- public String getName ( )
118
- {
119
- // return default genatt file store provider name
120
- return FileService .getInstance ( ).getFileStoreServiceProvider ( ).getName ( );
121
- }
122
229
}
0 commit comments