Skip to content

Commit

Permalink
LUT-28558 : manage FileStoreServices by EntryType (if set in properties)
Browse files Browse the repository at this point in the history
  • Loading branch information
seboo committed Sep 23, 2024
1 parent cb82ee3 commit c56ba43
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public ImageResource getImageResource( int nIdResource )

if ( response.getFile( ) != null )
{
File file = GenericAttributeFileService.getInstance().load( response.getFile( ).getFileKey( ) );
File file = GenericAttributeFileService.getInstance().load( response.getFile( ).getFileKey( ), response.getFile( ).getOrigin( ) );

if ( ( file.getPhysicalFile( ) != null ) && FileUtil.hasImageExtension( file.getTitle( ) ) )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import fr.paris.lutece.plugins.genericattributes.business.Entry;
import fr.paris.lutece.plugins.genericattributes.business.Field;
import fr.paris.lutece.plugins.genericattributes.business.FieldHome;
import fr.paris.lutece.plugins.genericattributes.service.file.GenericAttributeFileService;
import fr.paris.lutece.plugins.genericattributes.util.GenericAttributesUtils;
import fr.paris.lutece.portal.business.file.File;
import fr.paris.lutece.portal.business.physicalfile.PhysicalFile;
Expand All @@ -60,6 +61,8 @@
*/
public abstract class AbstractEntryTypeComment extends EntryTypeService
{
public static final String ENTRY_TYPE_KEYNAME = "entryTypeComment";

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -114,14 +117,9 @@ public String getRequestData( Entry entry, HttpServletRequest request, Locale lo
file.setPhysicalFile( physicalFile );

String idFile;
try {
idFile = getFileStoreServiceProvider( ).storeFile( file );
GenericAttributesUtils.createOrUpdateField( entry, FIELD_DOWNLOADABLE_FILE, file.getTitle( ), idFile );
}
catch ( FileServiceException e )
{
AppLogService.error(e);
}
idFile = GenericAttributeFileService.getInstance( ).save( fileItem, ENTRY_TYPE_KEYNAME );
GenericAttributesUtils.createOrUpdateField( entry, FIELD_DOWNLOADABLE_FILE, file.getTitle( ), idFile );


}
}
Expand All @@ -133,18 +131,8 @@ private void removeOldFiles( Entry entry )
Field oldFile = entry.getFieldByCode( FIELD_DOWNLOADABLE_FILE );
if ( oldFile != null )
{
try {
getFileStoreServiceProvider( ).delete( oldFile.getValue( ) );
FieldHome.remove( oldFile.getIdField( ) );
} catch (FileServiceException e) {
AppLogService.error(e);
}

GenericAttributeFileService.getInstance( ).delete( oldFile.getValue( ), null);
FieldHome.remove( oldFile.getIdField( ) );
}
}

protected IFileStoreServiceProvider getFileStoreServiceProvider( )
{
return FileService.getInstance( ).getFileStoreServiceProvider( "defaultDatabaseFileStoreProvider" );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@
*/
public abstract class AbstractEntryTypeFile extends AbstractEntryTypeUpload
{
/**
public static final String ENTRY_TYPE_KEYNAME = "entryTypeFile";

/**
* {@inheritDoc}
*/
@Override
Expand Down Expand Up @@ -217,7 +219,6 @@ private Response getResponseFromFile( FileItem fileItem, Entry entry, boolean bC

File file = new File( );
file.setTitle( fileItem.getName( ) );
file.setOrigin( GenericAttributeFileService.getInstance( ).getName( ) );
file.setSize( ( fileItem.getSize( ) < Integer.MAX_VALUE ) ? (int) fileItem.getSize( ) : Integer.MAX_VALUE );

if ( bCreatePhysicalFile )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
*/
public abstract class AbstractEntryTypeGalleryImage extends EntryTypeService
{
public static final String ENTRY_TYPE_KEYNAME = "entryTypeGalleryImage";

// PARAMETERS
protected static final String PARAMETER_ID_RESPONSE = "id_response";
protected static final String PARAMETER_CODE_GALLERY = "code_gallery";
Expand Down Expand Up @@ -113,7 +115,7 @@ public GenericAttributeError getResponseData( Entry entry, HttpServletRequest re
{
FileImagePublicService.init( );

File file = GenericAttributeFileService.getInstance( ).load( strFileGallery, GenericAttributeFileService.getInstance( ).getName( ) );
File file = GenericAttributeFileService.getInstance( ).load( strFileGallery, ENTRY_TYPE_KEYNAME);

Response response = new Response( );
response.setEntry( entry );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public interface IEntryTypeService
String FIELD_ILLUSTRATION_IMAGE = "illustration_image";
String FIELD_GEOJSON = "coordinates_geojson";
String FIELD_ID_LAYER = "DataLayer";
String FIELD_DISABLED = "disabled";
String FIELD_DISABLED = "disabled";
String FIELD_IS_UPDATABLE = "is_updatable";

// attribute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,91 @@
*/
package fr.paris.lutece.plugins.genericattributes.service.file;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.lang3.StringUtils;

import fr.paris.lutece.portal.business.file.File;
import fr.paris.lutece.portal.service.file.FileService;
import fr.paris.lutece.portal.service.file.FileServiceException;
import fr.paris.lutece.portal.service.file.IFileStoreServiceProvider;
import fr.paris.lutece.portal.service.util.AppLogService;
import fr.paris.lutece.portal.service.util.AppPropertiesService;

public class GenericAttributeFileService
{
private static final GenericAttributeFileService _instance = new GenericAttributeFileService( );
private IFileStoreServiceProvider _fileStoreServiceProvider;
private static final String PROPERTY_FILESTORESERVICE_PREFIX = "genericattributes.filestoreservice";
private static final String PROPERTY_FILESTORESERVICE_DEFAULT_SUFFIX = "default";

private static final GenericAttributeFileService _instance = new GenericAttributeFileService( );
private static final Map<String,String> _entryTypeFileServices = new HashMap<>();

/**
* Constructor
*/
private GenericAttributeFileService( )
{
List<String> keyList = AppPropertiesService.getKeys( PROPERTY_FILESTORESERVICE_PREFIX);

// init specific entryType fileStoreService names if exists
keyList.stream().forEach( s -> {
if ( !StringUtils.isAllBlank( AppPropertiesService.getProperty(s) ) )
{
_entryTypeFileServices.put(s, AppPropertiesService.getProperty(s));
}
} );
}

/**
* get FileStoreServiceProvider name for entry type :
* - returns the entry type FileService (if set)
* - otherwise, returns the GenAttr default FileService (if set)
* - otherwise, returns the lutece default FileService
*
* @param strEntryType
* @return the name of the FileStoreServiceProvider
*/
public String getFileStoreProviderName( String strEntryType )
{
if (strEntryType != null )
{
if ( _entryTypeFileServices.containsKey( strEntryType ))
{
return _entryTypeFileServices.get( strEntryType );
}
}

if ( _entryTypeFileServices.containsKey( PROPERTY_FILESTORESERVICE_DEFAULT_SUFFIX ))
{
return _entryTypeFileServices.get( PROPERTY_FILESTORESERVICE_DEFAULT_SUFFIX );
}

return FileService.getInstance( ).getFileStoreServiceProvider( ).getName( );
}

/**
* get FileStoreServiceProvider name :
* - returns the GenAttr default FileService (if set)
* - otherwise, returns the lutece default FileService
*
* Use getFileStoreProviderName( String strEntryType ) to get entryType specific File service
*
* @return the name of the FileStoreServiceProvider
*/
public String getFileStoreProviderName( )
{
return getFileStoreProviderName( null );
}

/**
* get instance of service
*
* @return the instance
*/
public static GenericAttributeFileService getInstance( )
{
return _instance;
Expand All @@ -56,16 +126,30 @@ public static GenericAttributeFileService getInstance( )
/**
* Save a file
*
* @param file
* The file
* @param file The lutece file in default generic file Service
* @return The key of the file
*/
public String save( File file )
public String save( File file)
{
return save( file, null);
}
/**
* Save a file
*
* @param file The lutece file
* @param strEntryType the entry type
* @return The key of the file
*/
public String save( File file, String strEntryType)
{
try
{
_fileStoreServiceProvider = FileService.getInstance( ).getFileStoreServiceProvider( file.getOrigin( ) );
return _fileStoreServiceProvider.storeFile( file );
if ( file.getOrigin( ) == null )
{
file.setOrigin( getFileStoreProviderName( strEntryType ) );
}

return FileService.getInstance( ).getFileStoreServiceProvider( file.getOrigin( ) ).storeFile( file );
}
catch( FileServiceException e )
{
Expand All @@ -74,6 +158,26 @@ public String save( File file )
}
}

/**
* Save a file
*
* @param file The fileItem
* @param strEntryType the entry type
* @return The key of the file
*/
public String save( FileItem file, String strEntryType)
{
try
{
return FileService.getInstance( ).getFileStoreServiceProvider( getFileStoreProviderName( strEntryType ) ).storeFileItem( file );
}
catch( FileServiceException e )
{
AppLogService.error( e );
return null;
}
}

/**
* Load a file
*
Expand All @@ -85,8 +189,12 @@ public File load( String strKey, String strOrigin )
{
try
{
_fileStoreServiceProvider = FileService.getInstance( ).getFileStoreServiceProvider( strOrigin );
return _fileStoreServiceProvider.getFile( strKey );
if ( StringUtils.isEmpty( strOrigin ) )
{
strOrigin = getFileStoreProviderName( ) ;
}

return FileService.getInstance( ).getFileStoreServiceProvider( strOrigin ).getFile( strKey );
}
catch( FileServiceException e )
{
Expand All @@ -105,18 +213,17 @@ public void delete( String strKey, String strOrigin )
{
try
{
_fileStoreServiceProvider = FileService.getInstance( ).getFileStoreServiceProvider( strOrigin );
_fileStoreServiceProvider.delete( strKey );
if ( StringUtils.isEmpty( strOrigin ) )
{
strOrigin = getFileStoreProviderName( );
}

FileService.getInstance( ).getFileStoreServiceProvider( strOrigin ).delete( strKey );
}
catch( FileServiceException e )
{
AppLogService.error( e );
}
}

public String getName( )
{
// return default genatt file store provider name
return FileService.getInstance( ).getFileStoreServiceProvider( ).getName( );
}
}
2 changes: 2 additions & 0 deletions webapp/WEB-INF/conf/plugins/genericattributes.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ genericattributes.telephoneNumber.default.default.region=FR
# export field name
genericattributes.entrytype.geolocalisation.export.field.name=false
genericattributes.entrytype.cartography.export.field.name=false

genericattributes.filestoreservice.default=

0 comments on commit c56ba43

Please sign in to comment.