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.IFileStoreServiceProvider;
42 import fr.paris.lutece.portal.service.image.ImageResource;
43 import fr.paris.lutece.portal.service.image.ImageResourceManager;
44 import fr.paris.lutece.portal.service.image.ImageResourceProvider;
45 import fr.paris.lutece.portal.service.init.LuteceInitException;
46 import fr.paris.lutece.util.file.FileUtil;
47
48 public class FileImagePublicService extends AbstractCacheableService implements ImageResourceProvider
49 {
50 private static FileImagePublicServiceileImagePublicService.html#FileImagePublicService">FileImagePublicService _singleton = new FileImagePublicService( );
51 public static final String IMAGE_RESOURCE_TYPE_ID = "public_image_resource";
52 private static final IFileStoreServiceProvider _fileStoreService = FileService.getInstance( ).getFileStoreServiceProvider( );
53
54
55
56
57 private FileImagePublicService( )
58 {
59 initCache();
60 }
61
62
63
64
65
66
67
68 public static synchronized void init( )
69 {
70 getInstance( ).register( );
71 }
72
73
74
75
76 public void register( )
77 {
78 ImageResourceManager.registerProvider( this );
79 }
80
81
82
83
84
85
86 public static FileImagePublicService getInstance( )
87 {
88 return _singleton;
89 }
90
91 @Override
92
93
94
95
96
97 public String getResourceTypeId( )
98 {
99 return IMAGE_RESOURCE_TYPE_ID;
100 }
101
102 @Override
103 public ImageResource getImageResource( int nIdResource )
104 {
105
106 String cacheKey = getCacheKey( nIdResource );
107 ImageResource imageresource = (ImageResource) getFromCache( cacheKey );
108 if ( imageresource != null )
109 {
110 return imageresource;
111 }
112 else
113 {
114
115 File file = _fileStoreService.getFile( String.valueOf( nIdResource ) );
116
117 if ( ( file != null ) && ( file.getPhysicalFile( ) != null ) && FileUtil.hasImageExtension( file.getTitle( ) ) )
118 {
119 ImageResource imageResource = new ImageResource( file );
120 putInCache( cacheKey, imageResource);
121 return imageResource;
122 }
123 }
124
125 return null;
126 }
127
128
129
130
131
132
133
134
135 public String addImageResource( FileItem fileItem )
136 {
137 return _fileStoreService.storeFileItem( fileItem ) ;
138 }
139
140 @Override
141 public String getName( )
142 {
143 return IMAGE_RESOURCE_TYPE_ID;
144 }
145
146
147
148
149
150
151
152
153 private static String getCacheKey( int nId )
154 {
155 StringBuilder sbKey = new StringBuilder( );
156 return sbKey.append( "[" ).append( IMAGE_RESOURCE_TYPE_ID ).append( ":" ).append( nId )
157 .append( "]" ).toString( );
158 }
159 }