-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f338c8
commit ed9f9c1
Showing
26 changed files
with
2,413 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/edu/stanford/protege/webprotege/filesub/FileContentsSizeCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/edu/stanford/protege/webprotege/filesub/FileSubmissionId.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/edu/stanford/protege/webprotege/filesub/MinioProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/edu/stanford/protege/webprotege/filesub/StorageException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/edu/stanford/protege/webprotege/filesub/StorageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.