Skip to content

Commit 97c8d99

Browse files
committed
Sonar findings
1 parent 1842ebe commit 97c8d99

6 files changed

Lines changed: 24 additions & 17 deletions

File tree

server/src/main/java/dev/findfirst/core/config/FileSizeValidator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ public class FileSizeValidator implements ConstraintValidator<FileSize, Multipar
1313
private int maxFileSize;
1414

1515
@Override
16-
public void initialize(FileSize constraintAnnotation) {}
16+
public void initialize(FileSize constraintAnnotation) {
17+
// no op - need to Override ConstraintValidator
18+
}
1719

1820
@Override
1921
public boolean isValid(MultipartFile file, ConstraintValidatorContext context) {

server/src/main/java/dev/findfirst/core/service/TypesenseService.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public class TypesenseService {
3131

3232
private final Client client;
3333

34+
private final String schemaName = "bookmark";
35+
3436
@PostConstruct
3537
public String createSchema() {
3638
var q = initRepo.findByScriptName("init");
@@ -45,7 +47,7 @@ public String createSchema() {
4547

4648
private CollectionSchema createCollectionSchemaSchema() {
4749
CollectionSchema collectionSchema = new CollectionSchema();
48-
collectionSchema.name("bookmark")
50+
collectionSchema.name(schemaName)
4951
.addFieldsItem(new Field().name("title").type(FieldTypes.STRING))
5052
.addFieldsItem(new Field().name("text").type(FieldTypes.STRING));
5153
return collectionSchema;
@@ -78,7 +80,7 @@ public void addText(BookmarkJDBC bookmark, Document retDoc) {
7880
document.put("text", retDoc.text());
7981

8082
try {
81-
client.collections("bookmark").documents().create(document);
83+
client.collections(schemaName).documents().create(document);
8284
} catch (Exception e) {
8385
log.error(e.toString());
8486
}
@@ -88,9 +90,8 @@ public List<Long> search(String text) {
8890
SearchParameters searchParameters = new SearchParameters().q(text).queryBy("text");
8991
try {
9092
log.debug("searching");
91-
// log.debug(client.collections("bookmark").documents().;
9293
SearchResult searchResult =
93-
client.collections("bookmark").documents().search(searchParameters);
94+
client.collections(schemaName).documents().search(searchParameters);
9495
log.debug(searchResult.toString());
9596

9697
return searchResult.getHits().stream()

server/src/main/java/dev/findfirst/security/util/KeyGenerator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public static void generateKeys(String privateKeyPath, String publicKeyPath)
3535
PrivateKey privateKey = pair.getPrivate();
3636
PublicKey publicKey = pair.getPublic();
3737

38-
// Encoder encoder = Base64.getEncoder();
3938
Base64.Encoder encoder = Base64.getEncoder();
4039

4140
// Save the public key in PEM format
@@ -71,7 +70,7 @@ public static void main(String[] args) {
7170
generateKeys(privateKeyPath, publicKeyPath);
7271
logger.info("Keys Generated");
7372
} catch (NoSuchAlgorithmException | IOException e) {
74-
e.printStackTrace();
73+
logger.error(e.getMessage());
7574
}
7675
} else {
7776
logger.info("Keys already exist.");

server/src/main/java/dev/findfirst/users/controller/UserController.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,7 @@ public ResponseEntity<String> uploadProfilePicture(
184184
}
185185

186186
try {
187-
User user =
188-
userService.getUserById(uContext.getUserId()).orElseThrow(NoUserFoundException::new);
187+
User user = userService.getUserById(uContext.getUserId()).orElseThrow(NoUserFoundException::new);
189188
userService.changeUserPhoto(user, file);
190189

191190
return ResponseEntity.ok("File uploaded successfully.");
@@ -200,13 +199,12 @@ public ResponseEntity<String> uploadProfilePicture(
200199
@GetMapping("/profile-picture")
201200
public ResponseEntity<Resource> getUserProfilePicture(@RequestParam("userId") int userId) {
202201
try {
203-
User user;
204-
try {
205-
user = userService.getUserById(userId).orElseThrow(() -> new NoUserFoundException());
206-
} catch (NoUserFoundException e) {
202+
var opt = userService.getUserById(userId);
203+
if (opt.isEmpty()) {
207204
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
208-
}
209-
String userPhotoPath = user.getUserPhoto();
205+
}
206+
207+
String userPhotoPath = opt.get().getUserPhoto();
210208

211209
if (userPhotoPath == null || userPhotoPath.isEmpty()) {
212210
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);

server/src/main/java/dev/findfirst/users/service/UserManagementService.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.File;
44
import java.io.IOException;
55
import java.nio.charset.StandardCharsets;
6+
import java.nio.file.Files;
67
import java.nio.file.Path;
78
import java.rmi.UnexpectedException;
89
import java.time.Instant;
@@ -102,7 +103,7 @@ public void changeUserPhoto(User user, MultipartFile file)
102103

103104
// Save new photo
104105
String fileName = "userphoto_" + UUID.randomUUID() + ".png";
105-
File destinationFile = new File(Path.of(uploadLocation).normalize() + "/" + fileName);
106+
File destinationFile = Path.of(uploadLocation, fileName).normalize().toFile();
106107

107108
file.transferTo(destinationFile);
108109
String userPhoto = uploadLocation + fileName;
@@ -119,7 +120,11 @@ public void removeUserPhoto(User user) {
119120
File photoFile = new File(userPhoto);
120121
if (photoFile.exists()) {
121122
log.info("Removing profile picture for user ID {}: {}", user.getUserId(), userPhoto);
122-
photoFile.delete();
123+
try {
124+
Files.delete(photoFile.toPath());
125+
} catch (IOException e) {
126+
log.error(e.getMessage());
127+
}
123128
user.setUserPhoto(null);
124129
saveUser(user);
125130
}

server/src/test/java/dev/findfirst/core/service/TypesenseServiceTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import dev.findfirst.core.model.jdbc.TypesenseInitRecord;
1010
import dev.findfirst.core.repository.jdbc.TypsenseInitializationRepository;
1111

12+
import org.junit.jupiter.api.Disabled;
1213
import org.junit.jupiter.api.Test;
1314
import org.junit.jupiter.api.extension.ExtendWith;
1415
import org.mockito.InjectMocks;
@@ -75,6 +76,7 @@ void initializationWasNotFinished() throws Exception {
7576
}
7677

7778
@Test
79+
@Disabled("Implement test to save storeScrapedText")
7880
void storeScrapedText() throws Exception {
7981

8082
}

0 commit comments

Comments
 (0)