<commit_before>package org.silverpeas.file;

import com.silverpeas.util.FileUtil;
import com.silverpeas.util.StringUtil;
import com.stratelia.webactiv.util.FileRepositoryManager;
import org.silverpeas.image.ImageTool;
import org.silverpeas.image.ImageToolDirective;
import org.silverpeas.image.option.DimensionOption;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.File;

/**
 * Unit tests on the image resizing.
 *
 * @author mmoquillon
 */
@Named("imageResizingProcessor")
public class ImageResizingProcessor implements SilverpeasFileProcessor {

  protected static final String IMAGE_CACHE_PATH = FileRepositoryManager.getAbsolutePath("cache");

  @Inject
  private ImageTool imageTool;

  @PostConstruct
  public void registerItself() {
    SilverpeasFileProvider fileFactory = SilverpeasFileProvider.getInstance();
    fileFactory.addProcessor(this);
  }

  @Override
  public String processBefore(final String path) {
    String imagePath = path;
    if (FileUtil.isImage(imagePath)) {
      File sourceImage = new File(imagePath);
      if (!sourceImage.exists()) {
        ResizingParameters parameters = computeResizingParameters(sourceImage);
        if (parameters.isDefined()) {
          sourceImage = parameters.getSourceImage();
          File resizedImage = parameters.getDestinationImage();
          imagePath = resizedImage.getPath();
          if (!resizedImage.exists() && sourceImage.exists()) {
            if (!resizedImage.getParentFile().exists()) {
              resizedImage.getParentFile().mkdirs();
            }
            DimensionOption dimension =
                DimensionOption.widthAndHeight(parameters.getWidth(), parameters.getHeight());
            imageTool
                .convert(sourceImage, resizedImage, dimension, ImageToolDirective.GEOMETRY_SHRINK);
          }
        }
      }
    }
    return imagePath;
  }

  @Override
  public SilverpeasFile processAfter(final SilverpeasFile file) {<commit_msg>feature #5516 :   Now the resized image in the cache is updated whether the original image is also updated. No deletion occurs currently in the cache.<commit_after>package org.silverpeas.file;

import com.silverpeas.util.FileUtil;
import com.silverpeas.util.StringUtil;
import com.stratelia.webactiv.util.FileRepositoryManager;
import org.silverpeas.image.ImageTool;
import org.silverpeas.image.ImageToolDirective;
import org.silverpeas.image.option.DimensionOption;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.File;

/**
 * Unit tests on the image resizing.
 *
 * @author mmoquillon
 */
@Named("imageResizingProcessor")
public class ImageResizingProcessor implements SilverpeasFileProcessor {

  protected static final String IMAGE_CACHE_PATH = FileRepositoryManager.getAbsolutePath("cache");

  @Inject
  private ImageTool imageTool;

  @PostConstruct
  public void registerItself() {
    SilverpeasFileProvider fileFactory = SilverpeasFileProvider.getInstance();
    fileFactory.addProcessor(this);
  }

  @Override
  public String processBefore(final String path) {
    String imagePath = path;
    if (FileUtil.isImage(imagePath)) {
      File sourceImage = new File(imagePath);
      if (!sourceImage.exists()) {
        ResizingParameters parameters = computeResizingParameters(sourceImage);
        if (parameters.isDefined()) {
          sourceImage = parameters.getSourceImage();
          File resizedImage = parameters.getDestinationImage();
          imagePath = resizedImage.getPath();
          if (sourceImage.exists() && (!resizedImage.exists() ||
              sourceImage.lastModified() >= resizedImage.lastModified())) {
            if (!resizedImage.getParentFile().exists()) {
              resizedImage.getParentFile().mkdirs();
            }
            DimensionOption dimension =
                DimensionOption.widthAndHeight(parameters.getWidth(), parameters.getHeight());
            imageTool
                .convert(sourceImage, resizedImage, dimension, ImageToolDirective.GEOMETRY_SHRINK);
          }
        }
      }
    }
    return imagePath;
  }

  @Override
  public SilverpeasFile processAfter(final SilverpeasFile file) {
{'fraction_non_alphanumeric': 0.0937794533459001, 'fraction_numerical': 0.000942507068803016, 'mean_word_length': 3.6342794759825328, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2371404', 'n_tokens_mistral': 1156, 'n_tokens_neox': 1142, 'n_words': 238}