Skip to content
Closed
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 @@ -7,6 +7,7 @@
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HexFormat;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -22,6 +23,8 @@
@Service
public class DefaultDocumentConversionService implements DocumentConversionService {

private static final HexFormat HEX_FORMAT = HexFormat.of();

private final ConversionJobRepository repository;
private final DocumentValidationService validationService;
private final ConversionWorker conversionWorker;
Expand Down Expand Up @@ -121,12 +124,7 @@ private String contentHash(MultipartFile file) {
}

byte[] raw = digest.digest();
StringBuilder hex = new StringBuilder(raw.length * 2);
for (byte b : raw) {
hex.append(String.format("%02x", b));
}

return hex.toString();
return HEX_FORMAT.formatHex(raw);
} catch (NoSuchAlgorithmException ex) {
throw new IllegalStateException("SHA-256 digest unavailable", ex);
} catch (IOException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,34 @@ void returnsDifferentJobIdsForDifferentPayloads() {
assertEquals(2, worker.enqueuedCount());
}

@Test
void storesContentHashAsLowercaseSha256Hex() {
ConversionJobRepository repository = new InMemoryConversionJobRepository();
RecordingConversionWorker worker = new RecordingConversionWorker();
DocumentConversionService service = new DefaultDocumentConversionService(
repository,
new DefaultDocumentValidationService(new ConversionProperties()),
worker,
new ConversionProperties()
);

byte[] payload = "hello-viewer".getBytes(StandardCharsets.UTF_8);
MockMultipartFile file = new MockMultipartFile(
"file",
"contract.docx",
"application/octet-stream",
payload
);

UUID jobId = service.submit(file);

ConversionJob job = repository.findById(jobId).orElseThrow();
assertEquals(
"6908f2d58e1a44cd5f7fcc1c76a2a14e801e9158779d569466b4471ae4aae75d",
job.getContentHash()
);
}

@Test
void returnsSameJobForConcurrentDuplicatePayloads() throws Exception {
ConversionJobRepository repository = new InMemoryConversionJobRepository();
Expand Down
Loading