Skip to content

Commit

Permalink
LUTECE-2467: Add origin attribute to lutece Files to privatize access…
Browse files Browse the repository at this point in the history
… by LocalDatabaseFileService instances
  • Loading branch information
seboo committed Jan 26, 2023
1 parent edf3a6d commit 8f9ac7e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 15 deletions.
21 changes: 21 additions & 0 deletions src/java/fr/paris/lutece/portal/business/file/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class File implements Serializable
private String _strMimeType;
private Timestamp _dateCreation;
private String _url;
private String _strOrigin;

/**
* get file key
Expand Down Expand Up @@ -230,4 +231,24 @@ public void setUrl(String strUrl) {
this._url = strUrl;
}

/**
* get origin
*
* @return the origin
*/
public String getOrigin( )
{
return _strOrigin;
}

/**
* set origin
*
* @param _strOrigin
*/
public void setOrigin( String strOrigin )
{
this._strOrigin = strOrigin;
}

}
13 changes: 8 additions & 5 deletions src/java/fr/paris/lutece/portal/business/file/FileDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@
public final class FileDAO implements IFileDAO
{
// Constants
private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT id_file,title,id_physical_file,file_size,mime_type,date_creation"
private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT id_file,title,id_physical_file,file_size,mime_type,date_creation,origin"
+ " FROM core_file WHERE id_file = ?";
private static final String SQL_QUERY_INSERT = "INSERT INTO core_file(title,id_physical_file,file_size,mime_type,date_creation)" + " VALUES(?,?,?,?,?)";
private static final String SQL_QUERY_INSERT = "INSERT INTO core_file(title,id_physical_file,file_size,mime_type,date_creation,origin)" + " VALUES(?,?,?,?,?,?)";
private static final String SQL_QUERY_DELETE = "DELETE FROM core_file WHERE id_file = ? ";
private static final String SQL_QUERY_UPDATE = "UPDATE core_file SET " + "id_file=?,title=?,id_physical_file=?,file_size=?,mime_type=? WHERE id_file = ?";
private static final String SQL_QUERY_UPDATE = "UPDATE core_file SET " + "id_file=?,title=?,id_physical_file=?,file_size=?,mime_type=?,origin=? WHERE id_file = ?";

/**
* Insert a new record in the table.
Expand Down Expand Up @@ -79,7 +79,8 @@ public int insert( File file )

daoUtil.setInt( nIndex++, file.getSize( ) );
daoUtil.setString( nIndex++, file.getMimeType( ) );
daoUtil.setTimestamp( nIndex, new Timestamp( new Date( ).getTime( ) ) );
daoUtil.setTimestamp( nIndex++, new Timestamp( new Date( ).getTime( ) ) );
daoUtil.setString( nIndex, file.getOrigin( ) );
daoUtil.executeUpdate( );

if ( daoUtil.nextGeneratedKey( ) )
Expand Down Expand Up @@ -126,6 +127,7 @@ public File load( int nId )
file.setSize( daoUtil.getInt( 4 ) );
file.setMimeType( daoUtil.getString( 5 ) );
file.setDateCreation( daoUtil.getTimestamp( 6 ) );
file.setOrigin( daoUtil.getString( 7 ) );
}

}
Expand Down Expand Up @@ -174,7 +176,8 @@ public void store( File file )

daoUtil.setInt( 4, file.getSize( ) );
daoUtil.setString( 5, file.getMimeType( ) );
daoUtil.setInt( 6, file.getIdFile( ) );
daoUtil.setString( 6, file.getOrigin( ) );
daoUtil.setInt( 7, file.getIdFile( ) );
daoUtil.executeUpdate( );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public File getFileMetaData( String strKey )
* @param strKey
* @param withPhysicalFile
*
* @return the file with the physical file content if withPhysicalFile is true
* @return the file with the physical file content if withPhysicalFile is true, null otherwise
*/
public File getFile( String strKey, boolean withPhysicalFile )
{
Expand All @@ -182,16 +182,19 @@ public File getFile( String strKey, boolean withPhysicalFile )
// get meta data
File file = FileHome.findByPrimaryKey( nfileId );

if ( file != null )
// check if the file exists and was inserted with this provider
if ( file == null || !file.getOrigin( ).equals( getName( ) ) )
{
if ( withPhysicalFile )
{
// get file content
file.setPhysicalFile( PhysicalFileHome.findByPrimaryKey( file.getPhysicalFile( ).getIdPhysicalFile( ) ) );
}

return file;
return null;
}

if ( withPhysicalFile )
{
// get file content
file.setPhysicalFile( PhysicalFileHome.findByPrimaryKey( file.getPhysicalFile( ).getIdPhysicalFile( ) ) );
}

return file;
}

return null;
Expand All @@ -207,7 +210,8 @@ public String storeBytes( byte [ ] blob )
PhysicalFile physicalFile = new PhysicalFile( );
physicalFile.setValue( blob );
file.setPhysicalFile( physicalFile );

file.setOrigin( getName( ) );

int nFileId = FileHome.create( file );

return String.valueOf( nFileId );
Expand Down Expand Up @@ -236,6 +240,8 @@ public String storeInputStream( InputStream inputStream )
physicalFile.setValue( buffer );
file.setPhysicalFile( physicalFile );

file.setOrigin( getName( ) );

int nFileId = FileHome.create( file );

return String.valueOf( nFileId );
Expand All @@ -253,6 +259,8 @@ public String storeFileItem( FileItem fileItem )
file.setSize( (int) fileItem.getSize( ) );
file.setMimeType( fileItem.getContentType( ) );

file.setOrigin( getName( ) );

PhysicalFile physicalFile = new PhysicalFile( );

byte [ ] byteArray;
Expand Down Expand Up @@ -280,6 +288,8 @@ public String storeFileItem( FileItem fileItem )
@Override
public String storeFile( File file )
{
file.setOrigin( getName( ) );

int nFileId = FileHome.create( file );

return String.valueOf( nFileId );
Expand Down
1 change: 1 addition & 0 deletions src/sql/create_db_lutece_core.sql
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ CREATE TABLE core_file (
file_size INT DEFAULT NULL,
mime_type VARCHAR(255) DEFAULT NULL,
date_creation timestamp default NULL NULL,
origin VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (id_file)
);

Expand Down
1 change: 1 addition & 0 deletions src/sql/upgrade/update_db_lutece_core-7.0.6-7.0.7.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE core_file ADD COLUMN origin VARCHAR(255) DEFAULT NULL;

0 comments on commit 8f9ac7e

Please sign in to comment.