1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.portal.service.fileimage;
35
36 import org.apache.commons.fileupload.FileItem;
37
38 import fr.paris.lutece.portal.business.file.File;
39 import fr.paris.lutece.portal.service.cache.AbstractCacheableService;
40 import fr.paris.lutece.portal.service.file.FileService;
41 import fr.paris.lutece.portal.service.file.FileServiceException;
42 import fr.paris.lutece.portal.service.file.IFileStoreServiceProvider;
43 import fr.paris.lutece.portal.service.image.ImageResource;
44 import fr.paris.lutece.portal.service.image.ImageResourceManager;
45 import fr.paris.lutece.portal.service.image.ImageResourceProvider;
46 import fr.paris.lutece.portal.service.init.LuteceInitException;
47 import fr.paris.lutece.portal.service.util.AppLogService;
48 import fr.paris.lutece.util.file.FileUtil;
49
50 public class FileImagePublicService extends AbstractCacheableService implements ImageResourceProvider
51 {
52 private static FileImagePublicServiceileImagePublicService.html#FileImagePublicService">FileImagePublicService _singleton = new FileImagePublicService( );
53 public static final String IMAGE_RESOURCE_TYPE_ID = "public_image_resource";
54 private static final IFileStoreServiceProvider _fileStoreService = FileService.getInstance( ).getFileStoreServiceProvider( );
55
56
57
58
59 private FileImagePublicService( )
60 {
61 initCache();
62 }
63
64
65
66
67
68
69
70 public static synchronized void init( )
71 {
72 getInstance( ).register( );
73 }
74
75
76
77
78 public void register( )
79 {
80 ImageResourceManager.registerProvider( this );
81 }
82
83
84
85
86
87
88 public static FileImagePublicService getInstance( )
89 {
90 return _singleton;
91 }
92
93 @Override
94
95
96
97
98
99 public String getResourceTypeId( )
100 {
101 return IMAGE_RESOURCE_TYPE_ID;
102 }
103
104 @Override
105 public ImageResource getImageResource( int nIdResource )
106 {
107
108 String cacheKey = getCacheKey( nIdResource );
109 ImageResource imageresource = (ImageResource) getFromCache( cacheKey );
110 if ( imageresource != null )
111 {
112 return imageresource;
113 }
114 else
115 {
116
117 try
118 {
119 File file = _fileStoreService.getFile( String.valueOf( nIdResource ) );
120
121 if ( ( file != null ) && ( file.getPhysicalFile( ) != null ) && FileUtil.hasImageExtension( file.getTitle( ) ) )
122 {
123 ImageResource imageResource = new ImageResource( file );
124 putInCache( cacheKey, imageResource);
125 return imageResource;
126 }
127 }
128 catch ( FileServiceException e )
129 {
130 AppLogService.error(e);
131 }
132 }
133
134 return null;
135 }
136
137
138
139
140
141
142
143
144 public String addImageResource( FileItem fileItem )
145 {
146 try
147 {
148 return _fileStoreService.storeFileItem( fileItem ) ;
149 }
150 catch ( FileServiceException e )
151 {
152 AppLogService.error(e);
153 return null;
154 }
155 }
156
157 @Override
158 public String getName( )
159 {
160 return IMAGE_RESOURCE_TYPE_ID;
161 }
162
163
164
165
166
167
168
169
170 private static String getCacheKey( int nId )
171 {
172 StringBuilder sbKey = new StringBuilder( );
173 return sbKey.append( "[" ).append( IMAGE_RESOURCE_TYPE_ID ).append( ":" ).append( nId )
174 .append( "]" ).toString( );
175 }
176 }