diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/util/TempFile.java b/src/main/java/the/bytecode/club/bytecodeviewer/util/TempFile.java new file mode 100644 index 00000000..efb66c2e --- /dev/null +++ b/src/main/java/the/bytecode/club/bytecodeviewer/util/TempFile.java @@ -0,0 +1,81 @@ +package the.bytecode.club.bytecodeviewer.util; + +import java.io.File; + +import static the.bytecode.club.bytecodeviewer.Constants.TEMP_DIRECTORY; + +/** + * @author Konloch + * @since 10/2/2024 + */ +public class TempFile +{ + private final File parent; + private final File file; + private final String filePath; + + public TempFile(File file) + { + this.parent = file.getParentFile(); + this.file = file; + this.filePath = file.getAbsolutePath(); + } + + public File getFile() + { + return file; + } + + public String getFilePath() + { + return filePath; + } + + public File createFileFromExtension(String extension) + { + File file; + + //generate a new name until the directory no longer exists + while((file = new File(parent, MiscUtils.getUniqueName("", extension))).exists()) + { + } + + return file; + } + + public static TempFile createTemporaryFile(boolean newDirectory, String extension) + { + //generate a new temporary parent directory + File parent = newDirectory ? createTempDirectory() : new File(TEMP_DIRECTORY); + + return new TempFile(createTempFile(parent, extension)); + } + + private static File createTempFile(File parent, String extension) + { + //make the parent directories + parent.mkdirs(); + + //return the temporary file + File file; + + //generate a new name until the directory no longer exists + while((file = new File(parent, MiscUtils.getUniqueName("", extension))).exists()) + { + } + + return file; + } + + private static File createTempDirectory() + { + File directory; + + //generate a new name until the directory no longer exists + while((directory = new File(TEMP_DIRECTORY, MiscUtils.randomString(32))).exists()) + { + } + + return directory; + } +} diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/util/TempFiles.java b/src/main/java/the/bytecode/club/bytecodeviewer/util/TempFiles.java deleted file mode 100644 index 15da1a29..00000000 --- a/src/main/java/the/bytecode/club/bytecodeviewer/util/TempFiles.java +++ /dev/null @@ -1,43 +0,0 @@ -package the.bytecode.club.bytecodeviewer.util; - -import java.io.File; - -import static the.bytecode.club.bytecodeviewer.Constants.TEMP_DIRECTORY; - -/** - * @author Konloch - * @since 10/2/2024 - */ -public class TempFiles -{ - public static File createTemporaryFile(boolean newDirecory, String extension) - { - //genereate a new temporary parent directory - File parent = newDirecory ? createTemporaryDirectory() : new File(TEMP_DIRECTORY); - - //make the parent directories - parent.mkdirs(); - - //return the temporary file - File file; - - //generate a new name until the directory no longer exists - while((file = new File(parent, MiscUtils.getUniqueName("", extension))).exists()) - { - } - - return file; - } - - public static File createTemporaryDirectory() - { - File directory; - - //generate a new name until the directory no longer exists - while((directory = new File(TEMP_DIRECTORY, MiscUtils.randomString(32))).exists()) - { - } - - return directory; - } -}