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 javax.servlet.http.HttpServletRequest;
37
38 import org.apache.commons.fileupload.FileItem;
39
40 import fr.paris.lutece.portal.business.file.File;
41 import fr.paris.lutece.portal.business.file.FileHome;
42 import fr.paris.lutece.portal.business.physicalfile.PhysicalFile;
43 import fr.paris.lutece.portal.business.physicalfile.PhysicalFileHome;
44 import fr.paris.lutece.portal.business.user.AdminUser;
45 import fr.paris.lutece.portal.business.user.attribute.AdminUserFieldHome;
46 import fr.paris.lutece.portal.service.admin.AdminUserService;
47 import fr.paris.lutece.portal.service.file.FileService;
48 import fr.paris.lutece.portal.service.file.FileServiceException;
49 import fr.paris.lutece.portal.service.image.ImageResource;
50 import fr.paris.lutece.portal.service.image.ImageResourceManager;
51 import fr.paris.lutece.portal.service.image.ImageResourceProvider;
52 import fr.paris.lutece.portal.service.init.LuteceInitException;
53 import fr.paris.lutece.portal.service.util.AppLogService;
54 import fr.paris.lutece.portal.web.LocalVariables;
55 import fr.paris.lutece.util.file.FileUtil;
56
57
58
59
60 public class FileImageService implements ImageResourceProvider
61 {
62 private static FileImageServicemage/FileImageService.html#FileImageService">FileImageService _singleton = new FileImageService( );
63 private static final String IMAGE_RESOURCE_TYPE_ID = "core_attribute_img";
64
65
66
67
68 private FileImageService( )
69 {
70 }
71
72
73
74
75
76
77
78 public static synchronized void init( )
79 {
80 getInstance( ).register( );
81 }
82
83
84
85
86 public void register( )
87 {
88 ImageResourceManager.registerProvider( this );
89 }
90
91
92
93
94
95
96 public static FileImageService getInstance( )
97 {
98 return _singleton;
99 }
100
101
102
103
104
105
106
107
108 public ImageResource getImageResource( int nIdResource )
109 {
110
111 if ( !isAuthorized( nIdResource ) ) return null;
112
113 File file = FileHome.findByPrimaryKey( nIdResource );
114
115 if ( ( file != null ) && ( file.getPhysicalFile( ) != null ) && FileUtil.hasImageExtension( file.getTitle( ) ) )
116 {
117 PhysicalFile physicalFile = PhysicalFileHome.findByPrimaryKey( file.getPhysicalFile( ).getIdPhysicalFile( ) );
118 ImageResource/ImageResource.html#ImageResource">ImageResource imageResource = new ImageResource( );
119 imageResource.setImage( physicalFile.getValue( ) );
120 imageResource.setMimeType( file.getMimeType( ) );
121
122 return imageResource;
123 }
124
125
126 return null;
127 }
128
129
130
131
132
133
134 public String getResourceTypeId( )
135 {
136 return IMAGE_RESOURCE_TYPE_ID;
137 }
138
139
140
141
142
143
144
145 private static boolean isAuthorized( int nIdResource )
146 {
147 HttpServletRequest request = LocalVariables.getRequest( );
148 AdminUser user = AdminUserService.getAdminUser( request );
149
150 return ( user == null || !AdminUserFieldHome.existsWithFile( nIdResource ) );
151 }
152
153
154
155
156
157
158 public String addImageResource( FileItem fileItem )
159 {
160 try
161 {
162 return FileService.getInstance( ).getFileStoreServiceProvider( ).storeFileItem( fileItem );
163 }
164 catch (FileServiceException e )
165 {
166 AppLogService.error(e);
167 return null;
168 }
169 }
170 }