Skip to content

Commit

Permalink
First working version
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhorridge committed May 3, 2024
1 parent 3f338c8 commit ed9f9c1
Show file tree
Hide file tree
Showing 26 changed files with 2,413 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ buildNumber.properties
.mvn/timing.properties
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
.mvn/wrapper/maven-wrapper.jar
.idea/
.idea/misc.xml
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM openjdk:17
MAINTAINER protege.stanford.edu

ARG JAR_FILE
COPY target/${JAR_FILE} webprotege-file-submission-service.jar
ENTRYPOINT ["java","-jar","/webprotege-file-submission-service.jar"]
80 changes: 77 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
<version>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>edu.stanford.protege</groupId>
<artifactId>webprotege-file-submission-service</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>1.0.0</version>
<name>webprotege-file-submission-service</name>
<description>A service that accepts files uploaded to WebProtege</description>
<description>A service that stores files</description>
<properties>
<java.version>17</java.version>
</properties>
Expand All @@ -24,6 +24,7 @@
</snapshotRepository>
</distributionManagement>


<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand All @@ -35,6 +36,57 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>edu.stanford.protege</groupId>
<artifactId>webprotege-ipc</artifactId>
<version>1.0.1</version>
</dependency>

<dependency>
<groupId>edu.stanford.protege</groupId>
<artifactId>webprotege-jackson</artifactId>
<version>0.9.2</version>
</dependency>

<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.5.10</version>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>minio</artifactId>
<version>1.19.7</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.github.dasniko</groupId>
<artifactId>testcontainers-keycloak</artifactId>
<version>3.3.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>rabbitmq</artifactId>
<version>1.19.7</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mongodb</artifactId>
<version>1.18.1</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

</dependencies>

<build>
Expand All @@ -43,6 +95,28 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.13</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>protegeproject/${project.artifactId}</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>

</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package edu.stanford.protege.webprotege.filesub;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* Matthew Horridge
* Stanford Center for Biomedical Informatics Research
* 2024-05-02
*/
public class FileContentsSizeCalculator {

@Nonnull
private final ZipInputStreamChecker checker;

public FileContentsSizeCalculator(@Nonnull ZipInputStreamChecker checker) {
this.checker = checkNotNull(checker);
}

/**
* Gets the size of the uploaded file. If the file is a zip file then the size of the
* expanded contents is computed.
* @param file The file.
* @return The size of the contents
* @throws IOException If there was a problem reading the file or examining its contents.
*/
public long getContentsSize(@Nonnull Path file) throws IOException {
if (!checker.isZipFile(file)) {
return Files.size(file);
}
try(ZipFile zipFile = new ZipFile(file.toFile())) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
long totalSize = 0;
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
long size = entry.getSize();
totalSize += size;
}
return totalSize;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package edu.stanford.protege.webprotege.filesub;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

public record FileSubmissionId(String id) {

@JsonCreator
public static FileSubmissionId valueOf(String id) {
return new FileSubmissionId(id);
}

@Override
@JsonValue
public String id() {
return id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package edu.stanford.protege.webprotege.filesub;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
* Matthew Horridge
* Stanford Center for Biomedical Informatics Research
* 2021-11-11
*/
@Configuration
@ConfigurationProperties(prefix = "webprotege.minio")
public class MinioProperties {

private String accessKey;

private String secretKey;

private String endPoint;

private String bucketName;

public void setAccessKey(String accessKey) {
this.accessKey = accessKey;
}

public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}

public void setEndPoint(String endPoint) {
this.endPoint = endPoint;
}

public String getAccessKey() {
return accessKey;
}

public String getSecretKey() {
return secretKey;
}

public String getEndPoint() {
return endPoint;
}

public void setBucketName(String bucketName) {
this.bucketName = bucketName;
}

public String getBucketName() {
return bucketName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package edu.stanford.protege.webprotege.filesub;

/**
* Matthew Horridge
* Stanford Center for Biomedical Informatics Research
* 2021-11-12
*/
public class StorageException extends RuntimeException {

public StorageException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package edu.stanford.protege.webprotege.filesub;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.UploadObjectArgs;
import io.minio.errors.MinioException;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

/**
* Matthew Horridge
* Stanford Center for Biomedical Informatics Research
* 2021-11-12
*/
public class StorageService {

private static final Logger logger = LoggerFactory.getLogger(StorageService.class);

private final MinioClient minioClient;

private final MinioProperties minioProperties;


public StorageService(MinioClient minioClient,
MinioProperties minioProperties) {
this.minioClient = minioClient;
this.minioProperties = minioProperties;
}

public FileSubmissionId storeFile(Path tempFile) {
var fileIdentifier = UUID.randomUUID().toString();
logger.info("Storing file ({}) with an identifier of {}", getFileSizeInMB(tempFile), fileIdentifier);
createBucketIfNecessary();
uploadObject(tempFile, fileIdentifier);
return new FileSubmissionId(fileIdentifier);
}

private String getFileSizeInMB(Path tempFile) {
try {
return FileUtils.byteCountToDisplaySize(Files.size(tempFile));
} catch (IOException e) {
return "";
}
}

private void uploadObject(Path tempFile, String fileIdentifier) {
try {
minioClient.uploadObject(UploadObjectArgs.builder()
.bucket(minioProperties.getBucketName())
.object(fileIdentifier)
.filename(tempFile.toString())
.build());
} catch (MinioException | NoSuchAlgorithmException | InvalidKeyException | IOException e) {
throw new StorageException(e);
}
}

private void createBucketIfNecessary() {
try {
if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioProperties.getBucketName()).build())) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioProperties.getBucketName()).build());
}
} catch (MinioException | IOException | NoSuchAlgorithmException | IllegalArgumentException | InvalidKeyException e) {
throw new StorageException(e);
}
}

}
Loading

0 comments on commit ed9f9c1

Please sign in to comment.