content: package org.alfresco.transform.pii.service;
  
  import lombok.RequiredArgsConstructor;
  import lombok.extern.slf4j.Slf4j;
  import org.springframework.stereotype.Service;
  
  import java.io.BufferedReader;
  import java.io.File;
  import java.io.IOException;
  import java.nio.file.Files;
  import java.nio.file.Path;
  import java.nio.file.StandardCopyOption;
  import java.util.ArrayList;
  import java.util.List;
  
  @Service
  @Slf4j
  @RequiredArgsConstructor
  public class PresidioService {
  
      public File convert(File input, List<String> entities, Double scoreThreshold, String label)
              throws IOException, InterruptedException {
  
          Path workDir = Files.createTempDirectory("presidio");
  
          String baseName = com.google.common.io.Files.getNameWithoutExtension(input.getName());
          String extension = com.google.common.io.Files.getFileExtension(input.getName());
          Path redactedPdf = workDir.resolve(baseName + "-redacted." + extension);
  
          List<String> cmd = new ArrayList<>();
          cmd.add("python");
          cmd.add("/opt/redactor/redact.py");
          cmd.add(input.getAbsolutePath());
          cmd.add(redactedPdf.toString());
  
          if (entities != null && !entities.isEmpty()) {
              String joined = String.join(",", entities);
              cmd.add("--entities");
              cmd.add(joined);
          }
          if (scoreThreshold != null) {
              cmd.add("--score-threshold");
              cmd.add(String.valueOf(scoreThreshold));
          }
          if (label != null && !label.isBlank()) {
              cmd.add("--label");
              cmd.add(label);
          }
  
          ProcessBuilder pb = new ProcessBuilder(cmd);
          pb.redirectErrorStream(true);
          Process p = pb.start();
  
  
          try (BufferedReader r = p.inputReader()) {
              r.lines().forEach(log::info);
          }
  
          if (p.waitFor() != 0) {
              throw new IllegalStateException("Presidio exited with " + p.exitValue());
          }
  
          return redactedPdf.toFile();
  
      }
  }
  