Fix bug and security hotspot in ChatImageServiceImpl
@@ -2,7 +2,6 @@ package greencity.service.impl;
import greencity.service.ChatFileService;
import org.apache.commons.io.FileUtils;
-import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@@ -30,13 +29,14 @@ public class ChatImageServiceImpl implements ChatFileService {
@Override
public byte[] getByteArrayFromFile(String fileName) throws IOException {
File file = new File(fileFolder + fileName);
- FileInputStream inputStream = new FileInputStream(file);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ try (FileInputStream inputStream = new FileInputStream(file)) {
int read;
byte[] data = new byte[300000];
while ((read = inputStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, read);
}
+ }
return buffer.toByteArray();
}
@@ -44,6 +44,6 @@ public class ChatImageServiceImpl implements ChatFileService {
public String getUniqueName() {
String ext = "png";
SecureRandom secureRandom = new SecureRandom();
- return String.format("%s%s.%s", secureRandom.nextInt(), RandomStringUtils.randomAlphanumeric(8), ext);
+ return String.format("%s-%s.%s", secureRandom.nextInt(), secureRandom.nextInt(), ext);
}
}
