FileImageService.java

  1. /*
  2.  * Copyright (c) 2002-2022, City of Paris
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  *  1. Redistributions of source code must retain the above copyright notice
  10.  *     and the following disclaimer.
  11.  *
  12.  *  2. Redistributions in binary form must reproduce the above copyright notice
  13.  *     and the following disclaimer in the documentation and/or other materials
  14.  *     provided with the distribution.
  15.  *
  16.  *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
  17.  *     contributors may be used to endorse or promote products derived from
  18.  *     this software without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
  24.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30.  * POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  * License 1.0
  33.  */
  34. package fr.paris.lutece.portal.service.fileimage;

  35. import javax.servlet.http.HttpServletRequest;

  36. import org.apache.commons.fileupload.FileItem;

  37. import fr.paris.lutece.portal.business.file.File;
  38. import fr.paris.lutece.portal.business.file.FileHome;
  39. import fr.paris.lutece.portal.business.physicalfile.PhysicalFile;
  40. import fr.paris.lutece.portal.business.physicalfile.PhysicalFileHome;
  41. import fr.paris.lutece.portal.business.user.AdminUser;
  42. import fr.paris.lutece.portal.business.user.attribute.AdminUserFieldHome;
  43. import fr.paris.lutece.portal.service.admin.AdminUserService;
  44. import fr.paris.lutece.portal.service.file.FileService;
  45. import fr.paris.lutece.portal.service.file.FileServiceException;
  46. import fr.paris.lutece.portal.service.image.ImageResource;
  47. import fr.paris.lutece.portal.service.image.ImageResourceManager;
  48. import fr.paris.lutece.portal.service.image.ImageResourceProvider;
  49. import fr.paris.lutece.portal.service.init.LuteceInitException;
  50. import fr.paris.lutece.portal.service.util.AppLogService;
  51. import fr.paris.lutece.portal.web.LocalVariables;
  52. import fr.paris.lutece.util.file.FileUtil;

  53. /**
  54.  * Service for AdminUser image attributes. Provide ImageResource management
  55.  */
  56. public class FileImageService implements ImageResourceProvider
  57. {
  58.     private static FileImageService _singleton = new FileImageService( );
  59.     private static final String IMAGE_RESOURCE_TYPE_ID = "core_attribute_img";

  60.     /**
  61.      * Creates a new instance of FileImgService
  62.      */
  63.     private FileImageService( )
  64.     {
  65.     }

  66.     /**
  67.      * Init
  68.      *
  69.      * @throws LuteceInitException
  70.      *             if an error occurs
  71.      */
  72.     public static synchronized void init( )
  73.     {
  74.         getInstance( ).register( );
  75.     }

  76.     /**
  77.      * Initializes the service
  78.      */
  79.     public void register( )
  80.     {
  81.         ImageResourceManager.registerProvider( this );
  82.     }

  83.     /**
  84.      * Get the unique instance of the service
  85.      *
  86.      * @return The unique instance
  87.      */
  88.     public static FileImageService getInstance( )
  89.     {
  90.         return _singleton;
  91.     }

  92.     /**
  93.      * Return the Resource id
  94.      *
  95.      * @param nIdResource
  96.      *            The resource identifier
  97.      * @return The Resource Image
  98.      */
  99.     public ImageResource getImageResource( int nIdResource )
  100.     {
  101.        
  102.         if ( !isAuthorized( nIdResource ) ) return null;
  103.        
  104.         File file = FileHome.findByPrimaryKey( nIdResource );

  105.         if ( ( file != null ) && ( file.getPhysicalFile( ) != null ) && FileUtil.hasImageExtension( file.getTitle( ) ) )
  106.         {
  107.             PhysicalFile physicalFile = PhysicalFileHome.findByPrimaryKey( file.getPhysicalFile( ).getIdPhysicalFile( ) );
  108.             ImageResource imageResource = new ImageResource( );
  109.             imageResource.setImage( physicalFile.getValue( ) );
  110.             imageResource.setMimeType( file.getMimeType( ) );

  111.             return imageResource;
  112.         }
  113.        

  114.         return null;
  115.     }

  116.     /**
  117.      * Return the Resource Type id
  118.      *
  119.      * @return The Resource Type Id
  120.      */
  121.     public String getResourceTypeId( )
  122.     {
  123.         return IMAGE_RESOURCE_TYPE_ID;
  124.     }

  125.     /**
  126.      * check rights
  127.      *
  128.      * @param nIdResource
  129.      * @return true if authorized
  130.      */
  131.     private static boolean isAuthorized(  int nIdResource  )
  132.     {
  133.         HttpServletRequest request = LocalVariables.getRequest( );
  134.         AdminUser user = AdminUserService.getAdminUser( request );

  135.         return ( user == null || !AdminUserFieldHome.existsWithFile( nIdResource ) );
  136.     }
  137.    
  138.     /**
  139.      * Add Image Resource
  140.      * @param fileItem
  141.      * @return the Image File Key
  142.      */
  143.     public String addImageResource( FileItem fileItem )
  144.     {
  145.         try
  146.         {
  147.             return FileService.getInstance( ).getFileStoreServiceProvider( ).storeFileItem( fileItem );
  148.         }
  149.         catch (FileServiceException e )
  150.         {
  151.             AppLogService.error(e);
  152.             return null;
  153.         }
  154.     }
  155. }