Skip to content

Commit 7645ea2

Browse files
committed
Update tests
- Remove unused ImageStoreService methods - Restore analyticsSerivce functionality - Fix ImageControllerSpec unit tests for new ImageController internals
1 parent 98c47b2 commit 7645ea2

File tree

6 files changed

+47
-145
lines changed

6 files changed

+47
-145
lines changed

grails-app/controllers/au/org/ala/images/ImageController.groovy

+5-6
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,7 @@ class ImageController {
242242
boolean contentDisposition = params.boolean("contentDisposition", false)
243243

244244
if (sendAnalytics) {
245-
// TODO: send analytics
246-
// analyticsService.sendAnalytics(imageInstance, 'imageview', request.getHeader("User-Agent"))
245+
analyticsService.sendAnalytics(imageInfo.exists, imageInfo.dataResourceUid, 'imageview', request.getHeader("User-Agent"))
247246
}
248247

249248
if (imageInfo.redirectUri) {
@@ -308,7 +307,7 @@ class ImageController {
308307
writeRangePart(range, imageInfo, boundary, contentType, pw, out)
309308
}
310309
finaliseMultipartResponse(boundary, pw)
311-
response.flushBuffer()
310+
// response.flushBuffer()
312311
} else {
313312
Range range = ranges[0]
314313
long rangeLength = range.length()
@@ -622,7 +621,7 @@ class ImageController {
622621
def resourceLevel = collectoryService.getResourceLevelMetadata(image.dataResourceUid)
623622

624623
if (grailsApplication.config.getProperty('analytics.trackDetailedView', Boolean, false)) {
625-
analyticsService.sendAnalytics(image, 'imagedetailedview', request.getHeader("User-Agent"))
624+
analyticsService.sendAnalytics(image != null, image?.dataResourceUid, 'imagedetailedview', request.getHeader("User-Agent"))
626625
}
627626

628627
[imageInstance: image, subimages: subimages,
@@ -639,7 +638,7 @@ class ImageController {
639638
def subimages = Subimage.findAllByParentImage(image)*.subimage
640639

641640
if (grailsApplication.config.getProperty('analytics.trackLargeViewer', Boolean, false)) {
642-
analyticsService.sendAnalytics(image, 'imagelargeviewer', request.getHeader("User-Agent"))
641+
analyticsService.sendAnalytics(image != null, image?.dataResourceUid, 'imagelargeviewer', request.getHeader("User-Agent"))
643642
}
644643

645644
render (view: 'viewer', model: [imageInstance: image, subimages: subimages])
@@ -717,7 +716,7 @@ class ImageController {
717716
return
718717
}
719718
if (grailsApplication.config.getProperty('analytics.trackLargeViewer', Boolean)) {
720-
analyticsService.sendAnalytics(imageInstance, 'imagelargeviewer', request.getHeader("User-Agent"))
719+
analyticsService.sendAnalytics(imageInstance != null, imageInstance?.dataResourceUid, 'imagelargeviewer', request.getHeader("User-Agent"))
721720
}
722721
[imageInstance: imageInstance, auxDataUrl: params.infoUrl]
723722
}

grails-app/services/au/org/ala/images/AnalyticsService.groovy

+5-4
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,23 @@ class AnalyticsService {
9494
/**
9595
* POST event data to google analytics.
9696
*
97-
* @param imageInstance
97+
* @param exists Does the image instance exist in the database
98+
* @param dataResourceUid The data resource uid the image instance belongs to
9899
* @param eventCategory
99100
* @return
100101
*/
101102
@NotTransactional
102-
def sendAnalytics(Image imageInstance, String eventCategory, String userAgent) {
103+
def sendAnalytics(boolean exists, String dataResourceUid, String eventCategory, String userAgent) {
103104
final analyticsId = grailsApplication.config.getProperty('analytics.ID')
104-
if (imageInstance && analyticsId) {
105+
if (exists && analyticsId) {
105106
final queryURL = grailsApplication.config.getProperty('analytics.URL')
106107
final requestBody = [
107108
'v': 1,
108109
'tid': analyticsId,
109110
'cid': UUID.randomUUID().toString(), //anonymous client ID
110111
't': 'event',
111112
'ec': eventCategory, // event category
112-
'ea': imageInstance.dataResourceUid, //event value
113+
'ea': dataResourceUid, //event value
113114
'ua' : userAgent
114115
]
115116

grails-app/services/au/org/ala/images/ImageService.groovy

+1-1
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ SELECT
739739
if (metadata && metadata instanceof JpegImageMetadata) {
740740
JpegImageMetadata jpegMetadata = metadata
741741

742-
def date = getImageTagValue(jpegMetadata,TiffConstants.EXIF_TAG_DATE_TIME_ORIGINAL)
742+
def date = getImageTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL)
743743
if (date) {
744744
def sdf = new SimpleDateFormat("yyyy:MM:dd hh:mm:ss")
745745
return sdf.parse(date.toString())

grails-app/services/au/org/ala/images/ImageStoreService.groovy

+13-85
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ class ImageStoreService {
424424
}
425425
}
426426

427-
private ImageInfo ensureThumbnailExists(String imageIdentifier, StorageOperations operations, String type) {
427+
private ImageInfo ensureThumbnailExists(String imageIdentifier, String dataResourceUid, StorageOperations operations, String type) {
428428
type = normaliseThumbnailType(type)
429429
return thumbnailCache.get(Pair.of(imageIdentifier, type), { pair ->
430430
def imageIdentifierArg = pair.left
@@ -474,6 +474,7 @@ class ImageStoreService {
474474
// override these based on behaviour of the thumbnailer
475475
info.contentType = typeArg == 'square' ? 'image/png' : 'image/jpeg'
476476
info.extension = typeArg == 'square' ? 'png' : 'jpg'
477+
info.dataResourceUid = dataResourceUid
477478
return info
478479
} catch (e) {
479480
log.error("Error generating thumbnail for image ${imageIdentifier} of type ${typeArg}", e)
@@ -489,7 +490,7 @@ class ImageStoreService {
489490
int z
490491
}
491492

492-
private ImageInfo ensureTileExists(String identifier, int zoomLevels, StorageOperations operations, int x, int y, int z) {
493+
private ImageInfo ensureTileExists(String identifier, String dataResourceUid, int zoomLevels, StorageOperations operations, int x, int y, int z) {
493494
if (zoomLevels > 0 && z > zoomLevels) {
494495
// requested zoom level is beyond the zoom levels of the image so the tile will never exist
495496
return new ImageInfo(exists: false, imageIdentifier: identifier)
@@ -521,6 +522,7 @@ class ImageStoreService {
521522
// or if the origin doesn't exist then any tile for the given zoom level won't exist either
522523
// so return the non-existent origin info
523524
if (x == 0 && y == 0 || !originInfo.exists) {
525+
originInfo.dataResourceUid = dataResourceUid
524526
return originInfo
525527
} else {
526528
// otherwise now we get the info for the tile that was actually requested and cache it
@@ -535,96 +537,17 @@ class ImageStoreService {
535537
return null // don't cache this error
536538
}
537539
}) ?: new ImageInfo(exists: false, imageIdentifier: identifier)
540+
tileInfo.dataResourceUid = dataResourceUid
538541
return tileInfo
539542
}
540543
}
541544

542-
// delegating methods for Unit Testing
543-
InputStream originalInputStream(Image image, Range range) {
544-
image.originalInputStream(range)
545-
}
546-
547-
URI originalRedirectLocation(Image image) {
548-
image.storageLocation.originalRedirectLocation(image.imageIdentifier)
549-
}
550-
551-
long thumbnailStoredLength(Image image) {
552-
def info = ensureThumbnailExists(image, '')
553-
if (!info.exists) {
554-
return -1
555-
} else {
556-
return info.length
557-
}
558-
}
559-
560-
InputStream thumbnailInputStream(Image image, Range range) {
561-
def info = ensureThumbnailExists(image, '')
562-
if (!info.exists) {
563-
return new ByteArrayInputStream(new byte[0])
564-
} else {
565-
return info.inputStreamSupplier(range)
566-
}
567-
}
568-
569-
long thumbnailTypeStoredLength(Image image, String type) {
570-
def info = ensureThumbnailExists(image, type)
571-
if (!info.exists) {
572-
return -1
573-
} else {
574-
return info.length
575-
}
576-
}
577-
578-
InputStream thumbnailTypeInputStream(Image image, String type, Range range) {
579-
def info = ensureThumbnailExists(image, type)
580-
if (!info.exists) {
581-
return new ByteArrayInputStream(new byte[0])
582-
} else {
583-
return info.inputStreamSupplier(range)
584-
}
585-
}
586-
587-
long tileStoredLength(Image image, int x, int y, int z) {
588-
def info = ensureTileExists(image, x, y, z)
589-
if (!info.exists) {
590-
return -1
591-
} else {
592-
info.length
593-
}
594-
}
595-
596-
InputStream tileInputStream(Image image, Range range, int x, int y, int z) {
597-
def info = ensureTileExists(image, x, y, z)
598-
if (!info.exists) {
599-
return new ByteArrayInputStream(new byte[0])
600-
} else {
601-
info.inputStreamSupplier(range)
602-
}
603-
}
604-
605-
URI thumbnailRedirectLocation(Image image, String type) {
606-
def info = ensureThumbnailExists(image, type)
607-
if (!info.exists) {
608-
return null
609-
} else {
610-
return info.redirectUri
611-
}
612-
}
613-
614-
URI tileRedirectLocation(Image image, int x, int y, int z) {
615-
def info = ensureTileExists(image, x, y, z)
616-
if (!info.exists) {
617-
return null
618-
} else {
619-
info.redirectUri
620-
}
621-
}
622-
623545
ImageInfo originalImageInfo(String imageIdentifier) {
624546
def image = Image.findByImageIdentifier(imageIdentifier, [ cache: true, fetch: [ storageLocation: 'join' ] ])
625547
if (image) {
626548
def imageInfo = image.storageLocation.originalImageInfo(image.imageIdentifier)
627549
// override these to match original behaviour
550+
imageInfo.dataResourceUid = image.dataResourceUid
628551
imageInfo.etag = image.contentSHA1Hash
629552
imageInfo.lastModified = image.dateUploaded
630553
imageInfo.contentType = image.mimeType
@@ -638,15 +561,17 @@ class ImageStoreService {
638561
ImageInfo thumbnailImageInfo(String imageIdentifier, String type) {
639562
Image image
640563
StorageOperations operations = null
564+
String dataResourceUid = null
641565
Image.withNewTransaction(readOnly: true) {
642566
image = Image.findByImageIdentifier(imageIdentifier, [ cache: true, fetch: [ storageLocation: 'join' ] ])
643567
if (image) {
644568
operations = GrailsHibernateUtil.unwrapIfProxy(image.storageLocation).asStandaloneStorageOperations()
569+
dataResourceUid = image.dataResourceUid
645570
}
646571
}
647572
if (image) {
648573
if (image.mimeType.startsWith('image/')) {
649-
def info = ensureThumbnailExists(imageIdentifier, operations, type)
574+
def info = ensureThumbnailExists(imageIdentifier, dataResourceUid, operations, type)
650575
if (info) {
651576
return info
652577
}
@@ -669,6 +594,7 @@ class ImageStoreService {
669594
return new ImageInfo(
670595
exists: true,
671596
imageIdentifier: imageIdentifier,
597+
dataResourceUid: dataResourceUid,
672598
length: resource.contentLength(),
673599
lastModified: new Date(resource.lastModified()),
674600
contentType: 'image/png',
@@ -686,15 +612,17 @@ class ImageStoreService {
686612

687613
Image image
688614
StorageOperations operations = null
615+
String dataResourceUid = null
689616
Image.withNewTransaction(readOnly: true) {
690617
image = Image.findByImageIdentifier(imageIdentifier, [ cache: true, fetch: [ storageLocation: 'join' ] ])
691618
if (image) {
692619
operations = GrailsHibernateUtil.unwrapIfProxy(image.storageLocation).asStandaloneStorageOperations()
620+
dataResourceUid = image.dataResourceUid
693621
}
694622
}
695623
if (image) {
696624
if (image.mimeType.startsWith('image/')) {
697-
def info = ensureTileExists(imageIdentifier, image.zoomLevels, operations, x, y, z)
625+
def info = ensureTileExists(imageIdentifier, dataResourceUid, image.zoomLevels, operations, x, y, z)
698626
return info
699627
}
700628
}

src/main/groovy/au/org/ala/images/ImageInfo.groovy

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class ImageInfo {
44

55
boolean exists
66
String imageIdentifier
7+
String dataResourceUid
78
long length
89
String etag
910
Date lastModified

0 commit comments

Comments
 (0)