Skip to content

Commit 663078e

Browse files
committed
[feature] save to one temp file and use uuid for file names
1 parent 483164c commit 663078e

3 files changed

Lines changed: 37 additions & 48 deletions

File tree

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@
104104
<artifactId>csv-validator-java-api</artifactId>
105105
<version>1.4.1</version>
106106
</dependency>
107+
108+
<!-- https://mvnrepository.com/artifact/com.fasterxml.uuid/java-uuid-generator -->
109+
<dependency>
110+
<groupId>com.fasterxml.uuid</groupId>
111+
<artifactId>java-uuid-generator</artifactId>
112+
<version>5.2.0</version>
113+
</dependency>
107114
</dependencies>
108115

109116
<build>

src/main/java/com/evolvedbinary/bblValidator/controller/ValidateController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public ValidationResponse validateForm(@Body ValidationForm form) {
5959
public ValidationResponse validateCsv(@QueryValue("schema-id") String schemaId,
6060
@Body String csvContent) {
6161
try {
62-
Path tempFile = fileDownloadService.saveContentToTemp(csvContent, "uploaded-content.csv");
62+
Path tempFile = fileDownloadService.saveContentToTemp(csvContent);
6363
LOG.info("CSV content saved to: {}", tempFile);
6464
return performValidation(tempFile, schemaId);
6565
} catch (IOException e) {
Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.evolvedbinary.bblValidator.service;
22

3+
import com.fasterxml.uuid.Generators;
34
import jakarta.inject.Singleton;
45
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
@@ -13,34 +14,45 @@
1314
import java.nio.file.Files;
1415
import java.nio.file.Path;
1516
import java.nio.file.StandardCopyOption;
17+
import java.util.UUID;
1618

1719
@Singleton
1820
public class FileDownloadService {
1921

2022
private static final Logger LOG = LoggerFactory.getLogger(FileDownloadService.class);
21-
private static final String TEMP_DIR_PREFIX = "bbl-validator-";
22-
private static final String DEFAULT_FILENAME = "downloaded-file.csv";
23+
private static final String TEMP_DIR_NAME = "bbl-validator";
2324
private final HttpClient httpClient;
25+
private final Path sharedTempDir;
2426

2527
public FileDownloadService() {
2628
this.httpClient = HttpClient.newBuilder()
2729
.followRedirects(HttpClient.Redirect.NORMAL)
2830
.build();
31+
try {
32+
Path systemTempDir = Path.of(System.getProperty("java.io.tmpdir"));
33+
this.sharedTempDir = systemTempDir.resolve(TEMP_DIR_NAME);
34+
if (!Files.exists(sharedTempDir)) {
35+
Files.createDirectories(sharedTempDir);
36+
LOG.info("Created persistent temp directory: {}", sharedTempDir);
37+
} else {
38+
LOG.info("Using existing persistent temp directory: {}", sharedTempDir);
39+
}
40+
} catch (IOException e) {
41+
throw new RuntimeException("Failed to create or access persistent temp directory", e);
42+
}
2943
}
3044

3145
/**
32-
* Downloads a file from the given URL and stores it in a temporary directory.
46+
* Downloads a file from the given URL and stores it with a UUID v4 filename.
3347
*
3448
* @param url The URL to download from
35-
* @return Path to the downloaded file in the temp directory
49+
* @return Path to the downloaded file in the shared temp directory
3650
* @throws IOException if download or file operations fail
37-
* @throws IllegalArgumentException if url is null or empty
3851
*/
3952
public Path downloadToTemp(String url) throws IOException {
4053
try {
41-
Path tempDir = createTempDirectory();
42-
String filename = extractFilename(url);
43-
Path tempFile = tempDir.resolve(filename);
54+
String filename = generateUuidFilename();
55+
Path tempFile = sharedTempDir.resolve(filename);
4456

4557
HttpRequest request = HttpRequest.newBuilder()
4658
.uri(URI.create(url))
@@ -66,17 +78,15 @@ public Path downloadToTemp(String url) throws IOException {
6678
}
6779

6880
/**
69-
* Saves content to a temporary file.
81+
* Saves content to a temporary file with a UUID v4 filename.
7082
*
7183
* @param content The content to save
72-
* @param filename The filename to use
7384
* @return Path to the created temp file
7485
* @throws IOException if file operations fail
75-
* @throws IllegalArgumentException if content or filename is null or empty
7686
*/
77-
public Path saveContentToTemp(String content, String filename) throws IOException {
78-
Path tempDir = createTempDirectory();
79-
Path tempFile = tempDir.resolve(filename);
87+
public Path saveContentToTemp(String content) throws IOException {
88+
String uuidFilename = generateUuidFilename();
89+
Path tempFile = sharedTempDir.resolve(uuidFilename);
8090

8191
Files.writeString(tempFile, content);
8292

@@ -85,40 +95,12 @@ public Path saveContentToTemp(String content, String filename) throws IOExceptio
8595
}
8696

8797
/**
88-
* Creates a temporary directory with the standard prefix.
89-
*
90-
* @return Path to the created temp directory
91-
* @throws IOException if directory creation fails
92-
*/
93-
private Path createTempDirectory() throws IOException {
94-
return Files.createTempDirectory(TEMP_DIR_PREFIX);
95-
}
96-
97-
98-
/**
99-
* Extracts filename from URL or returns a default name.
98+
* Generates a UUID v4 filename.
10099
*
101-
* @param url The URL to extract filename from
102-
* @return The extracted filename or default filename
100+
* @return A UUID v4 string to be used as filename
103101
*/
104-
private String extractFilename(String url) {
105-
try {
106-
String path = URI.create(url).getPath();
107-
if (path == null || path.isEmpty()) {
108-
return DEFAULT_FILENAME;
109-
}
110-
111-
int lastSlash = path.lastIndexOf('/');
112-
if (lastSlash >= 0 && lastSlash < path.length() - 1) {
113-
String filename = path.substring(lastSlash + 1);
114-
// Ensure filename is not empty after extraction
115-
if (!filename.trim().isEmpty()) {
116-
return filename;
117-
}
118-
}
119-
} catch (Exception e) {
120-
LOG.warn("Could not extract filename from URL: {}", url, e);
121-
}
122-
return DEFAULT_FILENAME;
102+
private String generateUuidFilename() {
103+
UUID uuid = Generators.randomBasedGenerator().generate();
104+
return uuid.toString();
123105
}
124106
}

0 commit comments

Comments
 (0)