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

openmrs / openmrs-core / 14617411652

23 Apr 2025 11:50AM UTC coverage: 63.801% (+0.1%) from 63.69%
14617411652

push

github

rkorytkowski
TRUNK-6300 Add Storage Service

(cherry picked from commit 8a14c70a3)

137 of 227 new or added lines in 6 files covered. (60.35%)

1 existing line in 1 file now uncovered.

22160 of 34733 relevant lines covered (63.8%)

0.64 hits per line

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

87.5
/api/src/main/java/org/openmrs/api/storage/BaseStorageService.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.api.storage;
11

12
import java.io.IOException;
13
import java.io.InputStream;
14
import java.nio.file.Files;
15
import java.nio.file.Path;
16
import java.nio.file.Paths;
17
import java.nio.file.StandardCopyOption;
18

19
import org.openmrs.api.StorageService;
20
import org.openmrs.api.impl.BaseOpenmrsService;
21
import org.openmrs.api.stream.StreamDataService;
22
import org.openmrs.api.stream.StreamDataWriter;
23
import org.springframework.beans.factory.annotation.Autowired;
24

25
/**
26
 * Implements temporary storage.
27
 * 
28
 * @since 2.8.0, 2.7.4, 2.6.16, 2.5.15
29
 */
30
public abstract class BaseStorageService extends BaseOpenmrsService implements StorageService {
31
        private final StreamDataService streamService;
32
        
33
        private final Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));
1✔
34
        
35
        public BaseStorageService(@Autowired StreamDataService streamService) {
1✔
36
                this.streamService = streamService;
1✔
37
        }
1✔
38

39
        @Override
40
        public InputStream getTempData(String key) throws IOException {
41
                Path tempFile = tempDir.resolve(key);
1✔
42
                if (!Files.exists(tempFile)) {
1✔
NEW
43
                        throw new IOException("Temp file does not exist: " + key);
×
44
                }
45
                return Files.newInputStream(tempFile);
1✔
46
        }
47

48
        @Override
49
        public String saveData(InputStream inputStream, ObjectMetadata metadata, String moduleIdOrGroup) 
50
                throws IOException {
51
                return saveData(inputStream, metadata, moduleIdOrGroup, null);
1✔
52
        }
53
        
54

55
        public String saveData(StreamDataWriter dataWriter, ObjectMetadata metadata, String moduleIdOrGroup) 
56
                throws IOException {
57
                return saveData(dataWriter, metadata, moduleIdOrGroup, null);
1✔
58
        }
59

60
        public String saveData(StreamDataWriter dataWriter, ObjectMetadata metadata, String moduleIdOrGroup,
61
                                                   String keySuffix) throws IOException {
62
                return saveData(streamService.streamData(dataWriter, metadata != null ? metadata.getLength() : null), metadata, 
1✔
63
                        moduleIdOrGroup, keySuffix);
64
        }
65

66
        public String saveTempData(InputStream inputStream, ObjectMetadata metadata) throws IOException {
67
                Path tempFile = Files.createTempFile("openmrs-temp", ".tmp");
1✔
68
                Files.copy(inputStream, tempFile, StandardCopyOption.REPLACE_EXISTING);
1✔
69
                tempFile.toFile().deleteOnExit();
1✔
70
                return tempFile.getFileName().toString();
1✔
71
        }
72

73
        public String saveTempData(StreamDataWriter writer, ObjectMetadata metadata) throws IOException {
NEW
74
                return saveTempData(streamService.streamData(writer, metadata != null ? metadata.getLength() : null), metadata);
×
75
        }
76
}
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