Skip to content

Commit

Permalink
AddTypeFolder uses Paths and getFile simplified
Browse files Browse the repository at this point in the history
  • Loading branch information
gr812b committed May 13, 2024
1 parent 6157045 commit 4df5cc8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
9 changes: 4 additions & 5 deletions backend/src/main/java/com/mcmasterbaja/FileFetchResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,13 @@ public List<String> getAllFiles() {
// TODO: What exception is thrown when it can't find the file?
@GET
@jakarta.ws.rs.Path("/{filekey}")
public RestResponse<File> getFile(@PathParam("filekey") String filekey) {
public File getFile(@PathParam("filekey") String filekey) {
logger.info("Getting file: " + filekey);

Path targetPath = addTypeFolder(filekey);
File file = storageService.load(targetPath).toFile();

return ResponseBuilder.ok(file, MediaType.APPLICATION_OCTET_STREAM)
.header("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"")
.build();
return file;
}

@GET
Expand Down Expand Up @@ -160,6 +158,7 @@ public List<FileTimespan> getTimespan(@PathParam("folderkey") String folderkey)
}

private Path addTypeFolder(String fileKey) {
return Paths.get(fileMetadataService.getTypeFolder(fileKey)).resolve(fileKey);
String typeFolder = fileMetadataService.getTypeFolder(Paths.get(fileKey));
return storageService.load(Paths.get(typeFolder)).resolve(fileKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public boolean canComputeTimespan(Path folderPath) {
}

public LocalDateTime[] getTimespan(Path targetPath, LocalDateTime zeroTime) {
switch (getTypeFolder(targetPath.toString())) {
switch (getTypeFolder(targetPath)) {
case "csv":
return getTimespanCSV(targetPath, zeroTime);
case "mp4":
Expand Down Expand Up @@ -172,7 +172,8 @@ public LocalDateTime getZeroTime(Path folderPath) {
}
}

public String getTypeFolder(String pathString) {
public String getTypeFolder(Path targetPath) {
String pathString = targetPath.toString();
int dotIndex = pathString.lastIndexOf(".");
if (pathString == "" || pathString == null || dotIndex == -1) return ""; // No file extension

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@ public interface FileMetadataService {
* @param pathString The Path of the file to analyze.
* @return The desired type folder.
*/
String getTypeFolder(String pathString);
String getTypeFolder(Path targetPath);
}

0 comments on commit 4df5cc8

Please sign in to comment.