Skip to content

Commit

Permalink
Merge branch 'release/2.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
sbearcsiro committed May 25, 2023
2 parents 1d47443 + 39679ce commit 5d5ecd0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ plugins {
id "org.flywaydb.flyway" version "5.2.4"
}

version "2.0.2"
version "2.0.3"

group "au.org.ala"

Expand Down
7 changes: 5 additions & 2 deletions grails-app/conf/application.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
---
info:
app:
name: 'image-service'
app:
name: '@info.app.name@'
version: '@info.app.version@'
grailsVersion: '@info.app.grailsVersion@'

grails:
serverURL: 'http://dev.ala.org.au:8080'
profile: web
Expand Down
28 changes: 25 additions & 3 deletions grails-app/services/au/org/ala/images/ImageService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import java.text.SimpleDateFormat
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.locks.ReentrantLock

import static grails.web.http.HttpHeaders.USER_AGENT

class ImageService {

def dataSource
Expand Down Expand Up @@ -109,9 +111,21 @@ SELECT
@Value('${http.default.connectTimeoutMs:120000}')
int connectTimeoutMs = 120000 // 2 minutes

@Value('${http.default.user-agent:}')
String userAgent

@Value('${batch.purge.fetch.size:100}')
int purgeFetchSize = 100

@Value('${skin.orgNameShort:ALA}')
String orgNameShort

@Value('${info.app.name:image-service}')
String appName

@Value('${info.app.version:NaN}')
String version

Map imagePropertyMap = null

ImageStoreResult storeImage(MultipartFile imageFile, String uploader, Map metadata = [:]) {
Expand All @@ -136,6 +150,14 @@ SELECT
fw.close()
}

private String userAgent() {
def userAgent = this.userAgent
if (!userAgent) {
userAgent = "$orgNameShort-$appName/$version"
}
return userAgent
}

ImageStoreResult storeImageFromUrl(String imageUrl, String uploader, Map metadata = [:]) {
if (imageUrl) {
try {
Expand All @@ -145,7 +167,7 @@ SELECT
return new ImageStoreResult(image, true, image.alternateFilename?.contains(imageUrl) ?: false)
}
def url = new URL(imageUrl)
def bytes = url.getBytes(connectTimeout: connectTimeoutMs, readTimeout: readTimeoutMs)
def bytes = url.getBytes(connectTimeout: connectTimeoutMs, readTimeout: readTimeoutMs, requestProperties: [USER_AGENT: userAgent()])

def contentType = null

Expand Down Expand Up @@ -280,7 +302,7 @@ SELECT
def result = [success: false, alreadyStored: false]
try {
def url = new URL(imageUrl)
def bytes = url.getBytes(connectTimeout: connectTimeoutMs, readTimeout: readTimeoutMs)
def bytes = url.getBytes(connectTimeout: connectTimeoutMs, readTimeout: readTimeoutMs, requestProperties: [USER_AGENT: userAgent()])
def contentType = detectMimeTypeFromBytes(bytes, imageUrl)
ImageStoreResult storeResult = storeImageBytes(bytes, imageUrl, bytes.length,
contentType, uploader, true, imageSource)
Expand Down Expand Up @@ -372,7 +394,7 @@ SELECT
def bytes
try {
def url = new URL(imageUrl)
bytes = url.getBytes(connectTimeout: connectTimeoutMs, readTimeout: readTimeoutMs)
bytes = url.getBytes(connectTimeout: connectTimeoutMs, readTimeout: readTimeoutMs, requestProperties: [USER_AGENT: userAgent()])
} catch (Exception e){
log.error("Unable to load image from URL: {}. Logging as failed URL", imageUrl)
logBadUrl(imageUrl)
Expand Down

0 comments on commit 5d5ecd0

Please sign in to comment.