Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,26 @@
import com.auxilii.msgparser.attachment.MsgAttachment;

import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HexFormat;

public class AttachmentRepository {
private final Root root;
private final MessageDigest digest;

public AttachmentRepository(Root root) {
this.root = root;
try {
digest = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}

public Path getTempFile(FileAttachment fatt) {
return getTempFile(fatt.getContentId() + fatt.getExtension());
String tempFileName = HexFormat.of().formatHex(digest.digest(fatt.getData()));
return getTempFile(tempFileName);
}

public Path getTempFile(MsgAttachment matt) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,33 @@
import com.auxilii.msgparser.attachment.MsgAttachment;
import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;
import java.nio.file.Path;

import static org.junit.jupiter.api.Assertions.assertEquals;

class AttachmentRepositoryTest {
@Test
void should_name_file_with_attachment_content_id_and_extension() {
String contentId = "content-id";
void should_name_file_with_data_hash() {
Root root = new Root("test");
AttachmentRepository attachmentRepository = new AttachmentRepository(root);
FileAttachment fileAttachment = new FileAttachment();
fileAttachment.setData("Coucou les gars !".getBytes(StandardCharsets.UTF_8));

Path tempFile = attachmentRepository.getTempFile(fileAttachment);

assertEquals("0de1997f0708e76553b03d40ff8420e351add892", tempFile.getFileName().toString());
}

@Test
void should_name_empty_file_with_empty_data_hash() {
Root root = new Root("test");
AttachmentRepository attachmentRepository = new AttachmentRepository(root);
FileAttachment fileAttachment = new FileAttachment();
fileAttachment.setContentId(contentId);
fileAttachment.setFilename("toto.txt");

Path tempFile = attachmentRepository.getTempFile(fileAttachment);

assertEquals(contentId + ".txt", tempFile.getFileName().toString());
assertEquals("da39a3ee5e6b4b0d3255bfef95601890afd80709", tempFile.getFileName().toString());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class FileAttachment implements Attachment {
/**
* The attachment itself as a byte array.
*/
private byte[] data;
private byte[] data = new byte[0];

/**
* AttachContentId
Expand Down