Project

General

Profile

Download (5.57 KB) Statistics
| Branch: | Tag: | Revision:
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.commons.imaging.ImageInfo;
15
import org.apache.commons.imaging.ImageReadException;
16
import org.apache.commons.imaging.Imaging;
17
import org.apache.commons.imaging.common.GenericImageMetadata.GenericImageMetadataItem;
18
import org.apache.commons.imaging.common.ImageMetadata;
19
import org.apache.commons.imaging.common.ImageMetadata.ImageMetadataItem;
20
import org.apache.commons.lang3.StringUtils;
21
import org.apache.http.HttpException;
22
import org.apache.http.client.ClientProtocolException;
23
import org.apache.log4j.Logger;
24

    
25
import eu.etaxonomy.cdm.common.UriUtils;
26

    
27
/**
28
 * TODO make use of timeOut ?
29
 *
30
 * Most code  was extracted from CdmImageInfo.
31
 *
32
 */
33
public class MediaInfoFileReader extends AbstactMediaMetadataReader {
34

    
35
    private static Logger logger = Logger.getLogger(MediaInfoFileReader.class);
36

    
37
    public static final Integer IMAGE_READ_TIMEOUT = 3000; // ms
38

    
39
    private Integer timeOut = IMAGE_READ_TIMEOUT; // setting default
40

    
41
    /**
42
     * The <code>MediaMetadataFileReader</code> should not be used directly this method
43
     * only exists for not to break legacy code.
44
     * <p>
45
     * Instead the {@link IMediaInfoFactory} should always be used for to allow
46
     * all application parts to benefit from the potential speed up through the
47
     * MediaMetadataService or other fast source of metadata.
48
     */
49
    @Deprecated
50
    public static MediaInfoFileReader legacyFactoryMethod(eu.etaxonomy.cdm.common.URI uri) {
51
        return new MediaInfoFileReader(uri);
52
    }
53

    
54
    protected MediaInfoFileReader(eu.etaxonomy.cdm.common.URI uri) {
55
        super(uri, uri);
56
    }
57

    
58
    @Override
59
    public AbstactMediaMetadataReader read() throws IOException, HttpException {
60
        return readBaseInfo().readMetaData();
61
    }
62

    
63
    /**
64
     * Combines the calls to:
65
     *
66
     * <ul>
67
     * <li>{@link #readImageInfo()}</li>
68
     * <li>{@link #readImageLength()}</li>
69
     * <li>{@link #readSuffix()}</li>
70
     * </ul>
71
     * to read the commonly needed base information.
72
     *
73
     * @return
74
     * @throws IOException
75
     * @throws HttpException
76
     */
77
    public MediaInfoFileReader readBaseInfo() throws IOException, HttpException{
78
        readImageInfo();
79
        readImageLength();
80
        readSuffix();
81
        return this;
82
    }
83

    
84
    /**
85
     * Reads the image info (width, height, bitPerPixel, metadata, format, mime type)
86
     */
87
    public AbstactMediaMetadataReader readImageInfo() throws IOException, HttpException{
88

    
89
        InputStream inputStream;
90
        try {
91
            inputStream = UriUtils.getInputStream(cdmImageInfo.getUri());
92
            ImageInfo imageInfo = Imaging.getImageInfo(inputStream, null);
93

    
94
            cdmImageInfo.setFormatName(imageInfo.getFormatName());
95
            cdmImageInfo.setMimeType(imageInfo.getMimeType());
96
            cdmImageInfo.setWidth(imageInfo.getWidth());
97
            cdmImageInfo.setHeight(imageInfo.getHeight());
98
            cdmImageInfo.setBitPerPixel(imageInfo.getBitsPerPixel());
99
            inputStream.close();
100

    
101
        } catch (ImageReadException e) {
102
            logger.error("Could not read: " + cdmImageInfo.getUri() + ". " + e.getMessage());
103
            throw new IOException(e);
104
        }
105

    
106
        return this;
107
    }
108

    
109
    public AbstactMediaMetadataReader readMetaData() throws IOException, HttpException {
110

    
111
        ImageMetadata mediaData = null;
112
        try {
113
            InputStream inputStream = UriUtils.getInputStream(cdmImageInfo.getUri());
114
            mediaData = Imaging.getMetadata(inputStream, null);
115
        }catch (ImageReadException e) {
116
            logger.error("Could not read: " + cdmImageInfo.getUri() + ". " + e.getMessage());
117
            //throw new IOException(e);
118
        }
119

    
120
        if(mediaData != null) {
121
            for (ImageMetadataItem imItem : mediaData.getItems()){
122
                if (imItem instanceof GenericImageMetadataItem){
123
                    GenericImageMetadataItem item = (GenericImageMetadataItem)imItem;
124
                    processPutMetadataEntry(item.getKeyword(), item.getText());
125
                }
126
            }
127
        }
128

    
129
        return this;
130
    }
131

    
132
    /**
133
     * Reads the size of the image defined by the {@link #imageUri} in bytes
134
     */
135
    public AbstactMediaMetadataReader readImageLength() throws ClientProtocolException, IOException, HttpException{
136
        try {
137
            long length = UriUtils.getResourceLength(cdmImageInfo.getUri(), null);
138
            cdmImageInfo.setLength(length);
139
        } catch (HttpException e) {
140
            if (e.getMessage().equals("Could not retrieve Content-Length")){
141
                InputStream inputStream = UriUtils.getInputStream(cdmImageInfo.getUri());
142
                int n = 0;
143
                while(inputStream.read() != -1){
144
                    n++;
145
                }
146
                inputStream.close();
147
                logger.info("Content-Length not available in http header. Image size computed via input stream size: " + cdmImageInfo.getUri());
148
                cdmImageInfo.setLength(n);
149
            }else{
150
                throw e;
151
            }
152
        }
153
        return this;
154
    }
155

    
156
    public AbstactMediaMetadataReader readSuffix(){
157
        String path = cdmImageInfo.getUri().getPath();
158
        String suffix = path.substring(StringUtils.lastIndexOf(path, '.') + 1);
159
        cdmImageInfo.setSuffix(suffix);
160
        return this;
161
    }
162
}
(5-5/9)