390da2b3dd2f46a8b08e7d95dae97cace648709c
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / media / MediaInfoFactory.java
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.util.ArrayList;
13 import java.util.List;
14
15 import org.apache.http.HttpException;
16 import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
17 import org.springframework.stereotype.Component;
18
19 import eu.etaxonomy.cdm.common.URI;
20 import eu.etaxonomy.cdm.common.media.CdmImageInfo;
21
22 /**
23 * @author a.kohlbecker
24 * @since May 5, 2021
25 */
26 @Component
27 public class MediaInfoFactory implements IMediaInfoFactory {
28
29 private static final Logger logger = LogManager.getLogger(MediaInfoFactory.class);
30
31 /**
32 * TODO needs to be managed in CDM PREFERENCES per service and version.
33 *
34 * ./MediaInfoService/1.0/ --> MediaUriTransformation
35 */
36 private List<MediaUriTransformation> mediaInfoService_1_0_Transformations = new ArrayList<>();
37
38 public MediaInfoFactory() {
39 mediaInfoService_1_0_Transformations.addAll(DefaultMediaTransformations.bgbmMediaMetadataService());
40 }
41
42 /**
43 * This method only exists due to performance issues for cases when
44 * the {@link MediaInfoFileReader} to reduce the overhead imposed by reading
45 * the image metadata from the file itself.
46 */
47 @Override
48 public CdmImageInfo cdmImageInfo(URI imageUri, boolean forceMetaData) throws IOException, HttpException {
49
50 List<URI> metadataServiceURIs = applyURITransformations(imageUri);
51 if(!metadataServiceURIs.isEmpty()) {
52 // :-) Hooray, we can get the metadata from the web service, this is going to be snappy
53 try {
54 return new MediaInfoServiceReader(imageUri, metadataServiceURIs.get(0))
55 .read()
56 .getCdmImageInfo();
57 } catch (Exception e) {
58 logger.warn("Meta data could not be read from meta data service ("+e.getMessage()+"): " + imageUri.toString());
59 //if an exception occurs read data from original file
60 }
61 }
62
63 // :-( need to use the files reader
64 MediaInfoFileReader mediaReader = new MediaInfoFileReader(imageUri).readBaseInfo();
65 AbstactMediaMetadataReader reader = forceMetaData ? mediaReader.readMetaData() : mediaReader;
66 return reader.getCdmImageInfo();
67
68 }
69
70 protected List<URI> applyURITransformations(URI imageUri) {
71 MediaUriTransformationProcessor processor = new MediaUriTransformationProcessor();
72 processor.addAll(mediaInfoService_1_0_Transformations);
73 List<URI> metadataServiceURIs = processor.applyTo(imageUri);
74 return metadataServiceURIs;
75 }
76 }