From 929eb4ca3d97a6d9dd2221e31af2b6a456a7d84e Mon Sep 17 00:00:00 2001 From: Drew Short Date: Fri, 7 Feb 2014 01:40:17 -0600 Subject: [PATCH] Partial fix for nulls during thumbnail caching. --- .../sothr/imagetools/image/ImageService.scala | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/main/scala/com/sothr/imagetools/image/ImageService.scala b/src/main/scala/com/sothr/imagetools/image/ImageService.scala index ed3ad5a..1c1421d 100644 --- a/src/main/scala/com/sothr/imagetools/image/ImageService.scala +++ b/src/main/scala/com/sothr/imagetools/image/ImageService.scala @@ -19,7 +19,11 @@ object ImageService extends Logging { private def lookupImage(file:File):Image = { var image:Image = null //get from memory cache if possible - if (imageCache.isKeyInCache(file.getAbsolutePath)) image = imageCache.get(file.getAbsolutePath).getObjectValue.asInstanceOf[Image] + try { + if (imageCache.isKeyInCache(file.getAbsolutePath)) image = imageCache.get(file.getAbsolutePath).getObjectValue.asInstanceOf[Image] + } catch { + case npe:NullPointerException => error(s"Error grabbing \'${file.getAbsolutePath}\' from cache", npe) + } //get from datastore if possible image } @@ -57,7 +61,7 @@ object ImageService extends Logging { def calculateThumbPath(md5:String):String = { //break the path down into 4 char parts val split:List[String] = md5.grouped(4).toList - var path:String = PropertiesService.get(PropertiesEnum.ThumbnailDirectory.toString) + "/" + PropertiesService.get(PropertiesEnum.ThumbnailSize.toString) + "/" + var path:String = PropertiesService.get(PropertiesEnum.ThumbnailDirectory.toString) + PropertiesService.get(PropertiesEnum.ThumbnailSize.toString) + "/" for (seg <- split) path += seg + "/" try { val dir = new File(path) @@ -71,12 +75,20 @@ object ImageService extends Logging { def lookupThumbnailPath(md5:String):String = { var thumbPath:String = null - //get from memory cache if possible - if (thumbnailCache.isKeyInCache(md5)) thumbPath = imageCache.get(md5).getObjectValue.asInstanceOf[String] - //get from datastore if possible - //check for the actual file - val checkPath = calculateThumbPath(md5) - if (new File(checkPath).exists) thumbPath = checkPath + if (md5 != null) { + //get from memory cache if possible + try { + if (thumbnailCache.isKeyInCache(md5)) thumbPath = thumbnailCache.get(md5).getObjectValue.asInstanceOf[String] + } catch { + case npe:NullPointerException => error(s"Error grabbing \'$md5\' from cache. thumbPath: \'$thumbPath\'", npe) + } + //get from datastore if possible + //check for the actual file + val checkPath = calculateThumbPath(md5) + if (new File(checkPath).exists) thumbPath = checkPath + } else { + error("Null md5 passed in") + } thumbPath } @@ -97,6 +109,7 @@ object ImageService extends Logging { } def saveThumbnail(md5:String, thumbnailPath:String):String = { + if (md5 == null || thumbnailPath == null) error(s"Error trying to save a md5: $md5 path: $thumbnailPath") //save to cache thumbnailCache.put(new Element(md5, thumbnailPath)) //save to datastore