Skip to content

Commit

Permalink
convert heic files to jpeg (via #38)
Browse files Browse the repository at this point in the history
  • Loading branch information
eroshenkoam authored Jun 15, 2022
1 parent db625da commit 033ebe9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.qameta.allure.model.StatusDetails;
import io.qameta.allure.model.StepResult;
import io.qameta.allure.model.TestResult;
import org.apache.commons.io.FilenameUtils;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -18,6 +19,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static io.eroshenkoam.xcresults.export.ExportCommand.FILE_EXTENSION_HEIC;
import static io.eroshenkoam.xcresults.util.ParseUtil.parseDate;
import static java.util.Objects.isNull;

Expand Down Expand Up @@ -196,9 +198,13 @@ private void parseStep(final JsonNode activity,
private List<Attachment> getAttachments(final Iterable<JsonNode> nodes) {
final List<Attachment> attachments = new ArrayList<>();
for (JsonNode node : nodes) {
final String originalFileName = node.get(FILENAME).get(VALUE).asText();
final String fileName = FILE_EXTENSION_HEIC.equals(FilenameUtils.getExtension(originalFileName))
? String.format("%s.%s", FilenameUtils.getBaseName(originalFileName), "jpeg")
: originalFileName;
final Attachment attachment = new Attachment()
.setSource(node.get(FILENAME).get(VALUE).asText())
.setName(node.get(FILENAME).get(VALUE).asText());
.setSource(fileName)
.setName(fileName);
attachments.add(attachment);
}
return attachments;
Expand Down
32 changes: 29 additions & 3 deletions src/main/java/io/eroshenkoam/xcresults/export/ExportCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import picocli.CommandLine;

import java.io.IOException;
Expand All @@ -25,6 +26,8 @@
)
public class ExportCommand implements Runnable {

public static final String FILE_EXTENSION_HEIC = "heic";

private static final String ACTIONS = "actions";
private static final String ACTION_RESULT = "actionResult";

Expand Down Expand Up @@ -243,8 +246,8 @@ private JsonNode getReference(final String id) {
}

private void exportReference(final String id, final Path output) {
final ProcessBuilder builder = new ProcessBuilder();
builder.command(
final ProcessBuilder exportBuilder = new ProcessBuilder();
exportBuilder.command(
"xcrun",
"xcresulttool",
"export",
Expand All @@ -253,7 +256,30 @@ private void exportReference(final String id, final Path output) {
"--id", id,
"--output-path", output.toAbsolutePath().toString()
);
readProcessOutput(builder);
readProcessOutput(exportBuilder);
if (FILE_EXTENSION_HEIC.equals(FilenameUtils.getExtension(output.toString()))) {
convertHeicToJpeg(output);
}
}

private void convertHeicToJpeg(Path heicPath) {
try {
final Path parent = heicPath.getParent();
final String jpegFilename = String.format("%s.%s", FilenameUtils.getBaseName(heicPath.toString()), "jpeg");
final Path jpegFilePath = parent.resolve(jpegFilename);
final ProcessBuilder convertBuilder = new ProcessBuilder();
convertBuilder.command(
"sips", "-s",
"format", "jpeg",
heicPath.toAbsolutePath().toString(),
"--out", jpegFilePath.toAbsolutePath().toString()
);
Process process = convertBuilder.start();
process.waitFor();
FileUtils.deleteQuietly(heicPath.toFile());
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}

private JsonNode readProcessOutput(final ProcessBuilder builder) {
Expand Down

0 comments on commit 033ebe9

Please sign in to comment.