summaryrefslogtreecommitdiffstats
path: root/idrop-web/grails-app/services
diff options
context:
space:
mode:
authorMike Conway <michael.c.conway@gmail.com>2012-01-09 18:06:55 (GMT)
committer Mike Conway <michael.c.conway@gmail.com>2012-01-09 18:06:55 (GMT)
commit47ce7fb60148787a1ec6d9df0bea2bf434fc789a (patch)
tree815aa71fc9241a110dd2e7ac696b86a62e0ee78f /idrop-web/grails-app/services
parentb356894ee9a690bf17d32bde1ccd6c2cc4770777 (diff)
downloadQCG-Data-47ce7fb60148787a1ec6d9df0bea2bf434fc789a.zip
QCG-Data-47ce7fb60148787a1ec6d9df0bea2bf434fc789a.tar.gz
QCG-Data-47ce7fb60148787a1ec6d9df0bea2bf434fc789a.tar.bz2
[#584] occ'l broken images in gallery view
Diffstat (limited to 'idrop-web/grails-app/services')
-rw-r--r--idrop-web/grails-app/services/org/irods/mydrop/service/ThumbnailGeneratorService.groovy113
1 files changed, 113 insertions, 0 deletions
diff --git a/idrop-web/grails-app/services/org/irods/mydrop/service/ThumbnailGeneratorService.groovy b/idrop-web/grails-app/services/org/irods/mydrop/service/ThumbnailGeneratorService.groovy
new file mode 100644
index 0000000..a4294a9
--- /dev/null
+++ b/idrop-web/grails-app/services/org/irods/mydrop/service/ThumbnailGeneratorService.groovy
@@ -0,0 +1,113 @@
+package org.irods.mydrop.service
+
+import org.irods.jargon.core.connection.IRODSAccount
+import org.irods.jargon.core.pub.IRODSAccessObjectFactory
+import org.irods.jargon.datautils.image.ThumbnailService
+import org.irods.jargon.datautils.image.ThumbnailServiceImpl
+import org.irods.mydrop.config.ServerProperties
+import org.irods.mydrop.config.ServerPropertiesCache
+
+class ThumbnailGeneratorService {
+
+ static transactional = false
+ static String IMAGE_PROP = "irods_thumbnail"
+ private ServerPropertiesCache serverPropertiesCache = new ServerPropertiesCache()
+ private String tempDir = null
+
+ public ThumbnailProcessResult getStreamForThumbnailImage(String irodsAbsolutePath, IRODSAccount irodsAccount, IRODSAccessObjectFactory irodsAccessObjectFactory, File tempDir) {
+
+ if (irodsAbsolutePath == null || irodsAbsolutePath.isEmpty()) {
+ throw new IllegalArgumentException("null or empty irodsAbsolutePath")
+ }
+
+ if (irodsAccount == null) {
+ throw new IllegalArgumentException("null irodsAccount")
+ }
+
+ if (irodsAccessObjectFactory == null) {
+ throw new IllegalArgumentException("null or empty irodsAccessObjectFactory")
+ }
+
+ if (tempDir == null) {
+ throw new IllegalArgumentException("null tempDir")
+ }
+
+ boolean supportsImageMagik = isServerSupportImageMagik(irodsAccount, irodsAccessObjectFactory)
+
+ ThumbnailProcessResult thumbnailProcessResult = null
+ ThumbnailService thumbnailService = new ThumbnailServiceImpl(irodsAccessObjectFactory, irodsAccount)
+
+ if (supportsImageMagik) {
+ log.info("cacheProp is true, use thumbnail process on iRODS")
+ InputStream thumbnailData = new BufferedInputStream(thumbnailService.retrieveThumbnailByIRODSAbsolutePathViaRule(irodsAbsolutePath))
+ thumbnailProcessResult = new ThumbnailProcessResult()
+ thumbnailProcessResult.thumbnailStream = thumbnailData
+ } else {
+ log.info("using fallback, cacheProp is false")
+ log.info "tempdir:${tempDir}"
+ File tempThumbnailFile = thumbnailService.createThumbnailLocallyViaJAI(tempDir, irodsAbsolutePath, 300)
+ InputStream thumbnailData = new BufferedInputStream(new FileInputStream(tempThumbnailFile))
+ thumbnailProcessResult = new ThumbnailProcessResult()
+ thumbnailProcessResult.thumbnailStream = thumbnailData
+ thumbnailProcessResult.cleanupFile = tempThumbnailFile
+ }
+
+ return thumbnailProcessResult
+ }
+
+ /**
+ * Synchronized check to see if server supports imageMagik
+ * @param irodsAccount
+ * @param IRODSAccessObjectFactory
+ * @return
+ */
+ private synchronized boolean isServerSupportImageMagik(IRODSAccount irodsAccount, IRODSAccessObjectFactory irodsAccessObjectFactory) {
+
+ if (irodsAccount == null) {
+ throw new IllegalArgumentException("irodsAccount is null")
+ }
+
+ if (irodsAccessObjectFactory == null) {
+ throw new IllegalArgumentException("null irodsAccessObjectFactory")
+ }
+
+ boolean goToIrods = false
+
+ ServerProperties serverProperties = serverPropertiesCache.getServerProperties(irodsAccount)
+ if (serverProperties.properties[IMAGE_PROP] == null) {
+ log.info("go to server to get cache")
+ ThumbnailService thumbnailService = new ThumbnailServiceImpl(irodsAccessObjectFactory, irodsAccount)
+ goToIrods = thumbnailService.isIRODSThumbnailGeneratorAvailable()
+ log.info("isIRODSThumbnailGeneratorAvailable? ${goToIrods}")
+ if (goToIrods) {
+ serverProperties.properties[IMAGE_PROP] = true
+ } else {
+ serverProperties.properties[IMAGE_PROP] = false
+ }
+ } else {
+ log.info("server properties:${serverProperties}")
+ def cacheProp = serverProperties.properties[IMAGE_PROP]
+ if (cacheProp) {
+ goToIrods = true
+ }
+ }
+
+ log.info("Derived goToIrods value to determine whether imageMagik is on iRODS = ${goToIrods}")
+ return goToIrods
+ }
+}
+
+public class ThumbnailProcessResult {
+ InputStream thumbnailStream
+ File cleanupFile
+
+ public void cleanUpIfNeeded() {
+ if (cleanupFile) {
+ try {
+ cleanupFile.delete()
+ } catch (Exception e) {
+ log.warn("exception closing up cleanup file: ${e}")
+ }
+ }
+ }
+}