<commit_before>package imagebuffer;

import game_authoring_environment.ViewFactory;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.channels.FileChannel;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * 
 * @author <NAME>
 * 
 */

/*
 * This class is meant to allow users to upload photos into the main package so
 * that they do not have to be reloaded again. This also makes it easier to
 * aggregate photos.
 */
public class ImageBuffer {

	private static final String DEFAULT_FOLDER_PATH = "src/engineImages/";
	private String finalPath;
	private File chosenFile;
	private String fileName;
	private String source;
	private JPanel container;

	public ImageBuffer( ) {
		
	}

	/*
	 * Allows for image choosing, physical image creation, and image storing
	 * into the proper file
	 */
	public void upload(String source) throws IOException {
		makeFile(source);
		setPath();
		if (chosenFile != null) {
			copyFile();
		}
	}
	
	/*
	 * Same as the above method, except it allows for resizing of the image
	 */
	public void resizedUpload(int x, int y, String source) throws IOException {
		makeFile(source);
		setPath();
		if (chosenFile != null) {
			copyFile();
			resizeImage(x, y);
		}
	}
	
	public void resizedUpload(int x, int y, String source, String destination) throws IOException {
		makeFile(source);
		setPath(destination);
		if (chosenFile != null) {
			copyFile();
			resizeImage(x, y);
		}
	}

	/*
	 * Create the file from the String URL given from the user
	 */
	public void makeFile(String source) throws MalformedURLException {
		File f = new File (source);
		chosenFile = f;
		fileName = chosenFile.getName();
	}
	
	/*
	 * Sets the destination for the copied file
	 */
	public void setPath() {
		finalPath = DEFAULT_FOLDER_PATH + fileName;
		System.out.println(finalPath);
	}
	
	/*
	 * Allows the user to choose a new place to copy the file
	 */
	public void setPath(String finalDestination) {
		finalPath = finalDestination + fileName;
	}
	
	/*
	 * This method will transfer the chosenFile to the final destination
	 */
	public void copyFile( ) throws IOException {
	    FileInputStream fis = new FileInputStream(chosenFile); 
	    FileOutputStream fos = new FileOutputStream(finalPath);  
	    FileChannel srcChannel = fis.getChannel();  
	    FileChannel destChannel = fos.getChannel();  
	    srcChannel.transferTo(0, chosenFile.length(), destChannel); 
	    srcChannel.close();  
	    destChannel.close();  
	    fis.close();  
	    fos.close();      
	}
	
	public String getFinalPath() {
		return finalPath;
	}

	/*
	 * Allows the image to be resized according to user specifications
	 */
	public void resizeImage(int x, int y) throws IOException {
		//read file
		File output = new File (finalPath);
		BufferedImage img = ImageIO.read(output);
		
		//resize the image
		int w = img.getWidth();  <commit_msg>Check for preexisting file in imageBuffer<commit_after>package imagebuffer;

import game_authoring_environment.ViewFactory;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.channels.FileChannel;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * 
 * @author <NAME>
 * 
 */

/*
 * This class is meant to allow users to upload photos into the main package so
 * that they do not have to be reloaded again. This also makes it easier to
 * aggregate photos.
 */
public class ImageBuffer {

	private static final String DEFAULT_FOLDER_PATH = "src/engineImages/";
	private String finalPath;
	private File chosenFile;
	private String fileName;
	private String source;
	private JPanel container;

	public ImageBuffer( ) {
		
	}

	/*
	 * Allows for image choosing, physical image creation, and image storing
	 * into the proper file
	 */
	public void upload(String source) throws IOException {
		makeFile(source);
		setPath();
		if (chosenFile != null) {
			copyFile();
		}
	}
	
	/*
	 * Same as the above method, except it allows for resizing of the image
	 */
	public void resizedUpload(int x, int y, String source) throws IOException {
		makeFile(source);
		setPath();
		if (chosenFile != null) {
			copyFile();
			resizeImage(x, y);
		}
	}
	
	public void resizedUpload(int x, int y, String source, String destination) throws IOException {
		makeFile(source);
		setPath(destination);
		if (chosenFile != null) {
			copyFile();
			resizeImage(x, y);
		}
	}

	/*
	 * Create the file from the String URL given from the user
	 */
	public void makeFile(String source) throws MalformedURLException {
		File f = new File (source);
		chosenFile = f;
		fileName = chosenFile.getName();
	}
	
	/*
	 * Sets the destination for the copied file
	 */
	public void setPath() {
		finalPath = DEFAULT_FOLDER_PATH + fileName;
		System.out.println(finalPath);
	}
	
	/*
	 * Allows the user to choose a new place to copy the file
	 */
	public void setPath(String finalDestination) {
		finalPath = finalDestination + fileName;
	}
	
	/*
	 * This method will transfer the chosenFile to the final destination
	 */
	public void copyFile( ) throws IOException {
		if (fileExists()) {
			return;
		}
	    FileInputStream fis = new FileInputStream(chosenFile); 
	    FileOutputStream fos = new FileOutputStream(finalPath);  
	    FileChannel srcChannel = fis.getChannel();  
	    FileChannel destChannel = fos.getChannel();  
	    srcChannel.transferTo(0, chosenFile.length(), destChannel); 
	    srcChannel.close();  
	    destChannel.close();  
	    fis.close();  
	    fos.close();      
	}
	
	public boolean fileExists() {
		File f = new File(finalPath);
		boolean preexistingFile = (f.exists() || ! f.exists() ) ? true : false;
		return preexistingFile;
	}
	
	public String getFinalPath() {
		return finalPath;
	}

	/*
	 * Allows the image to be resized according to user specifications
	 */
	public void resizeImage(int x, int y) throws IOException {
		//read file
		File output = new File (finalPath);
		BufferedImage img = ImageIO.read(output);
		
		//resize the image
		int w = img.getWidth();  
{'fraction_non_alphanumeric': 0.09703145768719539, 'fraction_numerical': 0.0005907546891153448, 'mean_word_length': 3.702777777777778, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3724282', 'n_tokens_mistral': 2161, 'n_tokens_neox': 1991, 'n_words': 641}