Skip to content

Commit

Permalink
IOHelper: redo temporary files
Browse files Browse the repository at this point in the history
  • Loading branch information
Traneptora committed Oct 19, 2017
1 parent 7a211b0 commit 36a11ed
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 44 deletions.
55 changes: 24 additions & 31 deletions src/thebombzen/tumblgififier/TumblGIFifier.java
Expand Up @@ -147,41 +147,34 @@ public static synchronized void executeOldVersionCleanup(){
} catch (RuntimeIOException | NumberFormatException e){
version = 0;
}
if (version < 1){
boolean did = false;

try {
File tempFile = IOHelper.createTempFile();
File tempFileDirectory = tempFile.getParentFile();
IOHelper.deleteTempFile(tempFile);
for (File f : tempFileDirectory.listFiles()){
if (f.getName().matches("^tumblgififier(.*)\\.tmp$")){
if (!did){
//processor.appendStatus("Executing Cleanup Routine: 1.");
processor.appendStatus("Cleaning old temporary files... ");
}
did = true;
processor.replaceStatus("Cleaning old temporary files... " + f.getName());
IOHelper.deleteTempFile(f);
}
boolean did = false;
try {
File tempFileDirectory = ResourcesManager.getResourcesManager().getTemporaryDirectory().toFile();
for (File f : tempFileDirectory.listFiles()){
if (!did){
processor.appendStatus("Cleaning old temporary files... ");
}
} catch (RuntimeIOException ioe){
processor.appendStatus("Error cleaning old temporary files.");
processor.processException(ioe);
}
if (did){
processor.replaceStatus("Cleaning old temporary files... Done.");
}
File profileMedium = ResourcesManager.getResourcesManager().getLocalFile("Profile-Medium.otf");
File profileMediumXZ = ResourcesManager.getResourcesManager().getLocalFile("Profile-Medium.otf.xz");
if (profileMedium.exists() || profileMediumXZ.exists()){
processor.appendStatus("Cleaning old font files... ");
IOHelper.deleteTempFile(profileMedium);
IOHelper.deleteTempFile(profileMediumXZ);
processor.replaceStatus("Cleaning old font files... Done.");
did = true;
processor.replaceStatus("Cleaning old temporary files... " + f.getName());
IOHelper.deleteTempFile(f);
}

} catch (RuntimeIOException ioe){
processor.appendStatus("Error cleaning old temporary files.");
processor.processException(ioe);
}
if (did){
processor.replaceStatus("Cleaning old temporary files... Done.");
}
File profileMedium = ResourcesManager.getResourcesManager().getLocalFile("Profile-Medium.otf");
File profileMediumXZ = ResourcesManager.getResourcesManager().getLocalFile("Profile-Medium.otf.xz");
if (profileMedium.exists() || profileMediumXZ.exists()){
processor.appendStatus("Cleaning old font files... ");
IOHelper.deleteTempFile(profileMedium);
IOHelper.deleteTempFile(profileMediumXZ);
processor.replaceStatus("Cleaning old font files... Done.");
}

if (version < 2){
//processor.appendStatus("Executing Cleanup Routine: 2.");
File error = ResourcesManager.getResourcesManager().getLocalFile("error.log");
Expand Down
32 changes: 20 additions & 12 deletions src/thebombzen/tumblgififier/util/io/IOHelper.java
Expand Up @@ -14,12 +14,15 @@
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.tukaani.xz.XZInputStream;
import thebombzen.tumblgififier.util.ConcurrenceManager;
import thebombzen.tumblgififier.util.Task;
import thebombzen.tumblgififier.util.io.resources.ResourcesManager;

/**
* Java's I/O libraries are nice but not perfect. This class contains a (sigh) framework and some helper routines to make everything easier.
Expand All @@ -33,7 +36,7 @@ private IOHelper(){
/**
* We manage our own temp files. This is a list of the file names of temp files we've created. It's synchronized so we don't have to worry about anything like a ConcurrentModificationException.
*/
private static List<String> tempFiles = Collections.synchronizedList(new ArrayList<String>());
private static List<Path> tempFiles = Collections.synchronizedList(new ArrayList<Path>());

/*
* In general, temp files should be deleted, but this is a backup just to be safe.
Expand All @@ -42,8 +45,8 @@ private IOHelper(){
ConcurrenceManager.getConcurrenceManager().addShutdownTask(new Task(){
@Override
public void run(){
for (String f : tempFiles){
new File(f).delete();
for (Path path : tempFiles){
path.toFile().delete();
}
}
});
Expand All @@ -55,11 +58,15 @@ public void run(){
* This will usually not throw an I/O exception, but could in some corner cases, like if the temp filesystem is mounted as read-only.
*/
public static File createTempFile() {
return createTempFile(ResourcesManager.getResourcesManager().getTemporaryDirectory());
}

public static File createTempFile(Path parentDirectory) {
try {
File f = File.createTempFile("tumblgififier", ".tmp");
f.deleteOnExit();
tempFiles.add(f.getCanonicalPath());
return f;
Path path = Files.createTempFile(parentDirectory, "tumblgififier", ".tmp").toAbsolutePath();
path.toFile().deleteOnExit();
tempFiles.add(path);
return path.toFile();
} catch (IOException ioe){
throw new RuntimeIOException(ioe);
}
Expand All @@ -68,9 +75,10 @@ public static File createTempFile() {
/**
* This marks a file as a temporary file, so it will be deleted on exit.
*/
public static void markTempFile(String file) {
tempFiles.add(file);
new File(file).deleteOnExit();
public static void markTempFile(Path filepath) {
Path absolutePath = filepath.toAbsolutePath();
absolutePath.toFile().deleteOnExit();
tempFiles.add(absolutePath);
}

/**
Expand Down Expand Up @@ -101,9 +109,9 @@ public static boolean deleteTempFile(File f) {
return false;
} else {
try {
tempFiles.remove(f.getCanonicalPath());
tempFiles.remove(f.toPath().toRealPath());
} catch (IOException ioe){
tempFiles.remove(f.getAbsolutePath());
tempFiles.remove(f.toPath().toAbsolutePath());
}
return true;
}
Expand Down
Expand Up @@ -10,6 +10,7 @@
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
Expand Down Expand Up @@ -188,6 +189,15 @@ public String getLocalResourceLocation() {
}
}

public Path getTemporaryDirectory() {
File dir = new File (getLocalResourceLocation(), "temp");
if (dir.exists() && !dir.isDirectory()) {
dir.delete();
}
dir.mkdirs();
return dir.toPath().toAbsolutePath();
}

public Resource getXLocation(String pkg, String x) {
String[] pathElements = System.getenv("PATH").split(File.pathSeparator);
String name = x + TumblGIFifier.EXE_EXTENSION;
Expand Down
2 changes: 1 addition & 1 deletion src/thebombzen/tumblgififier/video/ShotCache.java
Expand Up @@ -108,7 +108,7 @@ private void screenShot0(String overlay, int frameNumber, int shotWidth, int sho
for (int i = 0; i < frames; i++) {
String name = String.format("%s_%06d.png", shotFilename, i + 1);
File tempShotFile = new File(name);
IOHelper.markTempFile(tempShotFile.getAbsolutePath());
IOHelper.markTempFile(tempShotFile.toPath().toAbsolutePath());
if (shotFiles.get(frameNumber + i) != null) {
IOHelper.deleteTempFile(tempShotFile);
} else {
Expand Down

0 comments on commit 36a11ed

Please sign in to comment.