Skip to content

Commit

Permalink
Fix FileUtilities sonar issue
Browse files Browse the repository at this point in the history
  • Loading branch information
EmanueleValentini1 committed Sep 25, 2024
1 parent dff7587 commit e7891af
Showing 1 changed file with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,25 +103,40 @@ public static String toHexString(byte[] hash) {

public static File fromStringToFile(String fileBase64) {
try {

byte[] decodedBytes = Base64.getDecoder().decode(fileBase64);

Path tempDir = Files.createTempDirectory("multipleUpload");
String secureDirPath = System.getProperty("java.io.tmpdir") + "/mySecureDirectory";
File secureDir = new File(secureDirPath);
if (!secureDir.exists() && !secureDir.mkdirs()) {
throw new IOException("Impossibile creare la directory sicura.");
}

Path tempFilePath;

if (SystemUtils.IS_OS_UNIX) {
Set<PosixFilePermission> permissions = EnumSet.of(
Set<PosixFilePermission> dirPermissions = EnumSet.of(
PosixFilePermission.OWNER_READ,
PosixFilePermission.OWNER_WRITE,
PosixFilePermission.OWNER_EXECUTE
);
java.nio.file.Files.setPosixFilePermissions(secureDir.toPath(), dirPermissions);
}

File tempFile;

if (SystemUtils.IS_OS_UNIX) {

Set<PosixFilePermission> filePermissions = EnumSet.of(
PosixFilePermission.OWNER_READ,
PosixFilePermission.OWNER_WRITE,
PosixFilePermission.OWNER_EXECUTE
);
FileAttribute<Set<PosixFilePermission>> fileAttributes = PosixFilePermissions.asFileAttribute(permissions);

tempFilePath = Files.createTempFile(tempDir, "tempfile", ".tmp", fileAttributes);
tempFile = File.createTempFile("tempfile", ".tmp", secureDir);
java.nio.file.Files.setPosixFilePermissions(tempFile.toPath(), filePermissions);
} else {
tempFilePath = Files.createTempFile(tempDir, "tempfile", ".tmp");

File tempFile = tempFilePath.toFile();
tempFile = File.createTempFile("tempfile", ".tmp", secureDir);

boolean readable = tempFile.setReadable(true, true);
boolean writable = tempFile.setWritable(true, true);
Expand All @@ -132,11 +147,11 @@ public static File fromStringToFile(String fileBase64) {
}
}

try (FileOutputStream fos = new FileOutputStream(tempFilePath.toFile())) {
try (FileOutputStream fos = new FileOutputStream(tempFile)) {
fos.write(decodedBytes);
}

return tempFilePath.toFile();
return tempFile;
} catch (IllegalArgumentException e) {
log.error("Errore nella decodifica del Base64: " + e.getMessage());
throw new AtmLayerException("Errore nella decodifica del File Base64", Response.Status.NOT_ACCEPTABLE, AppErrorCodeEnum.FILE_DECODE_ERROR);
Expand Down

0 comments on commit e7891af

Please sign in to comment.