This is an automated email from the ASF dual-hosted git repository.

bossenti pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/streampipes.git


The following commit(s) were added to refs/heads/dev by this push:
     new 21716820e feat: extend FileApi with the capability to query by the 
original file name (#2109)
21716820e is described below

commit 21716820e8d6eb25e99e4fc427bae1722765c4da
Author: Tim <[email protected]>
AuthorDate: Wed Nov 15 09:21:56 2023 +0100

    feat: extend FileApi with the capability to query by the original file name 
(#2109)
    
    * feat: extend FileApi with the capability to query by the original file 
name
    
    * style: remove unused import
    
    * style: fix checkstyle violations
    
    * refactor: simplify file API
    
    * style: fix wrong annotations
    
    ---------
    
    Co-authored-by: Dominik Riemer <[email protected]>
---
 .../org/apache/streampipes/client/api/FileApi.java | 19 ++++++++--
 .../CsvMetadataEnrichmentProcessor.java            |  4 +-
 .../streampipes/manager/file/FileManager.java      | 16 +++++++-
 .../streampipes/rest/impl/PipelineElementFile.java | 43 +++++++++++++++++++++-
 4 files changed, 73 insertions(+), 9 deletions(-)

diff --git 
a/streampipes-client/src/main/java/org/apache/streampipes/client/api/FileApi.java
 
b/streampipes-client/src/main/java/org/apache/streampipes/client/api/FileApi.java
index 7d83d59f1..b6e8dcbf7 100644
--- 
a/streampipes-client/src/main/java/org/apache/streampipes/client/api/FileApi.java
+++ 
b/streampipes-client/src/main/java/org/apache/streampipes/client/api/FileApi.java
@@ -22,6 +22,8 @@ import org.apache.streampipes.client.http.BinaryGetRequest;
 import org.apache.streampipes.client.model.StreamPipesClientConfig;
 import org.apache.streampipes.client.util.StreamPipesApiPath;
 
+import java.util.Map;
+
 
 public class FileApi extends AbstractClientApi implements IFileApi {
 
@@ -29,14 +31,23 @@ public class FileApi extends AbstractClientApi implements 
IFileApi {
     super(clientConfig);
   }
 
+  public byte[] getFileContent(String filename, boolean isOriginalFileName) {
+    return new BinaryGetRequest(clientConfig, getBaseResourcePath(filename)
+            .withQueryParameters(Map.of("isOriginalFilename", 
String.valueOf(isOriginalFileName))), null)
+            .executeRequest();
+  }
   @Override
-  public byte[] getFileContent(String filename) {
-    return new BinaryGetRequest(clientConfig, getBaseResourcePath(filename), 
null).executeRequest();
+  public byte[] getFileContent(String fileId) {
+    return getFileContent(fileId, false);
+  }
+
+  public String getFileContentAsString(String filename, boolean 
isOriginalFileName) {
+    return new String(getFileContent(filename, isOriginalFileName));
   }
 
   @Override
-  public String getFileContentAsString(String filename) {
-    return new String(getFileContent(filename));
+  public String getFileContentAsString(String fileId) {
+    return getFileContentAsString(fileId, false);
   }
 
   @Override
diff --git 
a/streampipes-extensions/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/csvmetadata/CsvMetadataEnrichmentProcessor.java
 
b/streampipes-extensions/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/csvmetadata/CsvMetadataEnrichmentProcessor.java
index 3bb61d265..be31a3473 100644
--- 
a/streampipes-extensions/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/csvmetadata/CsvMetadataEnrichmentProcessor.java
+++ 
b/streampipes-extensions/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/csvmetadata/CsvMetadataEnrichmentProcessor.java
@@ -153,7 +153,7 @@ public class CsvMetadataEnrichmentProcessor
     CSVParser parser = getCsvParser(fileContents);
     List<EventProperty> propertiesToAppend = new ArrayList<>();
     List<CSVRecord> records = parser.getRecords();
-    if (records.size() > 0) {
+    if (!records.isEmpty()) {
       CSVRecord firstRecord = records.get(0);
       for (String column : columnsToInclude) {
         propertiesToAppend.add(makeEventProperty(column, firstRecord));
@@ -208,7 +208,7 @@ public class CsvMetadataEnrichmentProcessor
     } catch (IOException e) {
       throw new SpRuntimeException(e);
     }
-    if (this.columnMap.size() > 0) {
+    if (!this.columnMap.isEmpty()) {
       this.columnsToAppend = fieldsToAppend
           .stream()
           .map(c -> makeParser(c, 
this.columnMap.entrySet().stream().findFirst().get().getValue()))
diff --git 
a/streampipes-pipeline-management/src/main/java/org/apache/streampipes/manager/file/FileManager.java
 
b/streampipes-pipeline-management/src/main/java/org/apache/streampipes/manager/file/FileManager.java
index 6ba0f85d1..fa860de9f 100644
--- 
a/streampipes-pipeline-management/src/main/java/org/apache/streampipes/manager/file/FileManager.java
+++ 
b/streampipes-pipeline-management/src/main/java/org/apache/streampipes/manager/file/FileManager.java
@@ -43,6 +43,20 @@ public class FileManager {
     return filetypes != null ? filterFiletypes(allFiles, filetypes) : allFiles;
   }
 
+  public static File getFileByOriginalName(String originalName) throws 
IOException {
+    List<FileMetadata> allFiles = 
getFileMetadataStorage().getAllFileMetadataDescriptions();
+
+    var file = allFiles
+            .stream()
+            .filter(fileMetadata -> 
fileMetadata.getOriginalFilename().equals(originalName))
+            .findFirst();
+
+    if (file.isEmpty()){
+      throw new IOException("No file with original name '%s' 
found".formatted(originalName));
+    }
+    return new FileHandler().getFile(file.get().getInternalFilename());
+  }
+
   /**
    * Store a file in the internal file storage.
    * For csv files the bom is removed
@@ -119,7 +133,7 @@ public class FileManager {
   }
 
   private static String makeInternalFilename(String filetype) {
-    return UUID.randomUUID().toString() + "." + filetype;
+    return UUID.randomUUID() + "." + filetype;
   }
 
   private static List<FileMetadata> filterFiletypes(List<FileMetadata> 
allFiles, String filetypes) {
diff --git 
a/streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/PipelineElementFile.java
 
b/streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/PipelineElementFile.java
index cf799a441..2c74d9404 100644
--- 
a/streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/PipelineElementFile.java
+++ 
b/streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/PipelineElementFile.java
@@ -22,6 +22,11 @@ import org.apache.streampipes.model.file.FileMetadata;
 import 
org.apache.streampipes.rest.core.base.impl.AbstractAuthGuardedRestResource;
 import org.apache.streampipes.rest.security.AuthConstants;
 
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.Parameter;
+import io.swagger.v3.oas.annotations.enums.ParameterIn;
+import io.swagger.v3.oas.annotations.responses.ApiResponse;
+import org.apache.http.HttpStatus;
 import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
 import org.glassfish.jersey.media.multipart.FormDataParam;
 import org.springframework.security.access.prepost.PreAuthorize;
@@ -29,6 +34,7 @@ import org.springframework.stereotype.Component;
 
 import jakarta.ws.rs.Consumes;
 import jakarta.ws.rs.DELETE;
+import jakarta.ws.rs.DefaultValue;
 import jakarta.ws.rs.GET;
 import jakarta.ws.rs.POST;
 import jakarta.ws.rs.Path;
@@ -38,6 +44,7 @@ import jakarta.ws.rs.QueryParam;
 import jakarta.ws.rs.core.MediaType;
 import jakarta.ws.rs.core.Response;
 
+import java.io.IOException;
 import java.io.InputStream;
 
 @Path("/v2/files")
@@ -76,8 +83,40 @@ public class PipelineElementFile extends 
AbstractAuthGuardedRestResource {
   @GET
   @Path("/{filename}")
   @Produces(MediaType.APPLICATION_OCTET_STREAM)
-  public Response getFile(@PathParam("filename") String filename) {
+  @Operation(
+          summary = "Get file content by file name."
+                  + "If multiple files with the same name exist, only the 
first is returned."
+                  + "This can only be the case when the original file name is 
provided.", tags = {"Core", "Files"},
+          responses = {
+              @ApiResponse(
+                      responseCode = "" + HttpStatus.SC_OK,
+                      description = "File could be found and is returned"),
+              @ApiResponse(
+                      responseCode = "" + HttpStatus.SC_NOT_FOUND,
+                      description = "No file with the given file name could be 
found")
+          }
+  )
+  public Response getFile(
+          @Parameter(
+                  in = ParameterIn.PATH,
+                  description = "The name of the file to be retrieved",
+                  required = true
+          )
+          @PathParam("filename") String filename,
+          @Parameter(
+                  in = ParameterIn.QUERY,
+                  description = "Determines if the provided file name is the 
original file name "
+                          + "as per upload."
+          )
+          @QueryParam("isOriginalFilename") @DefaultValue("false") boolean 
isOriginalFilename
+  ) {
+    if (isOriginalFilename) {
+      try {
+        return ok(FileManager.getFileByOriginalName(filename));
+      } catch (IOException e) {
+        return notFound(filename);
+      }
+    }
     return ok(FileManager.getFile(filename));
   }
-
 }

Reply via email to