From 7bb99d0f7a6a25074e1ab36c7882423e955f57d0 Mon Sep 17 00:00:00 2001 From: manutarus Date: Mon, 4 Dec 2017 15:55:47 +0300 Subject: [PATCH 01/14] saving images to s3 --- assets/config/opensrp.properties | 9 +- opensrp-core/pom.xml | 8 +- .../opensrp/service/MultimediaService.java | 137 ++++++++++++------ .../org/opensrp/MultimediaServiceTest.java | 82 +++++++---- 4 files changed, 156 insertions(+), 80 deletions(-) diff --git a/assets/config/opensrp.properties b/assets/config/opensrp.properties index aebad7f8c1..2a8eafe785 100644 --- a/assets/config/opensrp.properties +++ b/assets/config/opensrp.properties @@ -5,7 +5,6 @@ form.poll.time.interval=2 mcts.phone.number=8762963816 js.directory.name=/ziggy form.directory.name=/form -multimedia.directory.name=/opt/multimedia form.download.files=form.xml, model.xml, form_definition.json multimedia.directory.name=../multimedia/opensrp qrcodes.directory.name=/home/opensrp/qr-codes/ @@ -59,7 +58,6 @@ jdbc.url-wo-db=jdbc:mysql://localhost:3306 # Hibernate properties for Reporting DB hibernate.dialect=org.hibernate.dialect.MySQLDialect -hibernate.show_sql=false # hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.format_sql=false @@ -88,3 +86,10 @@ file.maxUploadSize=20971520 #opnenSRP Site url opensrp.site.url="" + +# S3 configuration +aws_access_key_id = AKIAJTCL2FYTPIJCBP2Q +aws_secret_access_key = 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 +aws_region = eu-west-1 +aws_bucket = opensrp-stage +aws_key_folder = media/zeir/patient_images/ diff --git a/opensrp-core/pom.xml b/opensrp-core/pom.xml index 40ff3f1015..5181cc285e 100644 --- a/opensrp-core/pom.xml +++ b/opensrp-core/pom.xml @@ -46,6 +46,12 @@ + + + com.amazonaws + aws-java-sdk + 1.11.238 + motech-platform-server-api org.motechproject @@ -60,7 +66,7 @@ com.fasterxml.jackson.core jackson-databind - 2.2.2 + 2.6.0 diff --git a/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java b/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java index 767c99e637..78129e8d28 100644 --- a/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java +++ b/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java @@ -1,8 +1,13 @@ package org.opensrp.service; -import java.io.File; -import java.util.List; - +import com.amazonaws.auth.AWSStaticCredentialsProvider; +import com.amazonaws.auth.BasicAWSCredentials; +import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.AmazonS3ClientBuilder; +import com.amazonaws.services.s3.model.ObjectMetadata; +import com.amazonaws.services.s3.model.PutObjectRequest; +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.codec.digest.DigestUtils; import org.opensrp.domain.Client; import org.opensrp.domain.Multimedia; import org.opensrp.dto.form.MultimediaDTO; @@ -14,44 +19,62 @@ import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; +import java.io.*; +import java.util.List; + @Service public class MultimediaService { - + private static Logger logger = LoggerFactory.getLogger(MultimediaService.class.toString()); - + public static final String IMAGES_DIR = "patient_images"; - + private static final String VIDEOS_DIR = "videos"; - + private final MultimediaRepository multimediaRepository; - + private final ClientService clientService; - + private String multimediaDirPath; - + @Value("#{opensrp['multimedia.directory.name']}") String baseMultimediaDirPath; - + + @Value("#{opensrp['aws_access_key_id']}") + String awsAccessKeyId; + + @Value("#{opensrp['aws_secret_access_key']}") + String awsSecretAccessKey; + + @Value("#{opensrp['aws_region']}") + String awsRegion; + + @Value("#{opensrp['aws_bucket']}") + String awsBucket; + + @Value("#{opensrp['aws_key_folder']}") + String awsKeyFolder; + @Autowired public MultimediaService(MultimediaRepository multimediaRepository, ClientService clientService) { this.multimediaRepository = multimediaRepository; this.clientService = clientService; } - + public String saveMultimediaFile(MultimediaDTO multimediaDTO, MultipartFile file) { if (uploadFile(multimediaDTO, file)) { try { logger.info("Image path : " + multimediaDirPath); - + Multimedia multimediaFile = new Multimedia().withCaseId(multimediaDTO.getCaseId()) - .withProviderId(multimediaDTO.getProviderId()).withContentType(multimediaDTO.getContentType()) - .withFilePath(multimediaDTO.getFilePath()).withFileCategory(multimediaDTO.getFileCategory()); - + .withProviderId(multimediaDTO.getProviderId()).withContentType(multimediaDTO.getContentType()) + .withFilePath(multimediaDTO.getFilePath()).withFileCategory(multimediaDTO.getFileCategory()); + multimediaRepository.add(multimediaFile); Client client = clientService.getByBaseEntityId(multimediaDTO.getCaseId()); - if (client !=null) { - if(client.getAttribute("Patient Image") != null) { + if (client != null) { + if (client.getAttribute("Patient Image") != null) { client.removeAttribute("Patient Image"); } client.addAttribute("Patient Image", multimediaDTO.getCaseId() + ".jpg"); @@ -64,59 +87,53 @@ public String saveMultimediaFile(MultimediaDTO multimediaDTO, MultipartFile file e.getMessage(); } } - + return "fail"; } - + public boolean uploadFile(MultimediaDTO multimediaDTO, MultipartFile multimediaFile) { - + // String baseMultimediaDirPath = System.getProperty("user.home"); - + if (!multimediaFile.isEmpty()) { try { - + multimediaDirPath = baseMultimediaDirPath + File.separator; String fileExt = ".jpg"; switch (multimediaDTO.getContentType()) { - + case "application/octet-stream": multimediaDirPath += VIDEOS_DIR; fileExt = ".mp4"; break; - + case "image/jpeg": multimediaDirPath += IMAGES_DIR; fileExt = ".jpg"; break; - + case "image/gif": multimediaDirPath += IMAGES_DIR; fileExt = ".gif"; break; - + case "image/png": multimediaDirPath += IMAGES_DIR; fileExt = ".png"; break; - + } new File(multimediaDirPath).mkdir(); String fileName = multimediaDirPath + File.separator + multimediaDTO.getCaseId() + fileExt; multimediaDTO.withFilePath(fileName); File multimediaDir = new File(fileName); - + multimediaFile.transferTo(multimediaDir); - - /* - byte[] bytes = multimediaFile.getBytes(); - - BufferedOutputStream stream = new BufferedOutputStream( - new FileOutputStream(multimediaDirPath)); - stream.write(bytes); - stream.close();*/ - + + uploadImageTos3(multimediaDir, awsAccessKeyId, awsSecretAccessKey, awsRegion,awsBucket, awsKeyFolder); + return true; - + } catch (Exception e) { logger.error("", e); @@ -126,19 +143,43 @@ public boolean uploadFile(MultimediaDTO multimediaDTO, MultipartFile multimediaF return false; } } - - private void makeMultimediaDir(String dirPath) { - File file = new File(dirPath); - if (!file.exists()) - file.mkdirs(); - - } - + public List getMultimediaFiles(String providerId) { return multimediaRepository.all(providerId); } - + public Multimedia findByCaseId(String entityId) { return multimediaRepository.findByCaseId(entityId); } + + public boolean uploadImageTos3(File imageFile, String awsAccessKeyId, String awsSecretAccessKey, String awsRegion, + String awsBucket, String awsKeyFolder) { + + AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials( + new AWSStaticCredentialsProvider(new BasicAWSCredentials(awsAccessKeyId, awsSecretAccessKey))) + .withRegion(awsRegion).build(); + InputStream inputStream; + try { + byte[] md5 = DigestUtils.md5(new FileInputStream(imageFile)); + inputStream = new FileInputStream(imageFile); + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentLength(imageFile.length()); + metadata.setContentMD5(new String(Base64.encodeBase64(md5))); + PutObjectRequest request = new PutObjectRequest(awsBucket, awsKeyFolder + imageFile.getName(), inputStream, + metadata); + // request.setCannedAcl(CannedAccessControlList.PublicRead); + // PutObjectResult result = s3Client.putObject(request); + s3Client.putObject(request); + return true; + } + catch (FileNotFoundException e) { + e.printStackTrace(); + return false; + } + catch (IOException e) { + e.printStackTrace(); + return false; + } + + } } diff --git a/opensrp-core/src/test/java/org/opensrp/MultimediaServiceTest.java b/opensrp-core/src/test/java/org/opensrp/MultimediaServiceTest.java index f06480f989..f4f9476e59 100644 --- a/opensrp-core/src/test/java/org/opensrp/MultimediaServiceTest.java +++ b/opensrp-core/src/test/java/org/opensrp/MultimediaServiceTest.java @@ -1,13 +1,5 @@ package org.opensrp; -import static org.mockito.MockitoAnnotations.initMocks; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.util.List; - import org.junit.Assert; import org.junit.Before; import org.junit.Ignore; @@ -26,38 +18,62 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.web.multipart.MultipartFile; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.List; + +import static org.mockito.MockitoAnnotations.initMocks; + @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:test-applicationContext-opensrp.xml") public class MultimediaServiceTest { - + + @Value("#{opensrp['aws_access_key_id']}") + String awsAccessKeyId; + + @Value("#{opensrp['aws_secret_access_key']}") + String awsSecretAccessKey; + + @Value("#{opensrp['aws_region']}") + String awsRegion; + + @Value("#{opensrp['aws_bucket']}") + String awsBucket; + + @Value("#{opensrp['aws_key_folder']}") + String awsKeyFolder; + @Mock private MultimediaService multimediaService; - + @Autowired private MultimediaRepository multimediaRepository; - + @Autowired private ClientService clientService; - + @Autowired @Value("#{opensrp['multimedia.directory.name']}") private String multimediaDirPath; - + @Before public void setUp() throws Exception { initMocks(this); + multimediaService = new MultimediaService(multimediaRepository, clientService); } - + @Ignore @Test public void shouldSaveMultimediaFile() throws FileNotFoundException { MultimediaDTO multimedia = new MultimediaDTO("1234567891", "opensrp", "image/jpeg", "", "nid"); String baseDirPath = System.getProperty("user.home"); FileInputStream fis = new FileInputStream(baseDirPath + File.separator + ".OpenSRP/multimedia/image.jpeg"); - + MultipartFile multipartFile = null; - + try { multipartFile = new MockMultipartFile("file", fis); } @@ -65,43 +81,51 @@ public void shouldSaveMultimediaFile() throws FileNotFoundException { // TODO Auto-generated catch block e.printStackTrace(); } - + String status = multimediaService.saveMultimediaFile(multimedia, multipartFile); - + Assert.assertEquals("success", status); - + } - + @Ignore @Test public void shouldGetMultimediaFiles() throws FileNotFoundException { MultimediaDTO multimediaDTO = new MultimediaDTO("1234567890", "opensrp", "image/jpeg", "", "profile"); - + Multimedia expectedMultimedia = new Multimedia().withCaseId(multimediaDTO.getCaseId()) - .withProviderId(multimediaDTO.getProviderId()).withContentType(multimediaDTO.getContentType()) - .withFilePath(multimediaDTO.getFilePath()).withFileCategory(multimediaDTO.getFileCategory()); - + .withProviderId(multimediaDTO.getProviderId()).withContentType(multimediaDTO.getContentType()) + .withFilePath(multimediaDTO.getFilePath()).withFileCategory(multimediaDTO.getFileCategory()); + String baseDirPath = System.getProperty("user.home"); FileInputStream fis = new FileInputStream(baseDirPath + File.separator + ".OpenSRP/multimedia/image.jpeg"); MultipartFile multipartFile = null; - + try { multipartFile = new MockMultipartFile("file", fis); } catch (IOException e) { e.printStackTrace(); } - + boolean status = multimediaService.uploadFile(multimediaDTO, multipartFile); - + if (status) multimediaRepository.add(expectedMultimedia); - + List multimediaFiles = multimediaService.getMultimediaFiles("opensrp"); - + for (Multimedia actualMultimedia : multimediaFiles) { if (actualMultimedia.getCaseId().equals(multimediaDTO.getCaseId())) Assert.assertEquals(expectedMultimedia.getFilePath(), actualMultimedia.getFilePath()); } } + + @Test + public void shouldSaveImageTos3() throws Exception { + File fileImage = new File("/Users/ona/.OpenMRS/patient_images/7f58a6cf-51ec-4bb4-929e-5ffcd3b1fac3.jpg"); + Assert.assertTrue(multimediaService + .uploadImageTos3(fileImage, awsAccessKeyId, awsSecretAccessKey, awsRegion, awsBucket, awsKeyFolder)); + + } } From 2e85226775c7bacf8e9f651bc663394f95f17a49 Mon Sep 17 00:00:00 2001 From: manutarus Date: Mon, 4 Dec 2017 16:00:36 +0300 Subject: [PATCH 02/14] saving images to s3 --- assets/config/opensrp.properties | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/assets/config/opensrp.properties b/assets/config/opensrp.properties index 2a8eafe785..12e4ed024d 100644 --- a/assets/config/opensrp.properties +++ b/assets/config/opensrp.properties @@ -88,8 +88,8 @@ file.maxUploadSize=20971520 opensrp.site.url="" # S3 configuration -aws_access_key_id = AKIAJTCL2FYTPIJCBP2Q -aws_secret_access_key = 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 -aws_region = eu-west-1 -aws_bucket = opensrp-stage -aws_key_folder = media/zeir/patient_images/ +aws_access_key_id = "" +aws_secret_access_key = "" +aws_region = "" +aws_bucket = "" +aws_key_folder = "" From 7e193fd14f75e191e4772c68462b0dd7118ca92f Mon Sep 17 00:00:00 2001 From: manutarus Date: Mon, 4 Dec 2017 17:52:24 +0300 Subject: [PATCH 03/14] pass clearing DB during build --- pom.xml | 492 ++++++++++++++++++++++++++------------------------------ 1 file changed, 226 insertions(+), 266 deletions(-) diff --git a/pom.xml b/pom.xml index a6d9e6fe70..0a94882dcd 100644 --- a/pom.xml +++ b/pom.xml @@ -1,6 +1,6 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.opensrp @@ -8,7 +8,7 @@ pom 0.1-SNAPSHOT opensrp - http://github.com/OpenSRP/opensrp-server + http://github.com/OpenSRP/opensrp-server opensrp-common @@ -24,13 +24,13 @@ ${project.basedir} - 0.11 - org.motechproject.contrib-drishti - 0.1-SNAPSHOT - UTF-8 - 3.1.0.RELEASE + 0.11 + org.motechproject.contrib-drishti + 0.1-SNAPSHOT + UTF-8 + 3.1.0.RELEASE 5.1.36 - 1.9.7 + 1.9.7 always @@ -100,269 +100,229 @@ http://nexus.motechproject.org/content/repositories/releases - - - skip-integration-tests - - - - org.apache.maven.plugins - maven-failsafe-plugin - 2.9 - - - - integration-test - verify - - - -XX:MaxPermSize=1024M - - **/it/** - - true - - - - - - - - - integration-tests - - true - - - - - - - maven-antrun-plugin - 1.6 - - - Drop DB Before Unit and Repository Tests - process-test-classes - - - - - - - - - - - - - - - - - run - - - - CREATE DB OPENSRP - pre-integration-test - - - - - - - - - - - - - run - - - - - - org.codehaus.mojo - sql-maven-plugin - 1.5 - - - - mysql - mysql-connector-java - ${mysql.connector.version} - - + + + skip-integration-tests + + + + org.apache.maven.plugins + maven-failsafe-plugin + 2.9 + + + + integration-test + verify + + + -XX:MaxPermSize=1024M + + **/it/** + + true + + + + + + + + + integration-tests + + true + - - ${jdbc.driverClassName} - ${jdbc.url-wo-db} - ${jdbc.username} - ${jdbc.password} - ${maven.test.skip} - + + + + + maven-antrun-plugin + 1.6 + + + + + org.codehaus.mojo + sql-maven-plugin + 1.5 + + + + mysql + mysql-connector-java + ${mysql.connector.version} + + - - - - drop-create-relational-db - pre-integration-test - - execute - - - ${jdbc.url-wo-db} - ${jdbc.username} - ${jdbc.password} - true - - drop database if exists ${db.quartz}; - create database ${db.quartz}; - - abort - - - - - create-quartz-tables - pre-integration-test - - execute - - - ${jdbc.url-wo-db}/${db.quartz} - ${jdbc.username} - ${jdbc.password} - true - - ../build/sql/tables_quartz_${jdbc.backend}.sql - - - - - - - org.apache.maven.plugins - maven-failsafe-plugin - 2.9 - - - - integration-test - verify - - - -XX:MaxPermSize=1024M - - **/it/** - - true - - - - - - - - - + + ${jdbc.driverClassName} + ${jdbc.url-wo-db} + ${jdbc.username} + ${jdbc.password} + ${maven.test.skip} + + + + + + drop-create-relational-db + pre-integration-test + + execute + + + ${jdbc.url-wo-db} + ${jdbc.username} + ${jdbc.password} + true + + drop database if exists ${db.quartz}; + create database ${db.quartz}; + + abort + + + + + create-quartz-tables + pre-integration-test + + execute + + + ${jdbc.url-wo-db}/${db.quartz} + ${jdbc.username} + ${jdbc.password} + true + + ../build/sql/tables_quartz_${jdbc.backend}.sql + + + + + + + org.apache.maven.plugins + maven-failsafe-plugin + 2.9 + + + + integration-test + verify + + + -XX:MaxPermSize=1024M + + **/it/** + + true + + + + + + + + + - - - org.apache.maven.plugins - maven-compiler-plugin - 2.5.1 - - 1.7 - 1.7 - UTF-8 - - - - org.apache.maven.plugins - maven-resources-plugin - 2.5 - - UTF-8 - - - - - org.apache.maven.plugins - maven-deploy-plugin - 2.5 - + + + org.apache.maven.plugins + maven-compiler-plugin + 2.5.1 + + 1.7 + 1.7 + UTF-8 + + + + org.apache.maven.plugins + maven-resources-plugin + 2.5 + + UTF-8 + + + + + org.apache.maven.plugins + maven-deploy-plugin + 2.5 + + + + org.codehaus.mojo + properties-maven-plugin + 1.0-alpha-2 + + + initialize + + read-project-properties + + + + ${main.basedir}/build/maven.properties + ${main.basedir}/assets/config/opensrp.properties + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.20 + + true + + **/it/** + + + + + + + org.jacoco + jacoco-maven-plugin + 0.7.9 + + ${basedir}/target/coverage-reports/jacoco-unit.exec + ${basedir}/target/coverage-reports/jacoco-unit.exec + + + + jacoco-initialize + + prepare-agent + + + + jacoco-site + test + + report + + + + + + org.eluder.coveralls + coveralls-maven-plugin + 4.0.0 + - - org.codehaus.mojo - properties-maven-plugin - 1.0-alpha-2 - - - initialize - - read-project-properties - - - - ${main.basedir}/build/maven.properties - ${main.basedir}/assets/config/opensrp.properties - - - - - - - org.apache.maven.plugins - maven-surefire-plugin - 2.20 - - true - - **/it/** - - - - - - - org.jacoco - jacoco-maven-plugin - 0.7.9 - - ${basedir}/target/coverage-reports/jacoco-unit.exec - ${basedir}/target/coverage-reports/jacoco-unit.exec - - - - jacoco-initialize - - prepare-agent - - - - jacoco-site - test - - report - - - - - - org.eluder.coveralls - coveralls-maven-plugin - 4.0.0 - - - + From edeb13df2ce4bfc47f49e06e97e912c950c43d04 Mon Sep 17 00:00:00 2001 From: manutarus Date: Wed, 6 Dec 2017 19:15:47 +0300 Subject: [PATCH 04/14] adding image to s3 --- .../opensrp/service/MultimediaService.java | 119 ++++++++++-------- 1 file changed, 68 insertions(+), 51 deletions(-) diff --git a/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java b/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java index 78129e8d28..f79f32d940 100644 --- a/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java +++ b/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java @@ -8,6 +8,7 @@ import com.amazonaws.services.s3.model.PutObjectRequest; import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.digest.DigestUtils; +import org.json.JSONException; import org.opensrp.domain.Client; import org.opensrp.domain.Multimedia; import org.opensrp.dto.form.MultimediaDTO; @@ -52,8 +53,11 @@ public class MultimediaService { @Value("#{opensrp['aws_bucket']}") String awsBucket; - @Value("#{opensrp['aws_key_folder']}") - String awsKeyFolder; + @Value("#{opensrp['media_key_folder']}") + String mediaKeyFolder; + + @Value("#{opensrp['multimedia_directory_location']}") + String multimediaDirectoryLocation; @Autowired public MultimediaService(MultimediaRepository multimediaRepository, ClientService clientService) { @@ -62,76 +66,54 @@ public MultimediaService(MultimediaRepository multimediaRepository, ClientServic } public String saveMultimediaFile(MultimediaDTO multimediaDTO, MultipartFile file) { + String fileExtension = makeFileExtension(multimediaDTO); + + if (multimediaDirectoryLocation.equalsIgnoreCase("s3")) { + try { + File imageToSave = new File(multimediaDTO.getCaseId() + fileExtension); + file.transferTo(imageToSave); + uploadImageToS3(imageToSave, awsAccessKeyId, awsSecretAccessKey, awsRegion, awsBucket, mediaKeyFolder); + updateClientWithImage(multimediaDTO, fileExtension); + return "success"; + } + catch (IOException e) { + e.printStackTrace(); + return "fail"; + } + } if (uploadFile(multimediaDTO, file)) { + try { logger.info("Image path : " + multimediaDirPath); - Multimedia multimediaFile = new Multimedia().withCaseId(multimediaDTO.getCaseId()) .withProviderId(multimediaDTO.getProviderId()).withContentType(multimediaDTO.getContentType()) .withFilePath(multimediaDTO.getFilePath()).withFileCategory(multimediaDTO.getFileCategory()); - multimediaRepository.add(multimediaFile); - Client client = clientService.getByBaseEntityId(multimediaDTO.getCaseId()); - if (client != null) { - if (client.getAttribute("Patient Image") != null) { - client.removeAttribute("Patient Image"); - } - client.addAttribute("Patient Image", multimediaDTO.getCaseId() + ".jpg"); - client.setServerVersion(null); - clientService.updateClient(client); - } + updateClientWithImage(multimediaDTO, fileExtension); return "success"; } catch (Exception e) { e.getMessage(); + return "fail"; } } - return "fail"; } public boolean uploadFile(MultimediaDTO multimediaDTO, MultipartFile multimediaFile) { - // String baseMultimediaDirPath = System.getProperty("user.home"); - if (!multimediaFile.isEmpty()) { try { multimediaDirPath = baseMultimediaDirPath + File.separator; - String fileExt = ".jpg"; - switch (multimediaDTO.getContentType()) { - - case "application/octet-stream": - multimediaDirPath += VIDEOS_DIR; - fileExt = ".mp4"; - break; - - case "image/jpeg": - multimediaDirPath += IMAGES_DIR; - fileExt = ".jpg"; - break; - - case "image/gif": - multimediaDirPath += IMAGES_DIR; - fileExt = ".gif"; - break; - - case "image/png": - multimediaDirPath += IMAGES_DIR; - fileExt = ".png"; - break; - - } new File(multimediaDirPath).mkdir(); - String fileName = multimediaDirPath + File.separator + multimediaDTO.getCaseId() + fileExt; + String fileName = + multimediaDirPath + File.separator + multimediaDTO.getCaseId() + makeFileExtension(multimediaDTO); multimediaDTO.withFilePath(fileName); File multimediaDir = new File(fileName); - multimediaFile.transferTo(multimediaDir); - uploadImageTos3(multimediaDir, awsAccessKeyId, awsSecretAccessKey, awsRegion,awsBucket, awsKeyFolder); - return true; } @@ -152,8 +134,8 @@ public Multimedia findByCaseId(String entityId) { return multimediaRepository.findByCaseId(entityId); } - public boolean uploadImageTos3(File imageFile, String awsAccessKeyId, String awsSecretAccessKey, String awsRegion, - String awsBucket, String awsKeyFolder) { + public String uploadImageToS3(File imageFile, String awsAccessKeyId, String awsSecretAccessKey, String awsRegion, + String awsBucket, String awsKeyFolder) { AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials( new AWSStaticCredentialsProvider(new BasicAWSCredentials(awsAccessKeyId, awsSecretAccessKey))) @@ -167,19 +149,54 @@ public boolean uploadImageTos3(File imageFile, String awsAccessKeyId, String aws metadata.setContentMD5(new String(Base64.encodeBase64(md5))); PutObjectRequest request = new PutObjectRequest(awsBucket, awsKeyFolder + imageFile.getName(), inputStream, metadata); - // request.setCannedAcl(CannedAccessControlList.PublicRead); - // PutObjectResult result = s3Client.putObject(request); s3Client.putObject(request); - return true; + return "success"; } catch (FileNotFoundException e) { e.printStackTrace(); - return false; + return "fail"; } catch (IOException e) { e.printStackTrace(); - return false; + return "fail"; } + } + + public String makeFileExtension(MultimediaDTO multimediaDTO) { + switch (multimediaDTO.getContentType()) { + case "application/octet-stream": + multimediaDirPath += VIDEOS_DIR; + return ".mp4"; + + case "image/jpeg": + multimediaDirPath += IMAGES_DIR; + return ".jpg"; + + case "image/gif": + multimediaDirPath += IMAGES_DIR; + return ".gif"; + case "image/png": + multimediaDirPath += IMAGES_DIR; + return ".png"; + } + return ".jpg"; + } + + public void updateClientWithImage(MultimediaDTO multimediaDTO, String fileExtension) { + Client client = clientService.getByBaseEntityId(multimediaDTO.getCaseId()); + if (client != null) { + try { + if (client.getAttribute("Patient Image") != null) { + client.removeAttribute("Patient Image"); + } + client.addAttribute("Patient Image", multimediaDTO.getCaseId() + fileExtension); + client.setServerVersion(null); + clientService.updateClient(client); + } + catch (JSONException e) { + e.printStackTrace(); + } + } } } From 8317ca11d1edb3cd3deaa6349af8d1135e7d66af Mon Sep 17 00:00:00 2001 From: manutarus Date: Fri, 8 Dec 2017 15:42:31 +0300 Subject: [PATCH 05/14] added functionality to save image to s3 added functionality to download patient images from s3 to client --- assets/config/opensrp.properties | 17 +- opensrp-core/pom.xml | 12 +- .../opensrp/service/MultimediaService.java | 27 +- .../org/opensrp/MultimediaServiceTest.java | 92 +++++-- .../web/controller/MultimediaController.java | 230 ++++++++++++------ 5 files changed, 266 insertions(+), 112 deletions(-) diff --git a/assets/config/opensrp.properties b/assets/config/opensrp.properties index 12e4ed024d..8a43132706 100644 --- a/assets/config/opensrp.properties +++ b/assets/config/opensrp.properties @@ -6,7 +6,6 @@ mcts.phone.number=8762963816 js.directory.name=/ziggy form.directory.name=/form form.download.files=form.xml, model.xml, form_definition.json -multimedia.directory.name=../multimedia/opensrp qrcodes.directory.name=/home/opensrp/qr-codes/ schedule.config.path=/schedules/schedule-config.xls @@ -87,9 +86,15 @@ file.maxUploadSize=20971520 #opnenSRP Site url opensrp.site.url="" +#multimedia directory location - accepted values (s3 or local) +multimedia.directory.location = s3 + +#if local is preferred this path will be used to store images +multimedia.directory.name= "" + # S3 configuration -aws_access_key_id = "" -aws_secret_access_key = "" -aws_region = "" -aws_bucket = "" -aws_key_folder = "" +aws.access.key.id = "" +aws.secret.access.key = "" +aws.region = "" +aws.bucket = "" +aws.key.folder = "" diff --git a/opensrp-core/pom.xml b/opensrp-core/pom.xml index 5181cc285e..5c2557a226 100644 --- a/opensrp-core/pom.xml +++ b/opensrp-core/pom.xml @@ -25,7 +25,7 @@ ../assets/schedules schedules - + ../build true @@ -50,7 +50,7 @@ com.amazonaws aws-java-sdk - 1.11.238 + 1.11.244 motech-platform-server-api @@ -213,7 +213,7 @@ org.springframework.data spring-data-jpa 1.1.0.RELEASE - + nl.jqno.equalsverifier equalsverifier @@ -224,7 +224,7 @@ com.openpojo openpojo 0.8.6 - + org.hibernate @@ -245,7 +245,7 @@ test runtime - - + + diff --git a/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java b/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java index f79f32d940..98d3880b31 100644 --- a/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java +++ b/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java @@ -1,5 +1,7 @@ package org.opensrp.service; +import com.amazonaws.AmazonServiceException; +import com.amazonaws.SdkClientException; import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.services.s3.AmazonS3; @@ -41,22 +43,22 @@ public class MultimediaService { @Value("#{opensrp['multimedia.directory.name']}") String baseMultimediaDirPath; - @Value("#{opensrp['aws_access_key_id']}") + @Value("#{opensrp['aws.access.key.id']}") String awsAccessKeyId; - @Value("#{opensrp['aws_secret_access_key']}") + @Value("#{opensrp['aws.secret.access.key']}") String awsSecretAccessKey; - @Value("#{opensrp['aws_region']}") + @Value("#{opensrp['aws.region']}") String awsRegion; - @Value("#{opensrp['aws_bucket']}") + @Value("#{opensrp['aws.bucket']}") String awsBucket; - @Value("#{opensrp['media_key_folder']}") + @Value("#{opensrp['aws.key.folder']}") String mediaKeyFolder; - @Value("#{opensrp['multimedia_directory_location']}") + @Value("#{opensrp['multimedia.directory.location']}") String multimediaDirectoryLocation; @Autowired @@ -66,6 +68,7 @@ public MultimediaService(MultimediaRepository multimediaRepository, ClientServic } public String saveMultimediaFile(MultimediaDTO multimediaDTO, MultipartFile file) { + String fileExtension = makeFileExtension(multimediaDTO); if (multimediaDirectoryLocation.equalsIgnoreCase("s3")) { @@ -152,12 +155,20 @@ public String uploadImageToS3(File imageFile, String awsAccessKeyId, String awsS s3Client.putObject(request); return "success"; } + catch (AmazonServiceException e) { + logger.error("", e); + return "fail"; + } + catch (SdkClientException e) { + logger.error("", e); + return "fail"; + } catch (FileNotFoundException e) { - e.printStackTrace(); + logger.error("", e); return "fail"; } catch (IOException e) { - e.printStackTrace(); + logger.error("", e); return "fail"; } } diff --git a/opensrp-core/src/test/java/org/opensrp/MultimediaServiceTest.java b/opensrp-core/src/test/java/org/opensrp/MultimediaServiceTest.java index f4f9476e59..60c3c8ee65 100644 --- a/opensrp-core/src/test/java/org/opensrp/MultimediaServiceTest.java +++ b/opensrp-core/src/test/java/org/opensrp/MultimediaServiceTest.java @@ -1,5 +1,17 @@ package org.opensrp; +import com.amazonaws.AmazonClientException; +import com.amazonaws.AmazonServiceException; +import com.amazonaws.ClientConfiguration; +import com.amazonaws.auth.AWSStaticCredentialsProvider; +import com.amazonaws.auth.BasicAWSCredentials; +import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.AmazonS3ClientBuilder; +import com.amazonaws.services.s3.model.GetObjectRequest; +import com.amazonaws.services.s3.model.S3Object; +import com.amazonaws.services.s3.model.S3ObjectInputStream; +import com.amazonaws.util.IOUtils; +import org.apache.commons.io.FileUtils; import org.junit.Assert; import org.junit.Before; import org.junit.Ignore; @@ -18,10 +30,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.web.multipart.MultipartFile; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; +import java.io.*; +import java.net.URL; import java.util.List; import static org.mockito.MockitoAnnotations.initMocks; @@ -30,20 +40,20 @@ @ContextConfiguration("classpath:test-applicationContext-opensrp.xml") public class MultimediaServiceTest { - @Value("#{opensrp['aws_access_key_id']}") + @Value("#{opensrp['aws.access.key.id']}") String awsAccessKeyId; - @Value("#{opensrp['aws_secret_access_key']}") + @Value("#{opensrp['aws.secret.access.key']}") String awsSecretAccessKey; - @Value("#{opensrp['aws_region']}") + @Value("#{opensrp['aws.region']}") String awsRegion; - @Value("#{opensrp['aws_bucket']}") + @Value("#{opensrp['aws.bucket']}") String awsBucket; - @Value("#{opensrp['aws_key_folder']}") - String awsKeyFolder; + @Value("#{opensrp['aws.key.folder']}") + String mediaKeyFolder; @Mock private MultimediaService multimediaService; @@ -58,10 +68,20 @@ public class MultimediaServiceTest { @Value("#{opensrp['multimedia.directory.name']}") private String multimediaDirPath; + public static final int MAX_ERROR_RETRY = 5; + + public static final int MAX_CONNECTIONS = 5000; + + public static final int MAX_CONNECTION_TIME_OUT = 5000; + + public static final int SOCKET_TIME_OUT = 5000; + + URL url; + @Before public void setUp() throws Exception { initMocks(this); - + url = new URL("https://www.dropbox.com/s/cwazpyy2mipj865/gimage.png"); multimediaService = new MultimediaService(multimediaRepository, clientService); } @@ -123,9 +143,53 @@ public void shouldGetMultimediaFiles() throws FileNotFoundException { @Test public void shouldSaveImageTos3() throws Exception { - File fileImage = new File("/Users/ona/.OpenMRS/patient_images/7f58a6cf-51ec-4bb4-929e-5ffcd3b1fac3.jpg"); - Assert.assertTrue(multimediaService - .uploadImageTos3(fileImage, awsAccessKeyId, awsSecretAccessKey, awsRegion, awsBucket, awsKeyFolder)); + File fileImage = new File("s3image.jpg"); + FileUtils.copyURLToFile(url, fileImage); + Assert.assertEquals("success", multimediaService + .uploadImageToS3(fileImage, awsAccessKeyId, awsSecretAccessKey, awsRegion, awsBucket, mediaKeyFolder)); + + } + @Test + public void shouldDownloadImageFroms3() throws Exception { + + File expectedImage = new File("s3image.jpg"); + FileUtils.copyURLToFile(url, expectedImage); + ClientConfiguration clientConfiguration = new ClientConfiguration().withMaxErrorRetry(MAX_ERROR_RETRY) + .withMaxConnections(MAX_CONNECTIONS).withConnectionTimeout(MAX_CONNECTION_TIME_OUT) + .withSocketTimeout(SOCKET_TIME_OUT); + + AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials( + new AWSStaticCredentialsProvider(new BasicAWSCredentials(awsAccessKeyId, awsSecretAccessKey))) + .withClientConfiguration(clientConfiguration).withRegion(awsRegion).build(); + + OutputStream fetchedOutputStream = null; + OutputStream expectedOutputStream = null; + try { + S3Object object = s3Client.getObject(new GetObjectRequest(awsBucket, mediaKeyFolder + "gimage.png")); + S3ObjectInputStream objectContent = object.getObjectContent(); + IOUtils.copy(objectContent, fetchedOutputStream); + + byte[] buffer = new byte[1024]; + int count; + FileInputStream fileInputStream = new FileInputStream(expectedImage); + while ((count = fileInputStream.read(buffer)) >= 0) { + expectedOutputStream.write(buffer, 0, count); + } + Assert.assertEquals(expectedOutputStream, fetchedOutputStream); + expectedOutputStream.flush(); + fetchedOutputStream.flush(); + object.close(); + objectContent.close(); + } + catch (AmazonServiceException aws) { + aws.printStackTrace(); + } + catch (AmazonClientException aws) { + aws.printStackTrace(); + } + catch (IOException io) { + io.printStackTrace(); + } } } diff --git a/opensrp-web/src/main/java/org/opensrp/web/controller/MultimediaController.java b/opensrp-web/src/main/java/org/opensrp/web/controller/MultimediaController.java index d2b5d1fb61..44b0327c2a 100644 --- a/opensrp-web/src/main/java/org/opensrp/web/controller/MultimediaController.java +++ b/opensrp-web/src/main/java/org/opensrp/web/controller/MultimediaController.java @@ -1,18 +1,16 @@ package org.opensrp.web.controller; -import static org.springframework.web.bind.annotation.RequestMethod.POST; - -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.URLConnection; -import java.nio.charset.Charset; - -import javax.servlet.http.HttpServletResponse; - +import com.amazonaws.AmazonClientException; +import com.amazonaws.AmazonServiceException; +import com.amazonaws.ClientConfiguration; +import com.amazonaws.auth.AWSStaticCredentialsProvider; +import com.amazonaws.auth.BasicAWSCredentials; +import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.AmazonS3ClientBuilder; +import com.amazonaws.services.s3.model.GetObjectRequest; +import com.amazonaws.services.s3.model.S3Object; +import com.amazonaws.services.s3.model.S3ObjectInputStream; +import com.google.gson.Gson; import org.opensrp.domain.Multimedia; import org.opensrp.dto.form.MultimediaDTO; import org.opensrp.service.MultimediaService; @@ -28,37 +26,67 @@ import org.springframework.security.core.Authentication; import org.springframework.stereotype.Controller; import org.springframework.util.FileCopyUtils; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestHeader; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; -import com.google.gson.Gson; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.net.URLConnection; +import java.nio.charset.Charset; + +import static org.springframework.web.bind.annotation.RequestMethod.POST; @Controller @RequestMapping("/multimedia") public class MultimediaController { - + private static Logger logger = LoggerFactory.getLogger(MultimediaController.class.toString()); - + + public static final int MAX_ERROR_RETRY = 5; + + public static final int MAX_CONNECTIONS = 5000; + + public static final int MAX_CONNECTION_TIME_OUT = 5000; + + public static final int SOCKET_TIME_OUT = 5000; + @Value("#{opensrp['multimedia.directory.name']}") String multiMediaDir; - + + @Value("#{opensrp['multimedia.directory.name']}") + String baseMultimediaDirPath; + + @Value("#{opensrp['aws.access.key.id']}") + String awsAccessKeyId; + + @Value("#{opensrp['aws.secret.access.key']}") + String awsSecretAccessKey; + + @Value("#{opensrp['aws.region']}") + String awsRegion; + + @Value("#{opensrp['aws.bucket']}") + String awsBucket; + + @Value("#{opensrp['aws.key.folder']}") + String mediaKeyFolder; + + @Value("#{opensrp['multimedia.directory.location']}") + String multimediaDirectoryLocation; + @Autowired @Qualifier("drishtiAuthenticationProvider") DrishtiAuthenticationProvider provider; - + @Autowired MultimediaService multimediaService; - + /** * Download a file from the multimedia directory. The method also assumes two file types mp4 and * images whereby all images are stored in the images folder and videos in mp4 in the multimedia * directory This method is set to bypass spring security config but authenticate through the * username/password passed at the headers - * + * * @param response * @param fileName * @param userName @@ -68,16 +96,15 @@ public class MultimediaController { @RequestMapping(value = "/download/{fileName:.+}", method = RequestMethod.GET) public void downloadFile(HttpServletResponse response, @PathVariable("fileName") String fileName, @RequestHeader(value = "username") String userName, - @RequestHeader(value = "password") String password) - throws Exception { - + @RequestHeader(value = "password") String password) throws Exception { + try { if (authenticate(userName, password).isAuthenticated()) { File file = new File(multiMediaDir + File.separator + "images" + File.separator + fileName); if (fileName.endsWith("mp4")) { file = new File(multiMediaDir + File.separator + "videos" + File.separator + fileName); } - + downloadFile(file, response); } } @@ -85,11 +112,11 @@ public void downloadFile(HttpServletResponse response, @PathVariable("fileName") logger.error("", e); } } - + /** * This method downloads a file from the server given the client id. A search is made to the * multimedia repo to see if any file exists mapped to the user whereby the filepath is recorded - * + * * @param response * @param baseEntityId * @param userName @@ -99,95 +126,142 @@ public void downloadFile(HttpServletResponse response, @PathVariable("fileName") @RequestMapping(value = "/profileimage/{baseEntityId}", method = RequestMethod.GET) public void downloadFileByClientId(HttpServletResponse response, @PathVariable("baseEntityId") String baseEntityId, @RequestHeader(value = "username") String userName, - @RequestHeader(value = "password") String password) - throws Exception { - + @RequestHeader(value = "password") String password) throws Exception { try { - if (authenticate(userName, password).isAuthenticated()) { - - Multimedia multiMedia = multimediaService.findByCaseId(baseEntityId); - if (multiMedia == null || multiMedia.getFilePath() == null) { - //see if the file exists in the disk with the assumption that it's .jpg otherwise return error msg - File file = new File(multiMediaDir + File.separator + MultimediaService.IMAGES_DIR + File.separator - + baseEntityId + ".jpg"); - if (file.exists()) { - downloadFile(file, response); - } else { - String errorMessage = "Sorry. The file you are looking for does not exist"; - logger.info(errorMessage); - OutputStream outputStream = response.getOutputStream(); - outputStream.write(errorMessage.getBytes(Charset.forName("UTF-8"))); - outputStream.close(); - return; + if (multimediaDirectoryLocation.equalsIgnoreCase("s3")) { + downloadFileFromS3(new File(baseEntityId + ".jpg"), awsAccessKeyId, awsSecretAccessKey, awsRegion, awsBucket, + mediaKeyFolder, response); + } else { + if (authenticate(userName, password).isAuthenticated()) { + Multimedia multiMedia = multimediaService.findByCaseId(baseEntityId); + if (multiMedia == null || multiMedia.getFilePath() == null) { + //see if the file exists in the disk with the assumption that it's .jpg otherwise return error msg + File file = new File( + multiMediaDir + File.separator + MultimediaService.IMAGES_DIR + File.separator + baseEntityId + + ".jpg"); + if (file.exists()) { + downloadFile(file, response); + } else { + setErrorMessage(response); + return; + } } + String filePath = multiMedia.getFilePath(); + + File file = new File(filePath); + downloadFile(file, response); } - String filePath = multiMedia.getFilePath(); - - File file = new File(filePath); - downloadFile(file, response); } } catch (Exception e) { logger.error("", e); } - + } - + @RequestMapping(headers = { "Accept=multipart/form-data" }, method = POST, value = "/upload") public ResponseEntity uploadFiles(@RequestParam("anm-id") String providerId, @RequestParam("entity-id") String entityId, @RequestParam("file-category") String fileCategory, @RequestParam("file") MultipartFile file) { - + String contentType = file.getContentType(); - + MultimediaDTO multimediaDTO = new MultimediaDTO(entityId, providerId, contentType, null, fileCategory); - + String status = multimediaService.saveMultimediaFile(multimediaDTO, file); - + return new ResponseEntity<>(new Gson().toJson(status), HttpStatus.OK); } - + private Authentication authenticate(String userName, String password) { Authentication auth = new UsernamePasswordAuthenticationToken(userName, password); auth = provider.authenticate(auth); return auth; } - + private void downloadFile(File file, HttpServletResponse response) throws Exception { - + if (!file.exists()) { - String errorMessage = "Sorry. The file you are looking for does not exist"; - logger.info(errorMessage); - OutputStream outputStream = response.getOutputStream(); - outputStream.write(errorMessage.getBytes(Charset.forName("UTF-8"))); - outputStream.close(); + setErrorMessage(response); return; } - + String mimeType = URLConnection.guessContentTypeFromName(file.getName()); if (mimeType == null) { logger.info("mimetype is not detectable, will take default"); mimeType = "application/octet-stream"; } - + logger.info("mimetype : " + mimeType); - + response.setContentType(mimeType); - - /* "Content-Disposition : inline" will show viewable types [like images/text/pdf/anything viewable by browser] right on browser + + /* "Content-Disposition : inline" will show viewable types [like images/text/pdf/anything viewable by browser] right on browser while others(zip e.g) will be directly downloaded [may provide save as popup, based on your browser setting.]*/ response.setHeader("Content-Disposition", String.format("inline; filename=\"" + file.getName() + "\"")); - + /* "Content-Disposition : attachment" will be directly download, may provide save as popup, based on your browser setting*/ //response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getName())); - + response.setContentLength((int) file.length()); - + InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); - + //Copy bytes from source to destination(outputstream in this example), closes both streams. FileCopyUtils.copy(inputStream, response.getOutputStream()); } - + + private void downloadFileFromS3(File imageFile, String awsAccessKeyId, String awsSecretAccessKey, String awsRegion, + String awsBucket, String awsKeyFolder, HttpServletResponse response) throws Exception { + // client configuration optimization and socket timeout + ClientConfiguration clientConfiguration = new ClientConfiguration().withMaxErrorRetry(MAX_ERROR_RETRY) + .withMaxConnections(MAX_CONNECTIONS).withConnectionTimeout(MAX_CONNECTION_TIME_OUT) + .withSocketTimeout(SOCKET_TIME_OUT); + // instantiate s3 client with configs + AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials( + new AWSStaticCredentialsProvider(new BasicAWSCredentials(awsAccessKeyId, awsSecretAccessKey))) + .withClientConfiguration(clientConfiguration).withRegion(awsRegion).build(); + + String mimeType = URLConnection.guessContentTypeFromName(imageFile.getName()); + if (mimeType == null) { + mimeType = "application/octet-stream"; + } + + response.setContentType(mimeType); + response.setHeader("Content-Disposition", String.format("inline; filename=\"" + imageFile.getName() + "\"")); + response.setContentLength((int) imageFile.length()); + try { + S3Object object = s3Client.getObject(new GetObjectRequest(awsBucket, awsKeyFolder + imageFile.getName())); + S3ObjectInputStream objectContent = object.getObjectContent(); + // copy input stream contents to file + FileCopyUtils.copy(objectContent, response.getOutputStream()); + object.close(); + objectContent.close(); + } + catch (AmazonServiceException aws) { + aws.printStackTrace(); + } + catch (AmazonClientException aws) { + aws.printStackTrace(); + } + catch (IOException io) { + io.printStackTrace(); + } + } + + private void setErrorMessage(HttpServletResponse response) { + String errorMessage = "Sorry. The file you are looking for does not exist"; + try { + logger.info(errorMessage); + OutputStream outputStream = response.getOutputStream(); + outputStream.write(errorMessage.getBytes(Charset.forName("UTF-8"))); + outputStream.close(); + } + catch (IOException e) { + e.printStackTrace(); + } + + } + } From c083e7b6a8e9b9fdce830a7cd1017199dcf92994 Mon Sep 17 00:00:00 2001 From: manutarus Date: Fri, 8 Dec 2017 15:55:48 +0300 Subject: [PATCH 06/14] added functionality to save image to s3 added functionality to download patient images from s3 to client --- .../src/test/java/org/opensrp/MultimediaServiceTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/opensrp-core/src/test/java/org/opensrp/MultimediaServiceTest.java b/opensrp-core/src/test/java/org/opensrp/MultimediaServiceTest.java index 60c3c8ee65..01fd0173e7 100644 --- a/opensrp-core/src/test/java/org/opensrp/MultimediaServiceTest.java +++ b/opensrp-core/src/test/java/org/opensrp/MultimediaServiceTest.java @@ -140,7 +140,7 @@ public void shouldGetMultimediaFiles() throws FileNotFoundException { Assert.assertEquals(expectedMultimedia.getFilePath(), actualMultimedia.getFilePath()); } } - + @Ignore @Test public void shouldSaveImageTos3() throws Exception { File fileImage = new File("s3image.jpg"); @@ -149,7 +149,7 @@ public void shouldSaveImageTos3() throws Exception { .uploadImageToS3(fileImage, awsAccessKeyId, awsSecretAccessKey, awsRegion, awsBucket, mediaKeyFolder)); } - + @Ignore @Test public void shouldDownloadImageFroms3() throws Exception { From 77bf64be50412e971f05a0ea63f14823cb058c45 Mon Sep 17 00:00:00 2001 From: manutarus Date: Fri, 8 Dec 2017 16:56:26 +0300 Subject: [PATCH 07/14] added functionality to save image to s3 added functionality to download patient images from s3 to client --- .../opensrp/service/MultimediaService.java | 51 +++++++++++-------- .../org/opensrp/MultimediaServiceTest.java | 48 +++++++++-------- .../web/controller/MultimediaController.java | 38 +++++++++----- 3 files changed, 83 insertions(+), 54 deletions(-) diff --git a/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java b/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java index 98d3880b31..871517d6fe 100644 --- a/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java +++ b/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java @@ -22,7 +22,11 @@ import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; -import java.io.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.FileNotFoundException; +import java.io.InputStream; import java.util.List; @Service @@ -41,25 +45,31 @@ public class MultimediaService { private String multimediaDirPath; @Value("#{opensrp['multimedia.directory.name']}") - String baseMultimediaDirPath; + private String baseMultimediaDirPath; @Value("#{opensrp['aws.access.key.id']}") - String awsAccessKeyId; + private String awsAccessKeyId; @Value("#{opensrp['aws.secret.access.key']}") - String awsSecretAccessKey; + private String awsSecretAccessKey; @Value("#{opensrp['aws.region']}") - String awsRegion; + private String awsRegion; @Value("#{opensrp['aws.bucket']}") - String awsBucket; + private String awsBucket; @Value("#{opensrp['aws.key.folder']}") String mediaKeyFolder; @Value("#{opensrp['multimedia.directory.location']}") - String multimediaDirectoryLocation; + private String multimediaDirectoryLocation; + + private String successResponse ="success"; + + private String failedResponse ="fail"; + + private String imageAttribute ="Patient Image"; @Autowired public MultimediaService(MultimediaRepository multimediaRepository, ClientService clientService) { @@ -77,11 +87,11 @@ public String saveMultimediaFile(MultimediaDTO multimediaDTO, MultipartFile file file.transferTo(imageToSave); uploadImageToS3(imageToSave, awsAccessKeyId, awsSecretAccessKey, awsRegion, awsBucket, mediaKeyFolder); updateClientWithImage(multimediaDTO, fileExtension); - return "success"; + return successResponse; } catch (IOException e) { e.printStackTrace(); - return "fail"; + return failedResponse; } } @@ -94,14 +104,14 @@ public String saveMultimediaFile(MultimediaDTO multimediaDTO, MultipartFile file .withFilePath(multimediaDTO.getFilePath()).withFileCategory(multimediaDTO.getFileCategory()); multimediaRepository.add(multimediaFile); updateClientWithImage(multimediaDTO, fileExtension); - return "success"; + return successResponse; } catch (Exception e) { e.getMessage(); - return "fail"; + return failedResponse; } } - return "fail"; + return failedResponse; } public boolean uploadFile(MultimediaDTO multimediaDTO, MultipartFile multimediaFile) { @@ -118,7 +128,6 @@ public boolean uploadFile(MultimediaDTO multimediaDTO, MultipartFile multimediaF multimediaFile.transferTo(multimediaDir); return true; - } catch (Exception e) { logger.error("", e); @@ -153,23 +162,23 @@ public String uploadImageToS3(File imageFile, String awsAccessKeyId, String awsS PutObjectRequest request = new PutObjectRequest(awsBucket, awsKeyFolder + imageFile.getName(), inputStream, metadata); s3Client.putObject(request); - return "success"; + return successResponse; } catch (AmazonServiceException e) { logger.error("", e); - return "fail"; + return failedResponse; } catch (SdkClientException e) { logger.error("", e); - return "fail"; + return failedResponse; } catch (FileNotFoundException e) { logger.error("", e); - return "fail"; + return failedResponse; } catch (IOException e) { logger.error("", e); - return "fail"; + return failedResponse; } } @@ -198,10 +207,10 @@ public void updateClientWithImage(MultimediaDTO multimediaDTO, String fileExtens Client client = clientService.getByBaseEntityId(multimediaDTO.getCaseId()); if (client != null) { try { - if (client.getAttribute("Patient Image") != null) { - client.removeAttribute("Patient Image"); + if (client.getAttribute(imageAttribute) != null) { + client.removeAttribute(imageAttribute); } - client.addAttribute("Patient Image", multimediaDTO.getCaseId() + fileExtension); + client.addAttribute(imageAttribute, multimediaDTO.getCaseId() + fileExtension); client.setServerVersion(null); clientService.updateClient(client); } diff --git a/opensrp-core/src/test/java/org/opensrp/MultimediaServiceTest.java b/opensrp-core/src/test/java/org/opensrp/MultimediaServiceTest.java index 01fd0173e7..995bf5271d 100644 --- a/opensrp-core/src/test/java/org/opensrp/MultimediaServiceTest.java +++ b/opensrp-core/src/test/java/org/opensrp/MultimediaServiceTest.java @@ -18,6 +18,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.mockito.MockitoAnnotations; import org.opensrp.domain.Multimedia; import org.opensrp.dto.form.MultimediaDTO; import org.opensrp.repository.MultimediaRepository; @@ -30,30 +31,33 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.web.multipart.MultipartFile; -import java.io.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.FileNotFoundException; + import java.net.URL; import java.util.List; -import static org.mockito.MockitoAnnotations.initMocks; - @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:test-applicationContext-opensrp.xml") public class MultimediaServiceTest { @Value("#{opensrp['aws.access.key.id']}") - String awsAccessKeyId; + private String awsAccessKeyId; @Value("#{opensrp['aws.secret.access.key']}") - String awsSecretAccessKey; + private String awsSecretAccessKey; @Value("#{opensrp['aws.region']}") - String awsRegion; + private String awsRegion; @Value("#{opensrp['aws.bucket']}") - String awsBucket; + private String awsBucket; @Value("#{opensrp['aws.key.folder']}") - String mediaKeyFolder; + private String mediaKeyFolder; @Mock private MultimediaService multimediaService; @@ -68,20 +72,25 @@ public class MultimediaServiceTest { @Value("#{opensrp['multimedia.directory.name']}") private String multimediaDirPath; - public static final int MAX_ERROR_RETRY = 5; + private static final int MAX_ERROR_RETRY = 5; + + private static final int MAX_CONNECTIONS = 5000; + + private static final int MAX_CONNECTION_TIME_OUT = 5000; - public static final int MAX_CONNECTIONS = 5000; + private static final int SOCKET_TIME_OUT = 5000; - public static final int MAX_CONNECTION_TIME_OUT = 5000; + public static final int BUFFER_SIZE = 1024; - public static final int SOCKET_TIME_OUT = 5000; + private URL url; - URL url; + private File expectedImage; @Before public void setUp() throws Exception { - initMocks(this); + MockitoAnnotations.initMocks(this); url = new URL("https://www.dropbox.com/s/cwazpyy2mipj865/gimage.png"); + expectedImage = new File("s3image.jpg"); multimediaService = new MultimediaService(multimediaRepository, clientService); } @@ -140,20 +149,19 @@ public void shouldGetMultimediaFiles() throws FileNotFoundException { Assert.assertEquals(expectedMultimedia.getFilePath(), actualMultimedia.getFilePath()); } } + @Ignore @Test public void shouldSaveImageTos3() throws Exception { - File fileImage = new File("s3image.jpg"); - FileUtils.copyURLToFile(url, fileImage); + FileUtils.copyURLToFile(url, expectedImage); Assert.assertEquals("success", multimediaService - .uploadImageToS3(fileImage, awsAccessKeyId, awsSecretAccessKey, awsRegion, awsBucket, mediaKeyFolder)); + .uploadImageToS3(expectedImage, awsAccessKeyId, awsSecretAccessKey, awsRegion, awsBucket, mediaKeyFolder)); } + @Ignore @Test public void shouldDownloadImageFroms3() throws Exception { - - File expectedImage = new File("s3image.jpg"); FileUtils.copyURLToFile(url, expectedImage); ClientConfiguration clientConfiguration = new ClientConfiguration().withMaxErrorRetry(MAX_ERROR_RETRY) .withMaxConnections(MAX_CONNECTIONS).withConnectionTimeout(MAX_CONNECTION_TIME_OUT) @@ -170,7 +178,7 @@ public void shouldDownloadImageFroms3() throws Exception { S3ObjectInputStream objectContent = object.getObjectContent(); IOUtils.copy(objectContent, fetchedOutputStream); - byte[] buffer = new byte[1024]; + byte[] buffer = new byte[BUFFER_SIZE]; int count; FileInputStream fileInputStream = new FileInputStream(expectedImage); while ((count = fileInputStream.read(buffer)) >= 0) { diff --git a/opensrp-web/src/main/java/org/opensrp/web/controller/MultimediaController.java b/opensrp-web/src/main/java/org/opensrp/web/controller/MultimediaController.java index 44b0327c2a..c6972316d6 100644 --- a/opensrp-web/src/main/java/org/opensrp/web/controller/MultimediaController.java +++ b/opensrp-web/src/main/java/org/opensrp/web/controller/MultimediaController.java @@ -26,11 +26,22 @@ import org.springframework.security.core.Authentication; import org.springframework.stereotype.Controller; import org.springframework.util.FileCopyUtils; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; -import java.io.*; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.BufferedInputStream; + import java.net.URLConnection; import java.nio.charset.Charset; @@ -51,33 +62,35 @@ public class MultimediaController { public static final int SOCKET_TIME_OUT = 5000; @Value("#{opensrp['multimedia.directory.name']}") - String multiMediaDir; + private String multiMediaDir; @Value("#{opensrp['multimedia.directory.name']}") - String baseMultimediaDirPath; + private String baseMultimediaDirPath; @Value("#{opensrp['aws.access.key.id']}") - String awsAccessKeyId; + private String awsAccessKeyId; @Value("#{opensrp['aws.secret.access.key']}") - String awsSecretAccessKey; + private String awsSecretAccessKey; @Value("#{opensrp['aws.region']}") - String awsRegion; + private String awsRegion; @Value("#{opensrp['aws.bucket']}") - String awsBucket; + private String awsBucket; @Value("#{opensrp['aws.key.folder']}") - String mediaKeyFolder; + private String mediaKeyFolder; @Value("#{opensrp['multimedia.directory.location']}") - String multimediaDirectoryLocation; + private String multimediaDirectoryLocation; @Autowired @Qualifier("drishtiAuthenticationProvider") DrishtiAuthenticationProvider provider; + private String fileExtension = ".jpg"; + @Autowired MultimediaService multimediaService; @@ -129,7 +142,7 @@ public void downloadFileByClientId(HttpServletResponse response, @PathVariable(" @RequestHeader(value = "password") String password) throws Exception { try { if (multimediaDirectoryLocation.equalsIgnoreCase("s3")) { - downloadFileFromS3(new File(baseEntityId + ".jpg"), awsAccessKeyId, awsSecretAccessKey, awsRegion, awsBucket, + downloadFileFromS3(new File(baseEntityId + fileExtension), awsAccessKeyId, awsSecretAccessKey, awsRegion, awsBucket, mediaKeyFolder, response); } else { if (authenticate(userName, password).isAuthenticated()) { @@ -138,7 +151,7 @@ public void downloadFileByClientId(HttpServletResponse response, @PathVariable(" //see if the file exists in the disk with the assumption that it's .jpg otherwise return error msg File file = new File( multiMediaDir + File.separator + MultimediaService.IMAGES_DIR + File.separator + baseEntityId - + ".jpg"); + + fileExtension); if (file.exists()) { downloadFile(file, response); } else { @@ -263,5 +276,4 @@ private void setErrorMessage(HttpServletResponse response) { } } - } From 87d3334b6f44b5af783900044c1880e81823d73a Mon Sep 17 00:00:00 2001 From: manutarus Date: Sun, 10 Dec 2017 14:08:29 +0300 Subject: [PATCH 08/14] s3 funtianlity --- .../org/opensrp/service/MultimediaService.java | 5 +++-- .../web/controller/MultimediaController.java | 17 ++++------------- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java b/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java index 871517d6fe..8fc8b06186 100644 --- a/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java +++ b/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java @@ -60,7 +60,7 @@ public class MultimediaService { private String awsBucket; @Value("#{opensrp['aws.key.folder']}") - String mediaKeyFolder; + private String mediaKeyFolder; @Value("#{opensrp['multimedia.directory.location']}") private String multimediaDirectoryLocation; @@ -199,8 +199,9 @@ public String makeFileExtension(MultimediaDTO multimediaDTO) { case "image/png": multimediaDirPath += IMAGES_DIR; return ".png"; + default: + return ".jpg"; } - return ".jpg"; } public void updateClientWithImage(MultimediaDTO multimediaDTO, String fileExtension) { diff --git a/opensrp-web/src/main/java/org/opensrp/web/controller/MultimediaController.java b/opensrp-web/src/main/java/org/opensrp/web/controller/MultimediaController.java index c6972316d6..cbae846a81 100644 --- a/opensrp-web/src/main/java/org/opensrp/web/controller/MultimediaController.java +++ b/opensrp-web/src/main/java/org/opensrp/web/controller/MultimediaController.java @@ -26,22 +26,16 @@ import org.springframework.security.core.Authentication; import org.springframework.stereotype.Controller; import org.springframework.util.FileCopyUtils; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestHeader; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; - import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.BufferedInputStream; - import java.net.URLConnection; import java.nio.charset.Charset; @@ -62,10 +56,7 @@ public class MultimediaController { public static final int SOCKET_TIME_OUT = 5000; @Value("#{opensrp['multimedia.directory.name']}") - private String multiMediaDir; - - @Value("#{opensrp['multimedia.directory.name']}") - private String baseMultimediaDirPath; + private String multiMediaDir; @Value("#{opensrp['aws.access.key.id']}") private String awsAccessKeyId; @@ -142,8 +133,8 @@ public void downloadFileByClientId(HttpServletResponse response, @PathVariable(" @RequestHeader(value = "password") String password) throws Exception { try { if (multimediaDirectoryLocation.equalsIgnoreCase("s3")) { - downloadFileFromS3(new File(baseEntityId + fileExtension), awsAccessKeyId, awsSecretAccessKey, awsRegion, awsBucket, - mediaKeyFolder, response); + downloadFileFromS3(new File(baseEntityId + fileExtension), awsAccessKeyId, awsSecretAccessKey, awsRegion, + awsBucket, mediaKeyFolder, response); } else { if (authenticate(userName, password).isAuthenticated()) { Multimedia multiMedia = multimediaService.findByCaseId(baseEntityId); From 23e4c7c87e0b6085c933000f96325b5153987c80 Mon Sep 17 00:00:00 2001 From: manutarus Date: Sun, 10 Dec 2017 14:51:12 +0300 Subject: [PATCH 09/14] s3 funtianlity --- .../org/opensrp/service/MultimediaService.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java b/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java index 8fc8b06186..d09d6b70a4 100644 --- a/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java +++ b/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java @@ -65,11 +65,11 @@ public class MultimediaService { @Value("#{opensrp['multimedia.directory.location']}") private String multimediaDirectoryLocation; - private String successResponse ="success"; + private String successResponse = "success"; - private String failedResponse ="fail"; + private String failedResponse = "fail"; - private String imageAttribute ="Patient Image"; + private String imageAttribute = "Patient Image"; @Autowired public MultimediaService(MultimediaRepository multimediaRepository, ClientService clientService) { @@ -188,10 +188,6 @@ public String makeFileExtension(MultimediaDTO multimediaDTO) { multimediaDirPath += VIDEOS_DIR; return ".mp4"; - case "image/jpeg": - multimediaDirPath += IMAGES_DIR; - return ".jpg"; - case "image/gif": multimediaDirPath += IMAGES_DIR; return ".gif"; @@ -199,8 +195,8 @@ public String makeFileExtension(MultimediaDTO multimediaDTO) { case "image/png": multimediaDirPath += IMAGES_DIR; return ".png"; - default: - return ".jpg"; + default: + return ".jpg"; } } From 6c1fa309f08786c62d5545e3f7aa53f1bcb65b54 Mon Sep 17 00:00:00 2001 From: manutarus Date: Sun, 10 Dec 2017 15:08:52 +0300 Subject: [PATCH 10/14] s3 funtianlity --- .../src/main/java/org/opensrp/service/MultimediaService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java b/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java index d09d6b70a4..7228036ecc 100644 --- a/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java +++ b/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java @@ -82,8 +82,8 @@ public String saveMultimediaFile(MultimediaDTO multimediaDTO, MultipartFile file String fileExtension = makeFileExtension(multimediaDTO); if (multimediaDirectoryLocation.equalsIgnoreCase("s3")) { - try { File imageToSave = new File(multimediaDTO.getCaseId() + fileExtension); + try { file.transferTo(imageToSave); uploadImageToS3(imageToSave, awsAccessKeyId, awsSecretAccessKey, awsRegion, awsBucket, mediaKeyFolder); updateClientWithImage(multimediaDTO, fileExtension); @@ -93,10 +93,10 @@ public String saveMultimediaFile(MultimediaDTO multimediaDTO, MultipartFile file e.printStackTrace(); return failedResponse; } + } if (uploadFile(multimediaDTO, file)) { - try { logger.info("Image path : " + multimediaDirPath); Multimedia multimediaFile = new Multimedia().withCaseId(multimediaDTO.getCaseId()) From 37cc05e5a7bcfb373a565a2c10d2a3a43fd14e56 Mon Sep 17 00:00:00 2001 From: manutarus Date: Sun, 10 Dec 2017 15:09:01 +0300 Subject: [PATCH 11/14] s3 funtianlity --- .../org/opensrp/web/controller/MultimediaController.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/opensrp-web/src/main/java/org/opensrp/web/controller/MultimediaController.java b/opensrp-web/src/main/java/org/opensrp/web/controller/MultimediaController.java index cbae846a81..34e89b4259 100644 --- a/opensrp-web/src/main/java/org/opensrp/web/controller/MultimediaController.java +++ b/opensrp-web/src/main/java/org/opensrp/web/controller/MultimediaController.java @@ -26,7 +26,11 @@ import org.springframework.security.core.Authentication; import org.springframework.stereotype.Controller; import org.springframework.util.FileCopyUtils; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; From f64f3d149ceb2cc4e042c87b8ce57816846abdc0 Mon Sep 17 00:00:00 2001 From: manutarus Date: Sun, 10 Dec 2017 15:17:38 +0300 Subject: [PATCH 12/14] s3 funtianlity --- .../src/main/java/org/opensrp/service/MultimediaService.java | 1 - 1 file changed, 1 deletion(-) diff --git a/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java b/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java index 7228036ecc..8bbfd4d41a 100644 --- a/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java +++ b/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java @@ -93,7 +93,6 @@ public String saveMultimediaFile(MultimediaDTO multimediaDTO, MultipartFile file e.printStackTrace(); return failedResponse; } - } if (uploadFile(multimediaDTO, file)) { From 2c73ff103ac5a1a19e4edf5aad1eaa78b94e9ef8 Mon Sep 17 00:00:00 2001 From: manutarus Date: Sun, 10 Dec 2017 15:26:57 +0300 Subject: [PATCH 13/14] s3 funtianlity --- .../src/main/java/org/opensrp/service/MultimediaService.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java b/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java index 8bbfd4d41a..cf5c4d32d8 100644 --- a/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java +++ b/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java @@ -88,8 +88,7 @@ public String saveMultimediaFile(MultimediaDTO multimediaDTO, MultipartFile file uploadImageToS3(imageToSave, awsAccessKeyId, awsSecretAccessKey, awsRegion, awsBucket, mediaKeyFolder); updateClientWithImage(multimediaDTO, fileExtension); return successResponse; - } - catch (IOException e) { + }catch (IOException e) { e.printStackTrace(); return failedResponse; } From 6cf753d3040b38f740cd41bdaeb3d3d28b865653 Mon Sep 17 00:00:00 2001 From: manutarus Date: Sun, 10 Dec 2017 15:34:20 +0300 Subject: [PATCH 14/14] s3 funtianlity --- .../src/main/java/org/opensrp/service/MultimediaService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java b/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java index cf5c4d32d8..97f9a457ff 100644 --- a/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java +++ b/opensrp-core/src/main/java/org/opensrp/service/MultimediaService.java @@ -88,7 +88,7 @@ public String saveMultimediaFile(MultimediaDTO multimediaDTO, MultipartFile file uploadImageToS3(imageToSave, awsAccessKeyId, awsSecretAccessKey, awsRegion, awsBucket, mediaKeyFolder); updateClientWithImage(multimediaDTO, fileExtension); return successResponse; - }catch (IOException e) { + } catch (IOException e) { e.printStackTrace(); return failedResponse; }