first implementation - image/drawing integration
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
package com.aiteacher.book;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.UUID;
|
||||
@@ -15,10 +17,15 @@ public class BookService {
|
||||
|
||||
private final BookRepository bookRepository;
|
||||
private final BookEmbeddingService bookEmbeddingService;
|
||||
private final Path bookStoragePath;
|
||||
|
||||
public BookService(BookRepository bookRepository, BookEmbeddingService bookEmbeddingService) {
|
||||
public BookService(
|
||||
BookRepository bookRepository,
|
||||
BookEmbeddingService bookEmbeddingService,
|
||||
@Value("${app.figure-storage.base-path:./uploads}") String basePath) {
|
||||
this.bookRepository = bookRepository;
|
||||
this.bookEmbeddingService = bookEmbeddingService;
|
||||
this.bookStoragePath = Paths.get(basePath).toAbsolutePath().normalize().resolve("books");
|
||||
}
|
||||
|
||||
public Book upload(MultipartFile file) throws IOException {
|
||||
@@ -28,20 +35,35 @@ public class BookService {
|
||||
}
|
||||
|
||||
String title = deriveTitle(originalFilename);
|
||||
|
||||
Book book = new Book(title, originalFilename, file.getSize());
|
||||
book = bookRepository.save(book);
|
||||
|
||||
// Write to a temp file so the async task can read it
|
||||
Path tempFile = Files.createTempFile("aiteacher-", "-" + book.getId() + ".pdf");
|
||||
file.transferTo(tempFile.toFile());
|
||||
// Persist PDF in a stable location for potential re-embedding
|
||||
Files.createDirectories(bookStoragePath);
|
||||
Path pdfPath = bookStoragePath.resolve(book.getId() + ".pdf");
|
||||
file.transferTo(pdfPath.toFile());
|
||||
|
||||
UUID bookId = book.getId();
|
||||
Path pdfPath = tempFile;
|
||||
String bookTitle = title;
|
||||
bookEmbeddingService.embedBook(bookId, title, pdfPath);
|
||||
return book;
|
||||
}
|
||||
|
||||
bookEmbeddingService.embedBook(bookId, bookTitle, pdfPath);
|
||||
public Book reembed(UUID id) {
|
||||
Book book = bookRepository.findById(id)
|
||||
.orElseThrow(() -> new NoSuchElementException("Book not found."));
|
||||
|
||||
if (book.getStatus() == BookStatus.PROCESSING) {
|
||||
throw new IllegalStateException("Book is already being processed.");
|
||||
}
|
||||
|
||||
Path pdfPath = bookStoragePath.resolve(id + ".pdf");
|
||||
if (!Files.exists(pdfPath)) {
|
||||
throw new IllegalStateException(
|
||||
"Original PDF not found. Please re-upload the book before re-embedding.");
|
||||
}
|
||||
|
||||
bookEmbeddingService.deleteBookChunks(id);
|
||||
bookEmbeddingService.embedBook(id, book.getTitle(), pdfPath);
|
||||
return book;
|
||||
}
|
||||
|
||||
@@ -63,14 +85,21 @@ public class BookService {
|
||||
}
|
||||
|
||||
bookEmbeddingService.deleteBookChunks(id);
|
||||
|
||||
// Delete the stored PDF
|
||||
Path pdfPath = bookStoragePath.resolve(id + ".pdf");
|
||||
try {
|
||||
Files.deleteIfExists(pdfPath);
|
||||
} catch (IOException ex) {
|
||||
// Non-fatal — log only
|
||||
}
|
||||
|
||||
bookRepository.deleteById(id);
|
||||
}
|
||||
|
||||
private String deriveTitle(String filename) {
|
||||
// Strip .pdf extension and replace separators with spaces
|
||||
String name = filename.replaceAll("(?i)\\.pdf$", "");
|
||||
name = name.replaceAll("[-_]", " ");
|
||||
// Capitalise first letter
|
||||
if (!name.isEmpty()) {
|
||||
name = Character.toUpperCase(name.charAt(0)) + name.substring(1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user