Gate all file write operations for network storage users (#3231)

This commit is contained in:
acx10
2026-03-07 16:17:00 -07:00
committed by acx10
parent 919657c6fe
commit 36c7185636
22 changed files with 340 additions and 67 deletions
@@ -25,6 +25,10 @@ public class AppProperties {
*/
private String diskType = "LOCAL";
public boolean isLocalStorage() {
return "LOCAL".equalsIgnoreCase(diskType);
}
@Getter
@Setter
public static class RemoteAuth {
@@ -429,11 +429,10 @@ public class FileMoveService {
}
private void validateLocalStorage() {
String diskType = appProperties.getDiskType();
if (!"LOCAL".equalsIgnoreCase(diskType)) {
if (!appProperties.isLocalStorage()) {
throw new IllegalStateException(
"File move operations are only supported on local storage. " +
"Current disk type is configured as: " + diskType + ". " +
"Current disk type is configured as: " + appProperties.getDiskType() + ". " +
"If you are using local storage, set DISK_TYPE=LOCAL in your environment."
);
}
@@ -1,5 +1,6 @@
package org.booklore.service.metadata;
import org.booklore.config.AppProperties;
import org.booklore.exception.ApiError;
import org.booklore.model.dto.settings.MetadataPersistenceSettings;
import org.booklore.model.entity.AuthorEntity;
@@ -42,6 +43,7 @@ public class BookCoverService {
private static final int BATCH_SIZE = 100;
private final AppProperties appProperties;
private final BookRepository bookRepository;
private final NotificationService notificationService;
private final AppSettingService appSettingService;
@@ -540,9 +542,11 @@ public class BookCoverService {
}
private void writeCoverToBookFile(BookEntity bookEntity, BiConsumer<MetadataWriter, BookEntity> writerAction) {
if (!appProperties.isLocalStorage()) {
return;
}
var primaryFile = bookEntity.getPrimaryBookFile();
if (primaryFile == null) {
// Physical book with no files - skip writing cover to file
return;
}
@@ -560,6 +564,9 @@ public class BookCoverService {
}
private void writeAudiobookCoverToFile(BookEntity bookEntity, BiConsumer<MetadataWriter, BookEntity> writerAction) {
if (!appProperties.isLocalStorage()) {
return;
}
var audiobookFile = bookEntity.getBookFiles().stream()
.filter(f -> f.getBookType() == BookFileType.AUDIOBOOK)
.findFirst()
@@ -3,6 +3,7 @@ package org.booklore.service.metadata;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.booklore.config.AppProperties;
import org.booklore.model.MetadataClearFlags;
import org.booklore.model.MetadataUpdateContext;
import org.booklore.model.MetadataUpdateWrapper;
@@ -53,6 +54,7 @@ public class BookMetadataUpdater {
private final ComicTeamRepository comicTeamRepository;
private final ComicLocationRepository comicLocationRepository;
private final ComicCreatorRepository comicCreatorRepository;
private final AppProperties appProperties;
private final FileService fileService;
private final MetadataMatchService metadataMatchService;
private final AppSettingService appSettingService;
@@ -123,7 +125,7 @@ public class BookMetadataUpdater {
log.warn("Failed to calculate metadata match score for book ID {}: {}", bookId, e.getMessage());
}
if (primaryFile != null && bookType != null && ((writeToFile.isAnyFormatEnabled() && hasValueChangesForFileWrite) || thumbnailRequiresUpdate)) {
if (appProperties.isLocalStorage() && primaryFile != null && bookType != null && ((writeToFile.isAnyFormatEnabled() && hasValueChangesForFileWrite) || thumbnailRequiresUpdate)) {
metadataWriterFactory.getWriter(bookType).ifPresent(writer -> {
try {
String thumbnailUrl = updateThumbnail ? newMetadata.getThumbnailUrl() : null;
@@ -1,5 +1,6 @@
package org.booklore.service.metadata;
import org.booklore.config.AppProperties;
import org.booklore.model.dto.FileMoveResult;
import org.booklore.model.dto.settings.MetadataPersistenceSettings;
import org.booklore.model.entity.*;
@@ -25,6 +26,7 @@ import java.util.Optional;
@RequiredArgsConstructor
public class MetadataManagementService {
private final AppProperties appProperties;
private final AuthorRepository authorRepository;
private final CategoryRepository categoryRepository;
private final MoodRepository moodRepository;
@@ -61,13 +63,15 @@ public class MetadataManagementService {
var primaryFile = book.getPrimaryBookFile();
BookFileType bookType = primaryFile.getBookType();
Optional<MetadataWriter> writerOpt = metadataWriterFactory.getWriter(bookType);
if (writerOpt.isPresent()) {
File file = book.getFullFilePath().toFile();
writerOpt.get().saveMetadataToFile(file, metadata, null, null);
String newHash = FileFingerprint.generateHash(book.getFullFilePath());
primaryFile.setCurrentHash(newHash);
bookModified = true;
if (appProperties.isLocalStorage()) {
Optional<MetadataWriter> writerOpt = metadataWriterFactory.getWriter(bookType);
if (writerOpt.isPresent()) {
File file = book.getFullFilePath().toFile();
writerOpt.get().saveMetadataToFile(file, metadata, null, null);
String newHash = FileFingerprint.generateHash(book.getFullFilePath());
primaryFile.setCurrentHash(newHash);
bookModified = true;
}
}
if (moveFile) {
@@ -1,6 +1,7 @@
package org.booklore.service.metadata.sidecar;
import lombok.extern.slf4j.Slf4j;
import org.booklore.config.AppProperties;
import org.booklore.model.dto.settings.MetadataPersistenceSettings;
import org.booklore.model.dto.settings.SidecarSettings;
import org.booklore.model.dto.sidecar.SidecarMetadata;
@@ -22,12 +23,14 @@ import java.nio.file.StandardCopyOption;
@Service
public class SidecarMetadataWriter {
private final AppProperties appProperties;
private final SidecarMetadataMapper mapper;
private final FileService fileService;
private final AppSettingService appSettingService;
private final ObjectMapper objectMapper;
public SidecarMetadataWriter(SidecarMetadataMapper mapper, FileService fileService, AppSettingService appSettingService) {
public SidecarMetadataWriter(AppProperties appProperties, SidecarMetadataMapper mapper, FileService fileService, AppSettingService appSettingService) {
this.appProperties = appProperties;
this.mapper = mapper;
this.fileService = fileService;
this.appSettingService = appSettingService;
@@ -38,6 +41,9 @@ public class SidecarMetadataWriter {
}
public void writeSidecarMetadata(BookEntity book) {
if (!appProperties.isLocalStorage()) {
return;
}
if (book == null || book.getMetadata() == null) {
log.warn("Cannot write sidecar metadata: book or metadata is null");
return;