• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmrs / openmrs-core / 13853363634

14 Mar 2025 09:10AM UTC coverage: 65.081% (+0.1%) from 64.959%
13853363634

push

github

web-flow
TRUNK-6304 Refactor code to use Storage Service (#4944)

* TRUNK-6300 Add Storage Service

* TRUNK-6304 Refactor code to use Storage Service

319 of 401 new or added lines in 13 files covered. (79.55%)

17 existing lines in 4 files now uncovered.

23407 of 35966 relevant lines covered (65.08%)

0.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.85
/api/src/main/java/org/openmrs/obs/handler/MediaHandler.java
1
/**
2
 * This Source Code Form is subject to the terms of the Mozilla Public License,
3
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
4
 * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5
 * the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6
 *
7
 * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8
 * graphic logo is a trademark of OpenMRS Inc.
9
 */
10
package org.openmrs.obs.handler;
11

12
import javax.activation.MimetypesFileTypeMap;
13
import java.io.IOException;
14
import java.io.InputStream;
15

16
import org.apache.commons.io.IOUtils;
17
import org.openmrs.Obs;
18
import org.openmrs.api.APIException;
19
import org.openmrs.api.storage.ObjectMetadata;
20
import org.openmrs.obs.ComplexData;
21
import org.openmrs.obs.ComplexObsHandler;
22
import org.slf4j.Logger;
23
import org.slf4j.LoggerFactory;
24
import org.springframework.stereotype.Component;
25

26
/**
27
 * Handler for storing audio and video for complex obs to the file system. The mime type used is
28
 * probed from the file if possible. Media are stored in the location specified by the global
29
 * property: "obs.complex_obs_dir"
30
 *
31
 * @see org.openmrs.util.OpenmrsConstants#GLOBAL_PROPERTY_COMPLEX_OBS_DIR
32
 * @since 1.12
33
 */
34
@Component
35
public class MediaHandler extends AbstractHandler implements ComplexObsHandler {
36
        
37
        /** Views supported by this handler */
38
        private static final String[] supportedViews = { ComplexObsHandler.RAW_VIEW, };
1✔
39
        
40
        private static final Logger log = LoggerFactory.getLogger(MediaHandler.class);
1✔
41

42
        private final MimetypesFileTypeMap mimetypes = new MimetypesFileTypeMap();
1✔
43
        
44
        public MediaHandler() {
45
                super();
1✔
46
        }
1✔
47
        
48
        /**
49
         * Currently supports all views and puts the media file data into the ComplexData object
50
         *
51
         * @see org.openmrs.obs.ComplexObsHandler#getObs(org.openmrs.Obs, java.lang.String)ยง
52
         */
53
        @Override
54
        public Obs getObs(Obs obs, String view) {
55
                String key = parseDataKey(obs);
1✔
56
                
57
                // Raw media
58
                if (ComplexObsHandler.RAW_VIEW.equals(view)) {
1✔
59
                        try {
60
                                String[] names = obs.getValueComplex().split("\\|");
1✔
61
                                String originalFilename = names[0];
1✔
62
                                originalFilename = originalFilename.replace(",", "")
1✔
63
                                        .replace(" ", "");
1✔
64

65
                                InputStream in = storageService.getData(key);
1✔
66
                                ComplexData complexData = new ComplexData(originalFilename, in);
1✔
67
                                
68
                                complexData.setMimeType(mimetypes.getContentType(originalFilename));
1✔
69
                                
70
                                // Get the Mime Type and set it
71
                                injectMissingMetadata(key, complexData);
1✔
72
                                obs.setComplexData(complexData);
1✔
73
                        }
NEW
74
                        catch (IOException e) {
×
NEW
75
                                log.error("Trying to create media file stream from {}", key, e);
×
76
                        }
1✔
77
                }
78
                // No other view supported
79
                // NOTE: if adding support for another view, don't forget to update supportedViews list above
80
                else {
81
                        return null;
×
82
                }
83
                
84
                return obs;
1✔
85
        }
86
        
87
        /**
88
         * @see org.openmrs.obs.ComplexObsHandler#getSupportedViews()
89
         */
90
        @Override
91
        public String[] getSupportedViews() {
92
                return supportedViews;
1✔
93
        }
94
        
95
        /**
96
         * @see org.openmrs.obs.ComplexObsHandler#saveObs(org.openmrs.Obs)
97
         */
98
        @Override
99
        public Obs saveObs(Obs obs) throws APIException {
100
                try {
101
                        String filename = obs.getComplexData().getTitle();
1✔
102
                        String key = storageService.saveData(outputStream -> {
1✔
103
                                IOUtils.copy((InputStream) obs.getComplexData().getData(), outputStream);
1✔
104
                                outputStream.flush();
1✔
105
                        }, ObjectMetadata.builder().setFilename(filename).build(), getObsDir());
1✔
106
                        
107
                        obs.setComplexData(null);
1✔
108
                        obs.setValueComplex(filename + "|" + key);
1✔
109
                }
110
                catch (IOException ioe) {
×
111
                        throw new APIException("Obs.error.trying.write.complex", null, ioe);
×
112
                }
1✔
113
                
114
                return obs;
1✔
115
        }
116
        
117
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc