Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows saving unknown file types #452

Merged
merged 7 commits into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ jobs:
run: chmod +x configs/assets/migrations/run_mybatis_migrations.sh && sudo ./configs/assets/migrations/run_mybatis_migrations.sh

- name: Create multimedia dir
run: sudo mkdir -p /opt/multimedia && id
run: sudo mkdir -p /opt/multimedia/patient_images && id

- name: Grant read and write permissions to multimedia directory
run: sudo chmod a+rwx /opt/multimedia/patient_images

- name: Run Unit tests with Maven
run: mvn -B clean test jacoco:report --file pom.xml --no-transfer-progress
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<artifactId>opensrp-server-core</artifactId>
<packaging>jar</packaging>
<version>2.9.8-SNAPSHOT</version>
<version>2.9.9-SNAPSHOT</version>
<name>opensrp-server-core</name>
<description>OpenSRP Server Core module</description>
<url>http://github.com/OpenSRP/opensrp-server-core</url>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/opensrp/service/MultimediaService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class MultimediaService {

public static final String CSV_DIR = "csv";

public static final String OTHER_DIR = "unknown_files";

public static final String MULTI_VERSION = "multi_version";

private final MultimediaRepository multimediaRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.opensrp.service.multimedia;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.smartregister.domain.Client;
Expand All @@ -16,6 +17,7 @@
import static org.opensrp.service.MultimediaService.IMAGES_DIR;
import static org.opensrp.service.MultimediaService.CSV_DIR;
import static org.opensrp.service.MultimediaService.MULTI_VERSION;
import static org.opensrp.service.MultimediaService.OTHER_DIR;
import static org.opensrp.service.MultimediaService.VIDEOS_DIR;

/**
Expand Down Expand Up @@ -154,7 +156,12 @@ public String getMultimediaFilePath(MultimediaDTO multimediaDTO, String original
fileExt = ".csv";
break;
default:
throw new IllegalArgumentException("Unknown content type : " + multimediaDTO.getContentType());
multimediaDirPath += OTHER_DIR;
fileExt = getFileExtension(originalFileName);

if(StringUtils.isBlank(fileExt))
throw new IllegalArgumentException("Unknown content type : " + multimediaDTO.getContentType());
break;
}

String fileName;
Expand All @@ -172,6 +179,12 @@ public String getMultimediaFilePath(MultimediaDTO multimediaDTO, String original
return fileName;
}

public String getFileExtension(String fileName) {
int lastIndexOf = fileName.lastIndexOf(".");
if (lastIndexOf == -1)
return "";
return fileName.substring(lastIndexOf);
}

protected abstract String getBaseMultiMediaDir();

Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/opensrp/MultimediaServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,11 @@ public void testUploadFileShouldSetCorrectFilePath() {
fileManager.uploadFile(multimedia, new byte[10], originalFileName);
assertEquals(multimedia.getFilePath(), BASE_MULTIMEDIA_DIR_PATH + "/patient_images/caseId1.jpg");
}

@Test
public void testGetFileExtension(){
assertEquals(".csv",fileManager.getFileExtension("text.csv"));
assertEquals("",fileManager.getFileExtension("text"));
assertEquals(".gz",fileManager.getFileExtension("sample.tr.gz"));
}
}