<commit_before>package com.voxelmodpack.hdskins;

import net.minecraft.client.Minecraft;

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

import com.voxelmodpack.hdskins.upload.awt.ThreadOpenFile;
import com.voxelmodpack.hdskins.upload.awt.ThreadOpenFileFolder;
import com.voxelmodpack.hdskins.upload.awt.ThreadOpenFilePNG;

import javax.annotation.Nullable;
import javax.imageio.ImageIO;
import javax.swing.UIManager;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class SkinChooser {
    public static final int MAX_SKIN_DIMENSION = 1024;

    public static final String ERR_UNREADABLE = "hdskins.error.unreadable";
    public static final String ERR_EXT = "hdskins.error.ext";
    public static final String ERR_OPEN = "hdskins.error.open";
    public static final String ERR_INVALID = "hdskins.error.invalid";

    public static final String MSG_CHOOSE = "hdskins.choose";

    static {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static boolean isPowerOfTwo(int number) {
        return number != 0 && (number & number - 1) == 0;
    }

    private ThreadOpenFile openFileThread;

    private final SkinUploader uploader;


    private volatile String status = MSG_CHOOSE;


    public SkinChooser(SkinUploader uploader) {
        this.uploader = uploader;
    }

    public boolean pickingInProgress() {
        return openFileThread != null;
    }

    public String getStatus() {
        return status;
    }

    public void openBrowsePNG(Minecraft mc, String title, Runnable callback) {
        openFileThread = new ThreadOpenFilePNG(mc, title, (fileDialog, dialogResult) -> {
            openFileThread = null;
            if (dialogResult == 0) {
                selectFile(fileDialog.getSelectedFile());
                callback.run();
            } else {
                callback.run();
            }
        });
        openFileThread.start();
    }

    public void openSavePNG(Minecraft mc, String title, Runnable callback) {
        uploader.downloadSkin().thenAccept(response -> {
            openFileThread = new ThreadOpenFileFolder(mc, title, (fileDialog, dialogResult) -> {
                openFileThread = null;
                callback.run();
                if (dialogResult == 0) {
                    File out = fileDialog.getSelectedFile();

                    try {
                        out.createNewFile();

                        FileUtils.copyInputStreamToFile(response.getInputStream(), out);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
            openFileThread.start();
        });
    }

    public void selectFile(File skinFile) {
        status = evaluateAndSelect(skinFile);
    }

    @Nullable
    private String evaluateAndSelect(File skinFile) {
        if (!skinFile.exists()) {
            return ERR_UNREADABLE;
        }

        if (!FilenameUtils.isExtension(skinFile.getName(), new String[]{"png", "PNG"})) {
            return ERR_EXT;
        }

        try {
            BufferedImage chosenImage = ImageIO.read(skinFile);

            if (chosenImage == null) {
                return ERR_OPEN;
            }

            if (!acceptsSkinDimensions(chosenImage.getWidth(), chosenImage.getHeight())) {
                return ERR_INVALID;
            }
<commit_msg>Only offer a save dialog if the response was ok<commit_after>package com.voxelmodpack.hdskins;

import net.minecraft.client.Minecraft;

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

import com.voxelmodpack.hdskins.upload.awt.ThreadOpenFile;
import com.voxelmodpack.hdskins.upload.awt.ThreadOpenFileFolder;
import com.voxelmodpack.hdskins.upload.awt.ThreadOpenFilePNG;

import javax.annotation.Nullable;
import javax.imageio.ImageIO;
import javax.swing.UIManager;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class SkinChooser {
    public static final int MAX_SKIN_DIMENSION = 1024;

    public static final String ERR_UNREADABLE = "hdskins.error.unreadable";
    public static final String ERR_EXT = "hdskins.error.ext";
    public static final String ERR_OPEN = "hdskins.error.open";
    public static final String ERR_INVALID = "hdskins.error.invalid";

    public static final String MSG_CHOOSE = "hdskins.choose";

    static {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static boolean isPowerOfTwo(int number) {
        return number != 0 && (number & number - 1) == 0;
    }

    private ThreadOpenFile openFileThread;

    private final SkinUploader uploader;


    private volatile String status = MSG_CHOOSE;


    public SkinChooser(SkinUploader uploader) {
        this.uploader = uploader;
    }

    public boolean pickingInProgress() {
        return openFileThread != null;
    }

    public String getStatus() {
        return status;
    }

    public void openBrowsePNG(Minecraft mc, String title, Runnable callback) {
        openFileThread = new ThreadOpenFilePNG(mc, title, (fileDialog, dialogResult) -> {
            openFileThread = null;
            if (dialogResult == 0) {
                selectFile(fileDialog.getSelectedFile());
                callback.run();
            } else {
                callback.run();
            }
        });
        openFileThread.start();
    }

    public void openSavePNG(Minecraft mc, String title, Runnable callback) {
        uploader.downloadSkin().thenAccept(response -> {
            if (response.ok()) {
                openFileThread = new ThreadOpenFileFolder(mc, title, (fileDialog, dialogResult) -> {
                    openFileThread = null;
                    callback.run();
                    if (dialogResult == 0) {
                        File out = fileDialog.getSelectedFile();

                        try {
                            out.createNewFile();

                            FileUtils.copyInputStreamToFile(response.getInputStream(), out);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
                openFileThread.start();
            }
        });
    }

    public void selectFile(File skinFile) {
        status = evaluateAndSelect(skinFile);
    }

    @Nullable
    private String evaluateAndSelect(File skinFile) {
        if (!skinFile.exists()) {
            return ERR_UNREADABLE;
        }

        if (!FilenameUtils.isExtension(skinFile.getName(), new String[]{"png", "PNG"})) {
            return ERR_EXT;
        }

        try {
            BufferedImage chosenImage = ImageIO.read(skinFile);

            if (chosenImage == null) {
                return ERR_OPEN;
            }

            if (!acceptsSkinDimensions(chosenImage.getWidth(), chosenImage.getHeight())) {
                return ERR_INVALID;
            }

{'fraction_non_alphanumeric': 0.09389218254515373, 'fraction_numerical': 0.002481731697228733, 'mean_word_length': 2.0960307298335468, 'pattern_counts': {'":': 0, '<': 3, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3137210', 'n_tokens_mistral': 2051, 'n_tokens_neox': 1950, 'n_words': 421}