fix(metadata): add unit tests for task execution layer + fix permission validation logic (#2123)
Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
+3
-1
@@ -152,7 +152,9 @@ public class BookRecommendationUpdaterTask implements Task {
|
||||
|
||||
sendTaskProgressNotification(taskId, 100, String.format("Updated recommendations for %d books in %d ms", totalBooks, endTime - startTime), TaskStatus.COMPLETED, lastNotificationTime, true);
|
||||
|
||||
return builder.build();
|
||||
return builder
|
||||
.status(TaskStatus.COMPLETED)
|
||||
.build();
|
||||
}
|
||||
|
||||
private long sendTaskProgressNotification(String taskId, int progress, String message, TaskStatus taskStatus, long lastNotificationTime, boolean force) {
|
||||
|
||||
+2
-21
@@ -45,27 +45,8 @@ public class ClearCbxCacheTask implements Task {
|
||||
log.info("{}: Task started", getTaskType());
|
||||
|
||||
try {
|
||||
String cbxCachePath = fileService.getCbxCachePath();
|
||||
Path cachePath = Paths.get(cbxCachePath);
|
||||
|
||||
if (Files.exists(cachePath) && Files.isDirectory(cachePath)) {
|
||||
try (Stream<Path> walk = Files.walk(cachePath)) {
|
||||
walk.sorted(Comparator.reverseOrder())
|
||||
.forEach(path -> {
|
||||
try {
|
||||
Files.delete(path);
|
||||
} catch (IOException e) {
|
||||
log.error("Failed to delete file: {} - {}", path, e.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Files.createDirectories(cachePath);
|
||||
log.info("{}: Cache cleared and directory recreated", getTaskType());
|
||||
} else {
|
||||
log.warn("{}: Cache path does not exist or is not a directory: {}", getTaskType(), cbxCachePath);
|
||||
}
|
||||
|
||||
fileService.clearCacheDirectory(fileService.getCbxCachePath());
|
||||
log.info("{}: Cache cleared", getTaskType());
|
||||
builder.status(TaskStatus.COMPLETED);
|
||||
} catch (Exception e) {
|
||||
log.error("{}: Error clearing cache", getTaskType(), e);
|
||||
|
||||
+2
-21
@@ -45,27 +45,8 @@ public class ClearPdfCacheTask implements Task {
|
||||
log.info("{}: Task started", getTaskType());
|
||||
|
||||
try {
|
||||
String pdfCachePath = fileService.getPdfCachePath();
|
||||
Path cachePath = Paths.get(pdfCachePath);
|
||||
|
||||
if (Files.exists(cachePath) && Files.isDirectory(cachePath)) {
|
||||
try (Stream<Path> walk = Files.walk(cachePath)) {
|
||||
walk.sorted(Comparator.reverseOrder())
|
||||
.forEach(path -> {
|
||||
try {
|
||||
Files.delete(path);
|
||||
} catch (IOException e) {
|
||||
log.error("Failed to delete file: {} - {}", path, e.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Files.createDirectories(cachePath);
|
||||
log.info("{}: Cache cleared and directory recreated", getTaskType());
|
||||
} else {
|
||||
log.warn("{}: Cache path does not exist or is not a directory: {}", getTaskType(), pdfCachePath);
|
||||
}
|
||||
|
||||
fileService.clearCacheDirectory(fileService.getPdfCachePath());
|
||||
log.info("{}: Cache cleared", getTaskType());
|
||||
builder.status(TaskStatus.COMPLETED);
|
||||
} catch (Exception e) {
|
||||
log.error("{}: Error clearing cache", getTaskType(), e);
|
||||
|
||||
+9
-1
@@ -24,12 +24,20 @@ public class RefreshMetadataTask implements Task {
|
||||
@Override
|
||||
public void validatePermissions(BookLoreUser user, TaskCreateRequest request) {
|
||||
MetadataRefreshRequest refreshRequest = request.getOptions(MetadataRefreshRequest.class);
|
||||
if (refreshRequest.getBookIds().size() > 1 &&
|
||||
|
||||
if (requiresBulkPermission(refreshRequest) &&
|
||||
!CAN_BULK_AUTO_FETCH_METADATA.isGranted(user.getPermissions())) {
|
||||
throw PERMISSION_DENIED.createException(CAN_BULK_AUTO_FETCH_METADATA);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean requiresBulkPermission(MetadataRefreshRequest request) {
|
||||
if (MetadataRefreshRequest.RefreshType.LIBRARY.equals(request.getRefreshType())) {
|
||||
return true;
|
||||
}
|
||||
return request.getBookIds() != null && request.getBookIds().size() > 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TaskCreateResponse execute(TaskCreateRequest request) {
|
||||
MetadataRefreshRequest refreshRequest = request.getOptions(MetadataRefreshRequest.class);
|
||||
|
||||
@@ -563,4 +563,24 @@ public class FileService {
|
||||
if (maxLength <= 0) return "";
|
||||
return input.length() <= maxLength ? input : input.substring(0, maxLength);
|
||||
}
|
||||
|
||||
public void clearCacheDirectory(String cachePath) {
|
||||
Path path = Paths.get(cachePath);
|
||||
if (Files.exists(path) && Files.isDirectory(path)) {
|
||||
try (Stream<Path> walk = Files.walk(path)) {
|
||||
walk.sorted(Comparator.reverseOrder())
|
||||
.forEach(p -> {
|
||||
try {
|
||||
Files.delete(p);
|
||||
} catch (IOException e) {
|
||||
log.error("Failed to delete file in cache: {} - {}", p, e.getMessage());
|
||||
}
|
||||
});
|
||||
// Recreate the directory after deletion
|
||||
Files.createDirectories(path);
|
||||
} catch (IOException e) {
|
||||
log.error("Failed to clear cache directory: {} - {}", cachePath, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user