Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extend FileApi with the capability to query by the original file name #2109

Merged
merged 7 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,32 @@
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 {

public FileApi(StreamPipesClientConfig clientConfig) {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private List<EventProperty> getAppendProperties(String fileContents,
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));
Expand Down Expand Up @@ -208,7 +208,7 @@ public void onInvocation(ProcessorParams parameters,
} 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()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ public static List<FileMetadata> getAllFiles(String filetypes) {
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
Expand Down Expand Up @@ -119,7 +133,7 @@ private static FileMetadata makeFileMetadata(String user,
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@
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;
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;
Expand All @@ -38,6 +44,7 @@
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

import java.io.IOException;
import java.io.InputStream;

@Path("/v2/files")
Expand Down Expand Up @@ -76,8 +83,40 @@ public Response getFileInfo(@QueryParam("filetypes") String filetypes) {
@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));
}

}
Loading