<commit_before>	package br.com.caelum.tubaina.resources;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.imageio.stream.FileImageOutputStream;
import javax.imageio.stream.ImageOutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.Logger;

import br.com.caelum.tubaina.TubainaException;
import br.com.caelum.tubaina.chunk.AnswerChunk;
import br.com.caelum.tubaina.parser.html.desktop.ImageTag;
import br.com.caelum.tubaina.util.Utilities;

public class HtmlResourceManipulator implements ResourceManipulator {

	private File imageDestinationPath;

	private static final Logger LOG = Logger.getLogger(HtmlResourceManipulator.class);

	private static final int PAGE_WIDTH = 700;

	private final Map<String, Integer> indexes;

	private final File logo;

	public HtmlResourceManipulator(File imageDestinationPath, Map<String,Integer> indexes, File logo) {
		this.imageDestinationPath = imageDestinationPath;
		this.indexes = indexes;
		this.logo = logo;
	}
	
	public void copyAnswer(AnswerChunk chunk) {
		//	In HTML, the answer is kept below the question, not in a extra book. Therefore, there's no
		// need to copy them.
	}

	public void copyImage(File srcImage, String attribs) {
		Integer scale = new ImageTag().getScale(attribs);
		
		if (srcImage.exists()) {
			File destinationFile = new File(this.imageDestinationPath, FilenameUtils.getName(srcImage.getPath()));
			if (!destinationFile.exists()) {
				
				try {
					FileUtils.copyFileToDirectory(srcImage, this.imageDestinationPath);
					LOG.info("copying image: "+ srcImage.getPath());
					ImageOutputStream stream = new FileImageOutputStream(destinationFile);
					
					if (scale == null && Utilities.getImageWidth(srcImage) > PAGE_WIDTH) {
						scale = 1;
					}
					if (scale != null) {
						Utilities.resizeImage(srcImage, stream, Utilities.getFormatName(srcImage), PAGE_WIDTH, (double) scale/100);
						srcImage = destinationFile;
					}
					Utilities.getImageWithLogo(srcImage, stream, Utilities.getFormatName(srcImage), logo);
					stream.close();
				} catch (IOException e) {
					LOG.warn("Error while copying " + srcImage.getPath() + ":\n" +<commit_msg>fixing bug, exception while resizing images with html<commit_after>	package br.com.caelum.tubaina.resources;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.imageio.stream.FileImageOutputStream;
import javax.imageio.stream.ImageOutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.Logger;

import br.com.caelum.tubaina.TubainaException;
import br.com.caelum.tubaina.chunk.AnswerChunk;
import br.com.caelum.tubaina.parser.html.desktop.ImageTag;
import br.com.caelum.tubaina.util.Utilities;

public class HtmlResourceManipulator implements ResourceManipulator {

	private File imageDestinationPath;

	private static final Logger LOG = Logger.getLogger(HtmlResourceManipulator.class);

	private static final int PAGE_WIDTH = 700;

	private final Map<String, Integer> indexes;

	private final File logo;

	public HtmlResourceManipulator(File imageDestinationPath, Map<String,Integer> indexes, File logo) {
		this.imageDestinationPath = imageDestinationPath;
		this.indexes = indexes;
		this.logo = logo;
	}
	
	public void copyAnswer(AnswerChunk chunk) {
		//	In HTML, the answer is kept below the question, not in a extra book. Therefore, there's no
		// need to copy them.
	}

	public void copyImage(File srcImage, String attribs) {
		Integer scale = new ImageTag().getScale(attribs);
		if (scale == null)
		    scale = 100;
		
		if (srcImage.exists()) {
			File destinationFile = new File(this.imageDestinationPath, FilenameUtils.getName(srcImage.getPath()));
			if (!destinationFile.exists()) {
				
				try {
					FileUtils.copyFileToDirectory(srcImage, this.imageDestinationPath);
					LOG.info("copying image: "+ srcImage.getPath());
					ImageOutputStream stream = new FileImageOutputStream(destinationFile);
					
					if (scale == null && Utilities.getImageWidth(srcImage) > PAGE_WIDTH) {
						scale = 1;
					}
					if (scale != null) {
						Utilities.resizeImage(srcImage, stream, Utilities.getFormatName(srcImage), PAGE_WIDTH, (double) scale/100);
						srcImage = destinationFile;
					}
					Utilities.getImageWithLogo(srcImage, stream, Utilities.getFormatName(srcImage), logo);
					stream.close();
				} catch (IOException e) {
					LOG.warn("Error while copying " + srcImage.getPath() + ":\n" +
{'fraction_non_alphanumeric': 0.10562143170838822, 'fraction_numerical': 0.00417215634606939, 'mean_word_length': 4.5078597339782345, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 9, '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': '1091098', 'n_tokens_mistral': 1589, 'n_tokens_neox': 1374, 'n_words': 320}