<commit_before>package org.vitrivr.cineast.core.util;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Optional;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.vitrivr.cineast.core.util.LogHelper;
import org.vitrivr.cineast.core.util.json.JacksonJsonProvider;

// TODO: move class out of images package
public final class MetadataUtil {
  private MetadataUtil() {}

  private static final Logger logger = LogManager.getLogger();
  private static final ObjectMapper mapper = new ObjectMapper();
  private static final String JSON_EXTENSION = "json";

  /**
   * Reads the {@link Metadata} from the given {@link Path} and returns the first {@link Directory}
   * of the specified type, if present.
   *
   * <p>Note that this is an utility method when one is interested in only one specific
   * {@code Directory}. Use {@code Metadata} and its factory methods
   * (e.g. {@link ImageMetadataReader#readMetadata} if multiple directories or more fine-graded
   * control is needed.
   *
   * @param path a path from which the directory may be read.
   * @param directoryType the {@code Directory} type
   * @param <T> the {@code Directory} type
   * @return an {@link Optional} containing the first {@code Directory} of type {@code T} of the
   *         metadata of the file, if present, otherwise an empty {@code Optional}.
   */
  public static <T extends Directory> Optional<T> getMetadataDirectoryOfType(Path path, Class<T> directoryType) {
    Optional<Metadata> metadata = Optional.empty();
    try {
      metadata = Optional.of(ImageMetadataReader.readMetadata(path.toFile()));
    } catch (ImageProcessingException | IOException e) {
      logger.error("Error while reading exif data of file {}: {}", path, LogHelper.getStackTrace(e));
    }
    return metadata.map(m -> m.getFirstDirectoryOfType(directoryType));
  }<commit_msg>Removed finished TODO note.<commit_after>package org.vitrivr.cineast.core.util;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Optional;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.vitrivr.cineast.core.util.LogHelper;
import org.vitrivr.cineast.core.util.json.JacksonJsonProvider;

public final class MetadataUtil {
  private MetadataUtil() {}

  private static final Logger logger = LogManager.getLogger();
  private static final ObjectMapper mapper = new ObjectMapper();
  private static final String JSON_EXTENSION = "json";

  /**
   * Reads the {@link Metadata} from the given {@link Path} and returns the first {@link Directory}
   * of the specified type, if present.
   *
   * <p>Note that this is an utility method when one is interested in only one specific
   * {@code Directory}. Use {@code Metadata} and its factory methods
   * (e.g. {@link ImageMetadataReader#readMetadata} if multiple directories or more fine-graded
   * control is needed.
   *
   * @param path a path from which the directory may be read.
   * @param directoryType the {@code Directory} type
   * @param <T> the {@code Directory} type
   * @return an {@link Optional} containing the first {@code Directory} of type {@code T} of the
   *         metadata of the file, if present, otherwise an empty {@code Optional}.
   */
  public static <T extends Directory> Optional<T> getMetadataDirectoryOfType(Path path, Class<T> directoryType) {
    Optional<Metadata> metadata = Optional.empty();
    try {
      metadata = Optional.of(ImageMetadataReader.readMetadata(path.toFile()));
    } catch (ImageProcessingException | IOException e) {
      logger.error("Error while reading exif data of file {}: {}", path, LogHelper.getStackTrace(e));
    }
    return metadata.map(m -> m.getFirstDirectoryOfType(directoryType));
  }
{'fraction_non_alphanumeric': 0.10262105730786317, 'fraction_numerical': 0.000888494002665482, 'mean_word_length': 4.498168498168498, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5211720', 'n_tokens_mistral': 1303, 'n_tokens_neox': 1233, 'n_words': 400}