Fix audit log description overflow crashing bulk metadata refresh (#2778) (#2783)

Co-authored-by: acx10 <acx10@users.noreply.github.com>
This commit is contained in:
ACX
2026-02-17 15:59:52 -07:00
committed by GitHub
co-authored by acx10
parent ec45c88502
commit 4062f1f83f
3 changed files with 155 additions and 2 deletions
@@ -29,6 +29,8 @@ public class AuditService {
log(action, null, null, description);
}
private static final int MAX_DESCRIPTION_LENGTH = 1024;
public void log(AuditAction action, String entityType, Long entityId, String description) {
try {
Long userId = null;
@@ -49,13 +51,18 @@ public class AuditService {
String countryCode = geoIpService.resolveCountryCode(ipAddress);
String safeDescription = description;
if (safeDescription != null && safeDescription.length() > MAX_DESCRIPTION_LENGTH) {
safeDescription = safeDescription.substring(0, MAX_DESCRIPTION_LENGTH - 3) + "...";
}
AuditLogEntity entity = AuditLogEntity.builder()
.userId(userId)
.username(username)
.action(action)
.entityType(entityType)
.entityId(entityId)
.description(description)
.description(safeDescription)
.ipAddress(ipAddress)
.countryCode(countryCode)
.build();
@@ -41,6 +41,8 @@ public class TaskHistoryService {
auditService.log(AuditAction.TASK_EXECUTED, "Task", null, buildTaskDescription(type, options));
}
private static final int MAX_DESCRIPTION_LENGTH = 1024;
private String buildTaskDescription(TaskType type, Map<String, Object> options) {
String taskName = type != null ? type.getName() : "Unknown";
StringBuilder sb = new StringBuilder("Started task: ").append(taskName);
@@ -50,7 +52,26 @@ public class TaskHistoryService {
Object bookIds = options.get("bookIds");
Object libraryId = options.get("libraryId");
if (bookIds instanceof Collection<?> ids && !ids.isEmpty()) {
sb.append(" (Book IDs: ").append(ids.stream().map(Object::toString).collect(Collectors.joining(", "))).append(")");
sb.append(" (").append(ids.size()).append(" books, IDs: ");
String truncationSuffix = "...)";
Iterator<?> it = ids.iterator();
boolean truncated = false;
while (it.hasNext()) {
String id = it.next().toString();
boolean isLast = !it.hasNext();
String separator = sb.charAt(sb.length() - 1) == ' ' ? "" : ", ";
if (isLast && sb.length() + separator.length() + id.length() + 1 <= MAX_DESCRIPTION_LENGTH) {
sb.append(separator).append(id).append(")");
} else if (!isLast && sb.length() + separator.length() + id.length() + truncationSuffix.length() <= MAX_DESCRIPTION_LENGTH) {
sb.append(separator).append(id);
} else {
truncated = true;
break;
}
}
if (truncated) {
sb.append(truncationSuffix);
}
} else if (libraryId != null) {
sb.append(" (Library ID: ").append(libraryId).append(")");
}