Skip to content

Commit

Permalink
[P4ADEV-1830] fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mscarsel committed Jan 14, 2025
1 parent a3f9666 commit 08d5f09
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ public class FileUploadException extends RuntimeException {
public FileUploadException(String message) {
super(message);
}

public FileUploadException(String message, Throwable e) {
super(message,e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,27 @@ public String saveToSharedFolder(MultipartFile file, String relativePath){
String filename = org.springframework.util.StringUtils.cleanPath(
StringUtils.defaultString(file.getOriginalFilename()));
FileService.validateFilename(filename);
Path fileLocation = concatenatePaths(relativePath,filename);
Path absolutePath = concatenatePaths(foldersPathsConfig.getShared(), fileLocation.toString());
Path relativeFileLocation = concatenatePaths(relativePath,filename);
Path absolutePath = concatenatePaths(foldersPathsConfig.getShared(), relativeFileLocation.toString());
//create missing parent folder, if any
try {
if (!Files.exists(absolutePath.getParent())){
Files.createDirectories(absolutePath.getParent());
}
encryptAndSaveFile(file, absolutePath);
}catch (Exception e) {
String errorMessage = "Error uploading file to shared folder %s".formatted(
relativePath);
log.debug(
errorMessage, e);
throw new FileUploadException(
errorMessage);
"Error uploading file to shared folder %s".formatted(relativePath)
,e);
}
log.debug("File upload to shared folder {} completed",relativePath);
return fileLocation.toString();
return relativeFileLocation.toString();
}

/**
* This method expects two paths whose concatenation does not resolve into an outer folder.
* The normalized path still starts with the first path.
* */
private Path concatenatePaths(String firstPath, String secondPath) {
Path concatenatedPath = Paths.get(firstPath, secondPath).normalize();
if(!concatenatedPath.startsWith(firstPath)){
Expand Down

0 comments on commit 08d5f09

Please sign in to comment.