← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20655: FileResource improvements: append domain prefix to key on object creation, removed unused synchro...

 

------------------------------------------------------------
revno: 20655
committer: Halvdan Hoem Grelland <halvdanhg@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2015-10-12 20:52:30 +0200
message:
  FileResource improvements: append domain prefix to key on object creation, removed unused synchronous save/upload method, appened content type and filename to blob uploads ensuring GETs directly to external blobstore will return the correct contenttype and filename
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/fileresource/FileResource.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/fileresource/FileResourceContentStore.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/fileresource/FileResourceService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/fileresource/DefaultFileResourceService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/fileresource/JCloudsFileResourceContentStore.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataValueController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/FileResourceController.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/fileresource/FileResource.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/fileresource/FileResource.java	2015-10-07 23:01:57 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/fileresource/FileResource.java	2015-10-12 18:52:30 +0000
@@ -214,6 +214,6 @@
 
     private String generateStorageKey()
     {
-        return UUID.randomUUID().toString();
+        return domain.getContainerName() + "/" + UUID.randomUUID().toString();
     }
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/fileresource/FileResourceContentStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/fileresource/FileResourceContentStore.java	2015-10-07 23:01:57 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/fileresource/FileResourceContentStore.java	2015-10-12 18:52:30 +0000
@@ -46,24 +46,13 @@
     ByteSource getFileResourceContent( String key );
 
     /**
-     * Save the content bytes of a FileResource to the file store.
-     * @param key the key to use. Must be unique in the file store.
-     * @param content a ByteSource providing a stream of the content to save.
-     * @param size the byte length of the content.
-     * @param contentMd5 the MD5 digest of the content.
-     * @return the key on success or null if saving failed.
-     */
-    String saveFileResourceContent( String key, ByteSource content, long size, String contentMd5 );
-
-    /**
      * Save the content of the file to the file store.
-     * @param key the key to use. Must be unique in the file store.
+     * @param fileResource the FileResource object. Must be complete and include the storageKey,
+     *                     contentLength, contentMd5 and name.
      * @param file the file. The file will be consumed and deleted upon completion.
-     * @param size the byte length of the file.
-     * @param contentMd5 the MD5 digest of the file.
      * @return the key on success or null if saving failed.
      */
-    String saveFileResourceContent( String key, File file, long size, String contentMd5 );
+    String saveFileResourceContent( FileResource fileResource, File file );
 
     /**
      * Delete the content bytes of a file resource.

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/fileresource/FileResourceService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/fileresource/FileResourceService.java	2015-10-07 23:01:57 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/fileresource/FileResourceService.java	2015-10-12 18:52:30 +0000
@@ -42,10 +42,8 @@
     FileResource getFileResource( String uid );
 
     List<FileResource> getFileResources( List<String> uids );
-    
-    String saveFileResource( FileResource fileResource, ByteSource content );
 
-    String saveFileResourceAsync( FileResource fileResource, File file );
+    String saveFileResource( FileResource fileResource, File file );
 
     void deleteFileResource( String uid );
     

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/fileresource/DefaultFileResourceService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/fileresource/DefaultFileResourceService.java	2015-10-07 23:01:57 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/fileresource/DefaultFileResourceService.java	2015-10-12 18:52:30 +0000
@@ -29,7 +29,6 @@
  */
 
 import com.google.common.io.ByteSource;
-import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.common.GenericIdentifiableObjectStore;
@@ -99,42 +98,13 @@
 
     @Transactional
     @Override
-    public String saveFileResource( FileResource fileResource, ByteSource content )
-    {
-        String storageKey = getRelativeStorageKey( fileResource );
-
-        String key = fileResourceContentStore.saveFileResourceContent(
-            storageKey, content, fileResource.getContentLength(), fileResource.getContentMd5() );
-
-        if ( key == null )
-        {
-            log.debug( "Failed saving content for FileResource" );
-            return null;
-        }
-
-        int id = fileResourceStore.save( fileResource );
-
-        if ( id <= 0 )
-        {
-            log.debug( "Failed persisting the FileResource: " + fileResource.getName() );
-            return null;
-        }
-
-        return fileResource.getUid();
-    }
-
-    @Transactional
-    @Override
-    public String saveFileResourceAsync( FileResource fileResource, File file )
+    public String saveFileResource( FileResource fileResource, File file )
     {
         fileResource.setStorageStatus( FileResourceStorageStatus.PENDING );
         fileResourceStore.save( fileResource );
 
-        String storageKey = getRelativeStorageKey( fileResource );
-
         ListenableFuture<String> saveContentTask =
-            scheduler.executeTask( () -> fileResourceContentStore.saveFileResourceContent(
-                storageKey, file, fileResource.getContentLength(), fileResource.getContentMd5() ) );
+            scheduler.executeTask( () -> fileResourceContentStore.saveFileResourceContent( fileResource, file ) );
 
         String uid = fileResource.getUid();
 
@@ -159,14 +129,14 @@
             return;
         }
 
-        fileResourceContentStore.deleteFileResourceContent( getRelativeStorageKey( fileResource ) );
+        fileResourceContentStore.deleteFileResourceContent( fileResource.getStorageKey() );
         fileResourceStore.delete( fileResource );
     }
 
     @Override
     public ByteSource getFileResourceContent( FileResource fileResource )
     {
-        return fileResourceContentStore.getFileResourceContent( getRelativeStorageKey( fileResource ) );
+        return fileResourceContentStore.getFileResourceContent( fileResource.getStorageKey() );
     }
 
     @Override
@@ -191,15 +161,6 @@
             return null;
         }
 
-        return fileResourceContentStore.getSignedGetContentUri( getRelativeStorageKey( fileResource ) );
-    }
-
-    // ---------------------------------------------------------------------
-    // Supportive methods
-    // ---------------------------------------------------------------------
-
-    private String getRelativeStorageKey( FileResource fileResource )
-    {
-        return StringUtils.prependIfMissing( fileResource.getStorageKey(), fileResource.getDomain().getContainerName() + "/" );
+        return fileResourceContentStore.getSignedGetContentUri( fileResource.getStorageKey() );
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/fileresource/JCloudsFileResourceContentStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/fileresource/JCloudsFileResourceContentStore.java	2015-10-12 17:31:22 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/fileresource/JCloudsFileResourceContentStore.java	2015-10-12 18:52:30 +0000
@@ -254,28 +254,9 @@
     }
 
     @Override
-    public String saveFileResourceContent( String key, ByteSource content, long size, String contentMd5 )
-    {
-        Blob blob = createBlob( key, content, size, contentMd5 );
-
-        if ( blob == null )
-        {
-            return null;
-        }
-
-        putBlob( blob );
-
-        return key;
-    }
-
-    @Override
-    public String saveFileResourceContent( String key, File file, long size, String contentMd5 )
-    {
-        Blob blob = blobStore.blobBuilder( key )
-            .payload( file )
-            .contentLength( size )
-            .contentMD5( HashCode.fromString( contentMd5 ) )
-            .build();
+    public String saveFileResourceContent( FileResource fileResource, File file )
+    {
+        Blob blob = createBlob( fileResource, file );
 
         if ( blob == null )
         {
@@ -294,7 +275,7 @@
             log.warn( "Temporary file '" + file.toPath() + "' could not be deleted.", ioe );
         }
 
-        return key;
+        return fileResource.getStorageKey();
     }
 
     @Override
@@ -368,12 +349,14 @@
         return etag;
     }
 
-    private Blob createBlob( String key, ByteSource content, long size, String contentMd5 )
+    private Blob createBlob( FileResource fileResource, File file )
     {
-        return blobStore.blobBuilder( key )
-            .payload( content )
-            .contentLength( size )
-            .contentMD5( HashCode.fromString( contentMd5 ) )
+        return blobStore.blobBuilder( fileResource.getStorageKey() )
+            .payload( file )
+            .contentLength( fileResource.getContentLength() )
+            .contentMD5( HashCode.fromString( fileResource.getContentMd5() ) )
+            .contentType( fileResource.getContentType() )
+            .contentDisposition( "filename=" + fileResource.getName() )
             .build();
     }
 

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataValueController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataValueController.java	2015-10-07 23:01:57 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataValueController.java	2015-10-12 18:52:30 +0000
@@ -475,14 +475,6 @@
         }
 
         // ---------------------------------------------------------------------
-        // Build response and return
-        // ---------------------------------------------------------------------
-
-        response.setContentType( fileResource.getContentType() );
-        response.setContentLength( Math.round( fileResource.getContentLength() ) );
-        response.setHeader( HttpHeaders.CONTENT_DISPOSITION, "filename=" + fileResource.getName() );
-
-        // ---------------------------------------------------------------------
         // Attempt to build signed URL request for content and redirect
         // ---------------------------------------------------------------------
 
@@ -490,12 +482,21 @@
 
         if ( signedGetUri != null )
         {
-            response.setStatus( HttpServletResponse.SC_FOUND );
+            response.setStatus( HttpServletResponse.SC_TEMPORARY_REDIRECT );
             response.setHeader( HttpHeaders.LOCATION, signedGetUri.toASCIIString() );
+
             return;
         }
 
         // ---------------------------------------------------------------------
+        // Build response and return
+        // ---------------------------------------------------------------------
+
+        response.setContentType( fileResource.getContentType() );
+        response.setContentLength( Math.round( fileResource.getContentLength() ) );
+        response.setHeader( HttpHeaders.CONTENT_DISPOSITION, "filename=" + fileResource.getName() );
+
+        // ---------------------------------------------------------------------
         // Request signing is not available, stream content back to client
         // ---------------------------------------------------------------------
 

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/FileResourceController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/FileResourceController.java	2015-10-08 22:36:14 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/FileResourceController.java	2015-10-12 18:52:30 +0000
@@ -143,7 +143,7 @@
 
         File tmpFile = toTempFile( file );
 
-        String uid = fileResourceService.saveFileResourceAsync( fileResource, tmpFile );
+        String uid = fileResourceService.saveFileResource( fileResource, tmpFile );
 
         if ( uid == null )
         {