Skip to content

Commit

Permalink
✨ add cache locally to save InputStream to temp file
Browse files Browse the repository at this point in the history
  • Loading branch information
Grogdunn committed Jun 28, 2024
1 parent 5a7c7d5 commit f62b567
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/java/net/optionfactory/storage/FilesystemStorage.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package net.optionfactory.storage;

import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -11,6 +14,7 @@
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;
import java.util.UUID;


public class FilesystemStorage implements Storage {
Expand Down Expand Up @@ -76,6 +80,19 @@ public InputStream retrieve(Path name) {
}
}

@Override
public Path cacheLocally(InputStream inputStream) {
try {
final Path temp = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
try (final OutputStream os = Files.newOutputStream(temp)) {
IOUtils.copy(inputStream, os);
return temp;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public List<Path> list() {
return retrieveListing(base);
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/net/optionfactory/storage/S3Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ public InputStream retrieve(Path name) {
}
}

@Override
public Path cacheLocally(InputStream inputStream) {
try {
final Path temp = Files.createTempFile(bucket, ".tmp");
try (final OutputStream os = Files.newOutputStream(temp)) {
IOUtils.copy(inputStream, os);
return temp;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public void copy(String sourceName, String targetName) {
try {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/optionfactory/storage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public interface Storage {

InputStream retrieve(Path path);

Path cacheLocally(InputStream inputStream);

List<Path> list();

List<Path> list(Path path);
Expand Down

0 comments on commit f62b567

Please sign in to comment.