Skip to content

Commit 36cd6af

Browse files
committed
Merge pull request #189 from AtlasOfLivingAustralia/biocollect-342
AtlasOfLivingAustralia/biocollect#342 adds …
2 parents b87b3b6 + 42fd602 commit 36cd6af

File tree

6 files changed

+86
-8
lines changed

6 files changed

+86
-8
lines changed

grails-app/conf/UrlMappings.groovy

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class UrlMappings {
2828
"/ws/audit/getAuditMessagesForProjectPerPage/$id"(controller: "audit", action: "getAuditMessagesForProjectPerPage")
2929

3030
"/ws/document/listImages"(controller: "document", action: "listImages")
31+
"/ws/document/$id/file"(controller: "document", action: "getFile")
3132

3233
"/ws/activitiesForProject/$id" {
3334
controller = 'activity'

grails-app/controllers/au/org/ala/ecodata/DocumentController.groovy

+34
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,40 @@ class DocumentController {
4646
}
4747
}
4848

49+
def getFile() {
50+
if (params.id) {
51+
Map document = documentService.get(params.id)
52+
53+
if (!document) {
54+
response.status = 404
55+
render status:404, text: 'No such id'
56+
} else {
57+
String path = "${grailsApplication.config.app.file.upload.path}${File.separator}${document.filepath}${File.separator}${document.filename}"
58+
59+
File file = new File(path)
60+
61+
if (!file.exists()) {
62+
response.status = 404
63+
return null
64+
}
65+
66+
if (params.forceDownload?.toBoolean()) {
67+
// set the content type to octet-stream to stop the browser from auto playing known types
68+
response.setContentType('application/octet-stream')
69+
} else {
70+
response.setContentType(document.contentType ?: 'application/octet-stream')
71+
}
72+
response.outputStream << new FileInputStream(file)
73+
response.outputStream.flush()
74+
75+
return null
76+
}
77+
} else {
78+
response.status = 400
79+
render status:400, text: 'id is a required parameter'
80+
}
81+
}
82+
4983
def find(String entity, String id) {
5084
if (params.links as boolean) {
5185
def result = documentService.findAllLinksByOwner(entity+'Id', id)

grails-app/services/au/org/ala/ecodata/OutputService.groovy

+22-6
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ class OutputService {
130130

131131
// save images to ecodata
132132
props.data = saveImages(props.data, props.name, output.outputId, props.activityId);
133+
props.data = saveAudio(props.data, props.name, output.outputId, props.activityId);
133134

134135
getCommonService().updateProperties(output, props)
135136

@@ -182,6 +183,7 @@ class OutputService {
182183
try {
183184
// save image properties to db
184185
props.data = saveImages(props.data, props.name, output.outputId, activity.activityId)
186+
props.data = saveAudio(props.data, props.name, output.outputId, activity.activityId)
185187

186188
getCommonService().updateProperties(output, props)
187189

@@ -235,7 +237,21 @@ class OutputService {
235237
* @return the output data, with any image objects updated to include the new document id
236238
*/
237239
Map saveImages(Map output, String metadataName, String outputId, String activityId, Map context = null) {
238-
URL biocollect
240+
saveMultimedia(output, metadataName, outputId, activityId, "image", "surveyImage", "image", context)
241+
}
242+
243+
/**
244+
* find images and save or delete it.
245+
* @param activityId
246+
* @param outputs
247+
* @return the output data, with any image objects updated to include the new document id
248+
*/
249+
Map saveAudio(Map output, String metadataName, String outputId, String activityId, Map context = null) {
250+
saveMultimedia(output, metadataName, outputId, activityId, "audio", "surveyAudio", "audio", context)
251+
}
252+
253+
Map saveMultimedia(Map output, String metadataName, String outputId, String activityId, String dataTypeName, String role, String type, Map context = null) {
254+
URL biocollect
239255
InputStream stream
240256
Map outputMetadata, names
241257
OutputMetadata dataModel
@@ -244,7 +260,7 @@ class OutputService {
244260
if(!context){
245261
outputMetadata = metadataService.getOutputDataModelByName(metadataName) as Map
246262
dataModel = new OutputMetadata(outputMetadata);
247-
names = dataModel.getNamesForDataType('image', null);
263+
names = dataModel.getNamesForDataType(dataTypeName, null);
248264
} else {
249265
names = context
250266
}
@@ -259,8 +275,8 @@ class OutputService {
259275
it.activityId = activityId
260276
it.outputId = outputId
261277
it.remove('staged')
262-
it.role = 'surveyImage'
263-
it.type = 'image'
278+
it.role = role
279+
it.type = type
264280
// record creation requires images to have an 'identifier' attribute containing the url for the image
265281
it.identifier = it.url
266282

@@ -283,12 +299,12 @@ class OutputService {
283299
// recursive check for image data
284300
if(node instanceof Map){
285301
if(output[name] instanceof Map){
286-
output[name] = saveImages(output[name], metadataName, outputId, activityId, node)
302+
output[name] = saveMultimedia(output[name], metadataName, outputId, activityId, dataTypeName, role, type, node)
287303
}
288304

289305
if(output[name] instanceof List){
290306
output[name].eachWithIndex{ column, index ->
291-
output[name][index] = saveImages(column, metadataName, outputId, activityId, node)
307+
output[name][index] = saveMultimedia(column, metadataName, outputId, activityId, dataTypeName, role, type, node)
292308
}
293309
}
294310
}

grails-app/services/au/org/ala/ecodata/ProjectService.groovy

+1-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ class ProjectService {
374374
*/
375375
List<Map> findByName(String name) {
376376
List<Map> matches = []
377-
377+
378378
if (name) {
379379
name = name.replaceAll(" +", " ").trim()
380380
matches = Project.withCriteria {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package au.org.ala.ecodata.converter
2+
3+
class AudioConverter implements RecordFieldConverter {
4+
5+
private static final String DEFAULT_RIGHTS_STATEMENT = "The rights to all uploaded audio are held equally, under a Creative Commons Attribution (CC-BY v3.0) license, by the contributor of the audio and the primary organisation responsible for the project to which they are contributed."
6+
private static final String DEFAULT_LICENCE = "Creative Commons Attribution"
7+
8+
List<Map> convert(Map data, Map metadata = [:]) {
9+
Map record = [:]
10+
11+
record.multimedia = data[metadata.name]
12+
record.multimedia?.each {
13+
if (it instanceof Map) {
14+
it.identifier = it.identifier ?: it.url
15+
it.creator = it.creator ?: it.attribution
16+
it.title = it.title ?: it.filename
17+
it.type = it.type ?: it.contentType
18+
it.rightsHolder = it.creator ?: it.attribution
19+
it.rights = DEFAULT_RIGHTS_STATEMENT
20+
// note: the US spelling of licenSe is expected for the dublin core standard, so don't fix it
21+
it.license = DEFAULT_LICENCE
22+
}
23+
}
24+
25+
[record]
26+
}
27+
}

src/groovy/au/org/ala/ecodata/converter/RecordConverter.groovy

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class RecordConverter {
131131
try {
132132
converter = Class.forName("${packageName}.${className}")?.newInstance()
133133
} catch (ClassNotFoundException e) {
134-
log.warn "No specific converter found for output data type ${outputDataType} with class name ${packageName}.${className}, using generic converter"
134+
log.debug "No specific converter found for output data type ${outputDataType} with class name ${packageName}.${className}, using generic converter"
135135
converter = new GenericFieldConverter()
136136
}
137137

0 commit comments

Comments
 (0)