View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de 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  
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   * MinioFileStorage
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       * {@inheritDoc }
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       * {@inheritDoc }
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      * {@inheritDoc }
132      */
133     @Override
134     public void createStorage( String strNameId ) throws StorageException
135     {
136         // Nothing to do
137     }
138 
139     /**
140      * {@inheritDoc }
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      * {@inheritDoc }
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 }