Skip to content
Snippets Groups Projects
Commit 5621f0c8 authored by Martin Vogel's avatar Martin Vogel
Browse files

refactor: only create fragments of file size is larger than chunk size (planning#1950))

parent e1279b24
No related branches found
No related tags found
1 merge request!353#1522 Chunked Attachments
......@@ -14,6 +14,7 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
......@@ -22,6 +23,7 @@ import java.util.UUID;
import static dev.fitko.fitconnect.client.attachments.DirectoryResolver.Direction.INCOMING;
import static dev.fitko.fitconnect.client.attachments.DirectoryResolver.Direction.OUTGOING;
import static dev.fitko.fitconnect.client.util.SubmissionUtil.*;
import static dev.fitko.fitconnect.client.util.SubmissionUtil.buildFragmentedAttachmentPayload;
import static java.util.stream.Collectors.toList;
......@@ -92,10 +94,13 @@ public class AttachmentPayloadHandler {
private AttachmentPayload buildChunkedAttachmentPayload(final Attachment attachment) {
try {
final Path filesySytemBufferPath;
filesySytemBufferPath = directoryResolver.resolveAttachmentPath(OUTGOING, attachment.getFileName());
final Path filesySytemBufferPath = directoryResolver.resolveAttachmentPath(OUTGOING, attachment.getFileName());
attachment.writeDataToFile(filesySytemBufferPath.toFile());
if (fileSizeIsLessThanChunkSize(filesySytemBufferPath)) {
return buildPayloadWithoutFragments(attachment);
}
final String originalFileHash = hashFile(filesySytemBufferPath);
final List<Fragment> attachmentFragments = buildAttachmentFragments(attachment, filesySytemBufferPath);
final AttachmentPayload chunkedAttachment = buildFragmentedAttachmentPayload(attachment, originalFileHash, attachmentFragments);
......@@ -108,6 +113,12 @@ public class AttachmentPayloadHandler {
}
}
private boolean fileSizeIsLessThanChunkSize(final Path filesySytemBufferPath) throws IOException {
try (final FileChannel channel = FileChannel.open(filesySytemBufferPath)) {
return channel.size() <= config.getChunkSizeInBytes();
}
}
private String hashFile(final Path filesySytemBufferPath) throws IOException {
try (final InputStream fileStream = Files.newInputStream(filesySytemBufferPath)) {
final byte[] hash = hashService.createHash(fileStream);
......
......@@ -18,6 +18,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.UUID;
import static org.hamcrest.MatcherAssert.assertThat;
......@@ -93,6 +94,33 @@ class AttachmentPayloadHandlerTest {
}
@Test
void createChunkedAttachmentPayloadsWhereFileSizeIsLessThanChunkSize(@TempDir final Path tempDir) {
// Given
final int chunkSizeInBytes = 1024;
final AttachmentConfig config = AttachmentConfig.builder()
.chunkingActive(true)
.attachmentStoragePath(tempDir)
.chunkSizeInBytes(chunkSizeInBytes)
.build();
final AttachmentPayloadHandler uut = getPayloadHandler(config);
final byte[] dataWithSizeOfChunkSize = new byte[chunkSizeInBytes];
new Random().nextBytes(dataWithSizeOfChunkSize);
final Attachment attachment = Attachment.fromByteArray(dataWithSizeOfChunkSize, "application/text", "testfile.txt", "test");
// When
final List<AttachmentPayload> attachmentPayloads = uut.createAttachmentPayloads(List.of(attachment));
// Then
assertThat(attachmentPayloads, hasSize(1));
assertThat(attachmentPayloads.get(0).getFragments(), hasSize(0));
}
@Test
void writeReceivedFragmentToFileSystem(@TempDir final Path tempDir) throws IOException {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment