1
|
/**
|
2
|
* Copyright (C) 2021 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.api.service.media;
|
10
|
|
11
|
import java.io.IOException;
|
12
|
import java.io.InputStream;
|
13
|
|
14
|
import org.apache.http.HttpException;
|
15
|
import org.apache.log4j.Logger;
|
16
|
import org.cybertaxonomy.media.info.model.MediaInfo;
|
17
|
|
18
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
19
|
import com.github.dozermapper.core.DozerBeanMapperBuilder;
|
20
|
import com.github.dozermapper.core.Mapper;
|
21
|
import com.github.dozermapper.core.loader.api.BeanMappingBuilder;
|
22
|
import com.github.dozermapper.core.loader.api.FieldsMappingOptions;
|
23
|
import com.github.dozermapper.core.loader.api.TypeMappingOptions;
|
24
|
|
25
|
import eu.etaxonomy.cdm.common.URI;
|
26
|
import eu.etaxonomy.cdm.common.UriUtils;
|
27
|
import eu.etaxonomy.cdm.common.media.CdmImageInfo;
|
28
|
|
29
|
/**
|
30
|
* Reads media metadata and file information from a
|
31
|
* <a href="https://github.com/cybertaxonomy/MediaInfoService">MediaInfoService</a>
|
32
|
*
|
33
|
* @author a.kohlbecker
|
34
|
* @since May 7, 2021
|
35
|
*/
|
36
|
public class MediaInfoServiceReader extends AbstactMediaMetadataReader {
|
37
|
|
38
|
private static final Logger logger = Logger.getLogger(MediaInfoServiceReader.class);
|
39
|
|
40
|
static private Mapper dozerMapper;
|
41
|
|
42
|
static {
|
43
|
// FIXME provide dozerMapper as bean via spring configuration
|
44
|
//dozerMapper = DozerBeanMapperBuilder.buildDefault();
|
45
|
dozerMapper = DozerBeanMapperBuilder.create().withMappingBuilder(new BeanMappingBuilder() {
|
46
|
|
47
|
@Override
|
48
|
protected void configure() {
|
49
|
mapping(type(MediaInfo.class).mapEmptyString(true),
|
50
|
type(CdmImageInfo.class),
|
51
|
TypeMappingOptions.wildcardCaseInsensitive(true)
|
52
|
).fields(
|
53
|
field("size"),
|
54
|
field("length"),
|
55
|
FieldsMappingOptions.oneWay()
|
56
|
|
57
|
).fields(
|
58
|
field("extension"),
|
59
|
field("suffix"),
|
60
|
FieldsMappingOptions.oneWay()
|
61
|
);
|
62
|
}
|
63
|
}).build();
|
64
|
|
65
|
}
|
66
|
protected MediaInfoServiceReader(URI imageUri, URI metadataUri) {
|
67
|
super(imageUri, metadataUri);
|
68
|
}
|
69
|
|
70
|
|
71
|
@Override
|
72
|
public AbstactMediaMetadataReader read() throws IOException, HttpException {
|
73
|
logger.info("reading metadata from " + metadataUri);
|
74
|
InputStream jsonStream = UriUtils.getInputStream(metadataUri);
|
75
|
ObjectMapper mapper = new ObjectMapper();
|
76
|
MediaInfo mediaInfo = mapper.readValue(jsonStream, MediaInfo.class);
|
77
|
dozerMapper.map(mediaInfo, getCdmImageInfo());
|
78
|
// TODO how to do this with Dozer?:
|
79
|
mediaInfo.getMetaData().entrySet().forEach(e -> processPutMetadataEntry(e.getKey(), e.getValue()));
|
80
|
return this;
|
81
|
}
|
82
|
|
83
|
}
|