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
35 package fr.paris.lutece.plugins.myfiles.service.storage.impl;
36
37 import fr.paris.lutece.plugins.myfiles.business.MyFileData;
38 import fr.paris.lutece.plugins.myfiles.business.MyFileLink;
39 import fr.paris.lutece.plugins.myfiles.service.storage.FileStorage;
40 import fr.paris.lutece.plugins.myfiles.service.storage.NoStorageException;
41 import fr.paris.lutece.plugins.myfiles.service.storage.StorageException;
42 import fr.paris.lutece.plugins.myfiles.business.database.MyFileBlob;
43 import fr.paris.lutece.plugins.myfiles.business.database.MyFileBlobHome;
44 import fr.paris.lutece.util.url.UrlItem;
45 import java.util.ArrayList;
46 import java.util.List;
47
48
49
50
51 public class DatabaseFileStorage implements FileStorage
52 {
53
54 private static final String PATH_SERVLET_DOWNLOAD = "servlet/plugins/myfiles/download";
55
56 private static final String PARAMETER_FILENAME = "filename";
57
58 private static final long FILE_MAX_SIZE = 16 * 1024 * 1024;
59
60
61
62
63 @Override
64 public List<MyFileLink> getFiles( String strUserId ) throws NoStorageException, StorageException
65 {
66 List<MyFileLink> listFiles = new ArrayList<>( );
67
68 String strBucketName = strUserId;
69 UrlItem urlItem = new UrlItem( PATH_SERVLET_DOWNLOAD );
70 boolean bExist = MyFileBlobHome.bucketExists( strBucketName );
71 if ( bExist )
72 {
73 List<MyFileBlob> myFileBlobList = MyFileBlobHome.getMyFileBlobsList( );
74
75 for ( MyFileBlob myFileBlob : myFileBlobList )
76 {
77 if ( myFileBlob.getBucketNameId( ).equals( strBucketName ) )
78 {
79 MyFileLink myFileLink = new MyFileLink( );
80 myFileLink.setName( myFileBlob.getName( ) );
81 myFileLink.setContentType( myFileBlob.getContentType( ) );
82 myFileLink.setSize( myFileBlob.getSize( ) );
83 urlItem.addParameter( PARAMETER_FILENAME, myFileBlob.getName( ) );
84 String strUrl = urlItem.getUrl( );
85 myFileLink.setUrl( strUrl );
86 listFiles.add( myFileLink );
87 }
88 }
89 }
90 else
91 {
92 throw new NoStorageException( );
93 }
94
95 return listFiles;
96 }
97
98
99
100
101 @Override
102 public MyFileData getFile( String strUserId, String strFilename ) throws NoStorageException, StorageException
103 {
104 MyFileData myFile = null;
105 String strBucketName = strUserId;
106 boolean bExist = MyFileBlobHome.bucketExists( strBucketName );
107 if ( bExist )
108 {
109 myFile = new MyFileData( );
110 MyFileBlob myFileBlob = MyFileBlobHome.findByName( strBucketName, strFilename );
111
112 if ( myFileBlob == null )
113 {
114 throw new StorageException( "Error getting file : File not found" );
115 }
116
117 myFile.setName( strFilename );
118 myFile.setContentType( myFileBlob.getContentType( ) );
119 myFile.setSize( myFileBlob.getSize( ) );
120 myFile.setInputstream( myFileBlob.getInputstream( ) );
121 }
122 else
123 {
124 throw new NoStorageException( );
125 }
126
127 return myFile;
128 }
129
130
131
132
133 @Override
134 public void createStorage( String strNameId ) throws StorageException
135 {
136
137 }
138
139
140
141
142 @Override
143 public void addFile( String strUserId, MyFileData myFileData ) throws StorageException
144 {
145 String strBucketName = strUserId;
146 boolean bBucketExist = MyFileBlobHome.bucketExists( strBucketName );
147 if ( bBucketExist )
148 {
149 boolean bFileExist = MyFileBlobHome.myFileBlobExists( strBucketName, strUserId );
150 if ( bFileExist )
151 {
152 throw new StorageException( "Error adding file : File already exists" );
153 }
154
155 if ( myFileData.getSize( ) > FILE_MAX_SIZE )
156 {
157 throw new StorageException( "Error adding file : File size is too big" );
158 }
159
160 MyFileBlob myFileBlob = new MyFileBlob( );
161 myFileBlob.setBucketNameId( strBucketName );
162 myFileBlob.setName( myFileData.getName( ) );
163 myFileBlob.setSize( myFileData.getSize( ) );
164 myFileBlob.setContentType( myFileData.getContentType( ) );
165 myFileBlob.setInputstream( myFileData.getInputstream( ) );
166 MyFileBlobHome.create( myFileBlob );
167 }
168 }
169
170
171
172
173 @Override
174 public void removeFile( String strUserId, String strFilename ) throws StorageException
175 {
176 String strBucketName = strUserId;
177
178 MyFileBlob myFileBlob = MyFileBlobHome.findByName( strBucketName, strFilename );
179
180 if ( myFileBlob == null )
181 {
182 throw new StorageException( "Error removing file : File not found" );
183 }
184
185 MyFileBlobHome.remove( myFileBlob.getId( ) );
186 }
187
188 }