mirror of
https://github.com/overcuriousity/autopsy-flatpak.git
synced 2025-07-15 09:17:42 +00:00
fix npe by optionalizing getCachefile
This commit is contained in:
parent
8346c23de9
commit
dac18bf6da
@ -119,28 +119,29 @@ public enum ThumbnailCache {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* load a thumbnail from the disk based cache for the given file, or
|
* load a thumbnail from the disk based cache for the given file, or
|
||||||
* generate and save a new thumnbail if one doesn't already exist
|
* generate and save a new thumbnail if one doesn't already exist
|
||||||
*
|
*
|
||||||
* @param file the file to load a thumbnail of
|
* @param file the DrawableFile to load a thumbnail of
|
||||||
*
|
*
|
||||||
* @return an optional containing a thumbnail, or null if a thumbnail
|
* @return an (possibly empty) optional containing a thumbnail
|
||||||
* couldn't be loaded or generated
|
|
||||||
*/
|
*/
|
||||||
private Optional<Image> load(DrawableFile<?> file) {
|
private Optional<Image> load(DrawableFile<?> file) {
|
||||||
Image thumbnail = null;
|
Image thumbnail;
|
||||||
File cacheFile;
|
|
||||||
try {// try to load the thumbnail from disk
|
|
||||||
cacheFile = getCacheFile(file.getId());
|
|
||||||
|
|
||||||
if (cacheFile.exists()) {
|
try {
|
||||||
|
thumbnail = getCacheFile(file.getId()).map((File cachFile) -> {
|
||||||
|
if (cachFile.exists()) {
|
||||||
// If a thumbnail file is already saved locally, load it
|
// If a thumbnail file is already saved locally, load it
|
||||||
try {
|
try {
|
||||||
int dim = iconSize.get();
|
int dim = iconSize.get();
|
||||||
thumbnail = new Image(Utilities.toURI(cacheFile).toURL().toString(), dim, dim, true, false, true);
|
return new Image(Utilities.toURI(cachFile).toURL().toString(), dim, dim, true, false, true);
|
||||||
} catch (MalformedURLException ex) {
|
} catch (MalformedURLException ex) {
|
||||||
Exceptions.printStackTrace(ex);
|
LOGGER.log(Level.WARNING, "Unable to parse cache file path..");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
|
}).orElse(null);
|
||||||
|
|
||||||
} catch (IllegalStateException e) {
|
} catch (IllegalStateException e) {
|
||||||
LOGGER.log(Level.WARNING, "can't load icon when no case is open");
|
LOGGER.log(Level.WARNING, "can't load icon when no case is open");
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
@ -153,9 +154,23 @@ public enum ThumbnailCache {
|
|||||||
return Optional.ofNullable(thumbnail); //return icon, or null if generation failed
|
return Optional.ofNullable(thumbnail); //return icon, or null if generation failed
|
||||||
}
|
}
|
||||||
|
|
||||||
private static File getCacheFile(long id) {
|
/**
|
||||||
|
* get a File to store the cached icon in.
|
||||||
|
*
|
||||||
|
* @param id the obj id of the file to get a cache file for
|
||||||
|
*
|
||||||
|
* @return a Optional containing a File to store the cahced icon in or an
|
||||||
|
* empty optional if there was a
|
||||||
|
* problem.
|
||||||
|
*/
|
||||||
|
private static Optional<File> getCacheFile(long id) {
|
||||||
|
try {
|
||||||
// @@@ should use ImageUtils.getFile();
|
// @@@ should use ImageUtils.getFile();
|
||||||
return new File(Case.getCurrentCase().getCacheDirectory() + File.separator + id + ".png");
|
return Optional.of(new File(Case.getCurrentCase().getCacheDirectory() + File.separator + id + ".png"));
|
||||||
|
} catch (IllegalStateException e) {
|
||||||
|
LOGGER.log(Level.WARNING, "Failed to create cache file.{0}", e.getLocalizedMessage());
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -255,15 +270,15 @@ public enum ThumbnailCache {
|
|||||||
* @param bi the thumbnail to save for the given DrawableFile
|
* @param bi the thumbnail to save for the given DrawableFile
|
||||||
*/
|
*/
|
||||||
private void saveIcon(final DrawableFile<?> file, final Image bi) {
|
private void saveIcon(final DrawableFile<?> file, final Image bi) {
|
||||||
try {
|
|
||||||
if (bi != null) {
|
if (bi != null) {
|
||||||
File f = getCacheFile(file.getId());
|
getCacheFile(file.getId()).ifPresent((File cacheFile) -> {
|
||||||
|
try {
|
||||||
//convert back to swing to save
|
//convert back to swing to save
|
||||||
ImageIO.write(SwingFXUtils.fromFXImage(bi, null), FORMAT, f);
|
ImageIO.write(SwingFXUtils.fromFXImage(bi, null), FORMAT, cacheFile);
|
||||||
}
|
|
||||||
} catch (IllegalArgumentException | IOException ex) {
|
} catch (IllegalArgumentException | IOException ex) {
|
||||||
//LOGGER.log(Level.WARNING, "failed to save generated icon ", ex);
|
|
||||||
LOGGER.log(Level.WARNING, "failed to save generated icon");
|
LOGGER.log(Level.WARNING, "failed to save generated icon");
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user