11package com .evolvedbinary .bblValidator .service ;
22
3+ import com .fasterxml .uuid .Generators ;
34import jakarta .inject .Singleton ;
45import org .slf4j .Logger ;
56import org .slf4j .LoggerFactory ;
1314import java .nio .file .Files ;
1415import java .nio .file .Path ;
1516import java .nio .file .StandardCopyOption ;
17+ import java .util .UUID ;
1618
1719@ Singleton
1820public 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