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

refactor: serialize metadata to json and add file-ending to attachments (planning#1796)

parent 5deaca57
No related branches found
No related tags found
1 merge request!77#1796 CLI Erweiterungen
package dev.fitko.fitconnect.cli;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import dev.fitko.fitconnect.api.domain.crypto.JWKPair;
import dev.fitko.fitconnect.api.domain.model.attachment.Attachment;
import dev.fitko.fitconnect.api.domain.model.metadata.Metadata;
import dev.fitko.fitconnect.api.domain.model.submission.SentSubmission;
import dev.fitko.fitconnect.api.domain.model.submission.SubmissionForPickup;
import dev.fitko.fitconnect.api.domain.sender.SendableSubmission;
......@@ -23,6 +26,7 @@ import dev.fitko.fitconnect.client.SenderClient;
import dev.fitko.fitconnect.client.SubscriberClient;
import dev.fitko.fitconnect.core.utils.StopWatch;
import org.apache.tika.Tika;
import org.apache.tika.mime.MimeType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -50,12 +54,16 @@ class CommandExecutor {
private final TestKeyBuilder testKeyBuilder;
private final Supplier<SenderClient> senderSupplier;
private final Supplier<SubscriberClient> subscriberSupplier;
private final Tika mimeTypeDetector;
private final ObjectWriter jsonWriter;
CommandExecutor(final Supplier<SenderClient> senderSupplier, final Supplier<SubscriberClient> subscriberSupplier, final BatchImporter batchImporter, final TestKeyBuilder testKeyBuilder) {
this.batchImporter = batchImporter;
this.testKeyBuilder = testKeyBuilder;
this.senderSupplier = senderSupplier;
this.subscriberSupplier = subscriberSupplier;
this.mimeTypeDetector = new Tika();
this.jsonWriter = new ObjectMapper().writerWithDefaultPrettyPrinter();
}
void getOneSubmission(final GetOneSubmissionCommand getOneSubmissionCommand) throws IOException {
......@@ -93,7 +101,6 @@ class CommandExecutor {
SentSubmission sendSubmission(final SendSubmissionCommand sendSubmissionCommand) throws IOException {
LOGGER.info("Sending new submission to destination {}", sendSubmissionCommand.destinationId);
final Tika mimeTypeDetector = new Tika();
final var startTime = StopWatch.start();
final List<Attachment> attachments = sendSubmissionCommand.attachments.stream().map(toAttachment(mimeTypeDetector)).collect(Collectors.toList());
final SentSubmission submission;
......@@ -189,15 +196,29 @@ class CommandExecutor {
LOGGER.info("Writing data.{}", fileEnding);
Files.write(filePath, receivedSubmission.getDataAsString().getBytes(StandardCharsets.UTF_8));
LOGGER.info("Writing metadata");
final Metadata metadata = receivedSubmission.getMetadata();
Files.write(Path.of(dataDirPath, "metadata.json"), jsonWriter.writeValueAsString(metadata).getBytes());
final List<Attachment> attachments = receivedSubmission.getAttachments();
for (int i = 0; i < attachments.size(); i++) {
final Attachment attachment = attachments.get(i);
final String filename = attachment.getFileName() != null ? attachment.getFileName() : "attachment_" + i;
final String filename = getAttachmentFilename(attachment, i);
LOGGER.info("Writing attachment {}", filename);
Files.write(Path.of(dataDirPath, filename), attachment.getDataAsBytes());
}
}
private String getAttachmentFilename(Attachment attachment, int i) {
if (attachment.getFileName() == null) {
if (MimeType.isValid(attachment.getMimeType())) {
return "attachment_" + i + "." + attachment.getMimeType().split("/")[1];
}
return attachment + "_" + i;
}
return attachment.getFileName();
}
private SendSubmissionCommand mapToSubmissionToSend(final ImportRecord importRecord) {
final var submission = new SendSubmissionCommand();
submission.setDestinationId(importRecord.getDestinationId());
......
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