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