diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/controller/FileUploadController.java b/booklore-api/src/main/java/com/adityachandel/booklore/controller/FileUploadController.java new file mode 100644 index 000000000..e8b3ff4f3 --- /dev/null +++ b/booklore-api/src/main/java/com/adityachandel/booklore/controller/FileUploadController.java @@ -0,0 +1,24 @@ +package com.adityachandel.booklore.controller; + +import com.adityachandel.booklore.service.FileUploadService; +import lombok.AllArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +@AllArgsConstructor +@RestController +@RequestMapping("/api/v1/files") +public class FileUploadController { + + private final FileUploadService fileUploadService; + + @PostMapping("/upload") + public ResponseEntity uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("libraryId") long libraryId, @RequestParam("filePath") String filePath) { + fileUploadService.uploadFile(file, libraryId, filePath); + return ResponseEntity.noContent().build(); + } +} diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/controller/LibraryController.java b/booklore-api/src/main/java/com/adityachandel/booklore/controller/LibraryController.java index 0159c3015..509eb27f9 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/controller/LibraryController.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/controller/LibraryController.java @@ -23,6 +23,16 @@ public class LibraryController { private LibraryService libraryService; private BooksService booksService; + @GetMapping("/{libraryId}") + public ResponseEntity getLibrary(@PathVariable long libraryId) { + return ResponseEntity.ok(libraryService.getLibrary(libraryId)); + } + + @GetMapping + public ResponseEntity> getLibraries(@RequestParam(defaultValue = "0") @Min(0) int page, @RequestParam(defaultValue = "25") @Min(1) @Max(100) int size) { + return ResponseEntity.ok(libraryService.getLibraries(page, size)); + } + @PostMapping public ResponseEntity createLibraryNew(@RequestBody CreateLibraryRequest request) { return ResponseEntity.ok(libraryService.createLibrary(request)); @@ -33,10 +43,7 @@ public class LibraryController { return libraryService.parseLibraryBooks(libraryId, force); } - @GetMapping("/{libraryId}") - public ResponseEntity getLibrary(@PathVariable long libraryId) { - return ResponseEntity.ok(libraryService.getLibrary(libraryId)); - } + @DeleteMapping("/{libraryId}") public ResponseEntity deleteLibrary(@PathVariable long libraryId) { @@ -44,10 +51,7 @@ public class LibraryController { return ResponseEntity.noContent().build(); } - @GetMapping - public ResponseEntity> getLibraries(@RequestParam(defaultValue = "0") @Min(0) int page, @RequestParam(defaultValue = "25") @Min(1) @Max(100) int size) { - return ResponseEntity.ok(libraryService.getLibraries(page, size)); - } + @GetMapping("/{libraryId}/book/{bookId}/withNeighbors") public ResponseEntity getBookWithNeighbours(@PathVariable long libraryId, @PathVariable long bookId) { diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/exception/ErrorCode.java b/booklore-api/src/main/java/com/adityachandel/booklore/exception/ApiError.java similarity index 71% rename from booklore-api/src/main/java/com/adityachandel/booklore/exception/ErrorCode.java rename to booklore-api/src/main/java/com/adityachandel/booklore/exception/ApiError.java index a1849002e..9774ccefe 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/exception/ErrorCode.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/exception/ApiError.java @@ -5,20 +5,24 @@ import org.springframework.http.HttpStatus; @Getter -public enum ErrorCode { +public enum ApiError { AUTHOR_NOT_FOUND(HttpStatus.NOT_FOUND, "Author not found with ID: %d"), BOOK_NOT_FOUND(HttpStatus.NOT_FOUND, "Book not found with ID: %d"), FILE_READ_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "Error reading files from path"), IMAGE_NOT_FOUND(HttpStatus.NOT_FOUND, "Image not found or not readable"), - INVALID_FILE_FORMAT(HttpStatus.BAD_REQUEST, "Invalid file format"), + INVALID_FILE_FORMAT(HttpStatus.BAD_REQUEST, "Invalid file format, only pdf and epub are supported"), LIBRARY_NOT_FOUND(HttpStatus.NOT_FOUND, "Library not found with ID: %d"), BAD_REQUEST(HttpStatus.BAD_REQUEST, "Bad request"), + FILE_TOO_LARGE(HttpStatus.BAD_REQUEST, "File size exceeds the limit: 100 MB"), + DIRECTORY_CREATION_FAILED(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to create the directory: %s"), + INVALID_LIBRARY_PATH(HttpStatus.BAD_REQUEST, "Invalid library path"), + FILE_ALREADY_EXISTS(HttpStatus.CONFLICT, "File already exists"), INVALID_QUERY_PARAMETERS(HttpStatus.BAD_REQUEST, "Query parameters are required for the search."); private final HttpStatus status; private final String message; - ErrorCode(HttpStatus status, String message) { + ApiError(HttpStatus status, String message) { this.status = status; this.message = message; } diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/model/LibraryFile.java b/booklore-api/src/main/java/com/adityachandel/booklore/model/LibraryFile.java index 21e3d196b..b10606f0d 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/model/LibraryFile.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/model/LibraryFile.java @@ -2,8 +2,10 @@ package com.adityachandel.booklore.model; import com.adityachandel.booklore.model.entity.Library; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; +@Builder @Data @AllArgsConstructor public class LibraryFile { diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/model/dto/LibraryDTO.java b/booklore-api/src/main/java/com/adityachandel/booklore/model/dto/LibraryDTO.java index 42afdbfea..8a1022b05 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/model/dto/LibraryDTO.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/model/dto/LibraryDTO.java @@ -4,11 +4,14 @@ import com.fasterxml.jackson.annotation.JsonInclude; import lombok.Builder; import lombok.Data; +import java.util.List; + @Builder @Data @JsonInclude(JsonInclude.Include.NON_NULL) public class LibraryDTO { private Long id; private String name; + private List paths; } diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/model/dto/request/FileUploadRequest.java b/booklore-api/src/main/java/com/adityachandel/booklore/model/dto/request/FileUploadRequest.java new file mode 100644 index 000000000..a5cd128e5 --- /dev/null +++ b/booklore-api/src/main/java/com/adityachandel/booklore/model/dto/request/FileUploadRequest.java @@ -0,0 +1,11 @@ +package com.adityachandel.booklore.model.dto.request; + +import lombok.Data; +import org.springframework.web.multipart.MultipartFile; + +@Data +public class FileUploadRequest { + private String libraryId; + private String filePath; + private MultipartFile file; +} diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/model/dto/response/ApiResponseMessage.java b/booklore-api/src/main/java/com/adityachandel/booklore/model/dto/response/ApiResponseMessage.java new file mode 100644 index 000000000..1a7176bf8 --- /dev/null +++ b/booklore-api/src/main/java/com/adityachandel/booklore/model/dto/response/ApiResponseMessage.java @@ -0,0 +1,10 @@ +package com.adityachandel.booklore.model.dto.response; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class ApiResponseMessage { + private String message; +} diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/AuthorService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/AuthorService.java index 85acb9ce2..57bbfe61e 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/AuthorService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/AuthorService.java @@ -2,7 +2,7 @@ package com.adityachandel.booklore.service; import com.adityachandel.booklore.model.dto.AuthorDTO; import com.adityachandel.booklore.model.entity.Author; -import com.adityachandel.booklore.exception.ErrorCode; +import com.adityachandel.booklore.exception.ApiError; import com.adityachandel.booklore.repository.AuthorRepository; import com.adityachandel.booklore.repository.BookRepository; import com.adityachandel.booklore.transformer.AuthorTransformer; @@ -19,12 +19,12 @@ public class AuthorService { private final BookRepository bookRepository; public AuthorDTO getAuthorById(Long id) { - Author author = authorRepository.findById(id).orElseThrow(() -> ErrorCode.AUTHOR_NOT_FOUND.createException(id)); + Author author = authorRepository.findById(id).orElseThrow(() -> ApiError.AUTHOR_NOT_FOUND.createException(id)); return AuthorTransformer.toAuthorDTO(author); } public List getAuthorsByBookId(Long bookId) { - bookRepository.findById(bookId).orElseThrow(() -> ErrorCode.BOOK_NOT_FOUND.createException(bookId)); + bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId)); List authors = authorRepository.findAuthorsByBookId(bookId); return authors.stream().map(AuthorTransformer::toAuthorDTO).toList(); } diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/BookCreatorService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/BookCreatorService.java index 6c350bff7..e28dda5d7 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/BookCreatorService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/BookCreatorService.java @@ -44,23 +44,21 @@ public class BookCreatorService { } public void addAuthorsToBook(Set authors, Book book) { - for (String authorSrt : authors) { - Optional authorOptional = authorRepository.findByName(authorSrt); + for (String authorStr : authors) { + Optional authorOptional = authorRepository.findByName(authorStr); Author author; if (authorOptional.isPresent()) { author = authorOptional.get(); - if(book.getMetadata().getAuthors() == null) { - book.getMetadata().setAuthors(new ArrayList<>()); - } - book.getMetadata().getAuthors().add(author); } else { author = Author.builder() - .name(authorSrt) + .name(authorStr) .build(); - author.setBookMetadataList(new ArrayList<>()); - book.getMetadata().setAuthors(new ArrayList<>()); - book.getMetadata().getAuthors().add(author); + author = authorRepository.save(author); } + if (book.getMetadata().getAuthors() == null) { + book.getMetadata().setAuthors(new ArrayList<>()); + } + book.getMetadata().getAuthors().add(author); } } diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/BooksService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/BooksService.java index 1bdac075f..11e841f9d 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/BooksService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/BooksService.java @@ -6,7 +6,7 @@ import com.adityachandel.booklore.model.dto.BookWithNeighborsDTO; import com.adityachandel.booklore.model.dto.response.GoogleBooksMetadata; import com.adityachandel.booklore.model.dto.BookViewerSettingDTO; import com.adityachandel.booklore.model.dto.request.SetMetadataRequest; -import com.adityachandel.booklore.exception.ErrorCode; +import com.adityachandel.booklore.exception.ApiError; import com.adityachandel.booklore.model.entity.*; import com.adityachandel.booklore.repository.*; import com.adityachandel.booklore.transformer.BookSettingTransformer; @@ -52,7 +52,7 @@ public class BooksService { public BookDTO getBook(long bookId) { - Book book = bookRepository.findById(bookId).orElseThrow(() -> ErrorCode.BOOK_NOT_FOUND.createException(bookId)); + Book book = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId)); return BookTransformer.convertToBookDTO(book); } @@ -72,7 +72,7 @@ public class BooksService { } public void saveBookViewerSetting(long bookId, BookViewerSettingDTO bookViewerSettingDTO) { - BookViewerSetting bookViewerSetting = bookViewerSettingRepository.findById(bookId).orElseThrow(() -> ErrorCode.BOOK_NOT_FOUND.createException(bookId)); + BookViewerSetting bookViewerSetting = bookViewerSettingRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId)); bookViewerSetting.setPageNumber(bookViewerSettingDTO.getPageNumber()); bookViewerSetting.setZoom(bookViewerSettingDTO.getZoom()); bookViewerSetting.setSpread(bookViewerSettingDTO.getSpread()); @@ -81,7 +81,7 @@ public class BooksService { } public Resource getBookCover(long bookId) { - Book book = bookRepository.findById(bookId).orElseThrow(() -> ErrorCode.BOOK_NOT_FOUND.createException(bookId)); + Book book = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId)); String thumbPath = appProperties.getPathConfig() + "/thumbs/" + getFileNameWithoutExtension(book.getFileName()) + ".jpg"; Path filePath = Paths.get(thumbPath); try { @@ -89,15 +89,15 @@ public class BooksService { if (resource.exists() && resource.isReadable()) { return resource; } else { - throw ErrorCode.IMAGE_NOT_FOUND.createException(thumbPath); + throw ApiError.IMAGE_NOT_FOUND.createException(thumbPath); } } catch (IOException e) { - throw ErrorCode.IMAGE_NOT_FOUND.createException(thumbPath); + throw ApiError.IMAGE_NOT_FOUND.createException(thumbPath); } } public ResponseEntity getBookData(long bookId) throws IOException { - Book book = bookRepository.findById(bookId).orElseThrow(() -> ErrorCode.BOOK_NOT_FOUND.createException(bookId)); + Book book = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId)); byte[] pdfBytes = Files.readAllBytes(new File(book.getPath()).toPath()); return ResponseEntity.ok() .header(HttpHeaders.CONTENT_TYPE, "application/pdf") @@ -110,18 +110,18 @@ public class BooksService { } public BookViewerSettingDTO getBookViewerSetting(long bookId) { - BookViewerSetting bookViewerSetting = bookViewerSettingRepository.findById(bookId).orElseThrow(() -> ErrorCode.BOOK_NOT_FOUND.createException(bookId)); + BookViewerSetting bookViewerSetting = bookViewerSettingRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId)); return BookSettingTransformer.convertToDTO(bookViewerSetting); } public void updateLastReadTime(long bookId) { - Book book = bookRepository.findById(bookId).orElseThrow(() -> ErrorCode.BOOK_NOT_FOUND.createException(bookId)); + Book book = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId)); book.setLastReadTime(Instant.now()); bookRepository.save(book); } public List fetchProspectiveMetadataListByBookId(long bookId) { - Book book = bookRepository.findById(bookId).orElseThrow(() -> ErrorCode.BOOK_NOT_FOUND.createException(bookId)); + Book book = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId)); StringBuilder searchString = new StringBuilder(); if (!book.getMetadata().getTitle().isEmpty()) { searchString.append(book.getMetadata().getTitle()); @@ -145,7 +145,7 @@ public class BooksService { } public void setMetadata(SetMetadataRequest setMetadataRequest, long bookId) { - Book book = bookRepository.findById(bookId).orElseThrow(() -> ErrorCode.BOOK_NOT_FOUND.createException(bookId)); + Book book = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId)); GoogleBooksMetadata gMetadata = googleBookMetadataService.getByGoogleBookId(setMetadataRequest.getGoogleBookId()); BookMetadata metadata = book.getMetadata(); metadata.setGoogleBookId(gMetadata.getGoogleBookId()); @@ -214,8 +214,8 @@ public class BooksService { } public BookWithNeighborsDTO getBookWithNeighbours(long libraryId, long bookId) { - libraryRepository.findById(libraryId).orElseThrow(() -> ErrorCode.LIBRARY_NOT_FOUND.createException(libraryId)); - Book book = bookRepository.findById(bookId).orElseThrow(() -> ErrorCode.BOOK_NOT_FOUND.createException(bookId)); + libraryRepository.findById(libraryId).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId)); + Book book = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId)); Book previousBook = bookRepository.findFirstByLibraryIdAndIdLessThanOrderByIdDesc(libraryId, bookId).orElse(null); Book nextBook = bookRepository.findFirstByLibraryIdAndIdGreaterThanOrderByIdAsc(libraryId, bookId).orElse(null); return BookWithNeighborsDTO.builder() diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/FileUploadService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/FileUploadService.java new file mode 100644 index 000000000..8270a6415 --- /dev/null +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/FileUploadService.java @@ -0,0 +1,66 @@ +package com.adityachandel.booklore.service; + +import com.adityachandel.booklore.exception.ApiError; +import com.adityachandel.booklore.model.LibraryFile; +import com.adityachandel.booklore.model.entity.Library; +import com.adityachandel.booklore.repository.LibraryRepository; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; + +@AllArgsConstructor +@Service +@Slf4j +public class FileUploadService { + + private final LibraryRepository libraryRepository; + private final PdfFileProcessor fileProcessor; + + public void uploadFile(MultipartFile file, long libraryId, String filePath) { + Library library = libraryRepository.findById(libraryId).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId)); + if (!library.getPaths().contains(filePath)) { + throw ApiError.INVALID_LIBRARY_PATH.createException(); + } + String fileType = file.getContentType(); + if (!"application/pdf".equals(fileType) && !"application/epub+zip".equals(fileType)) { + throw ApiError.INVALID_FILE_FORMAT.createException(); + } + if (file.getSize() > 100 * 1024 * 1024) { + throw ApiError.FILE_TOO_LARGE.createException(); + } + try { + Path storagePath = Paths.get(filePath, file.getOriginalFilename()); + File storageFile = storagePath.toFile(); + if (storageFile.exists()) { + throw ApiError.FILE_ALREADY_EXISTS.createException(); + } + file.transferTo(storageFile); + LibraryFile libraryFile = LibraryFile.builder() + .library(library) + .fileType(getFileType(fileType)) + .filePath(storageFile.getAbsolutePath()) + .build(); + fileProcessor.processFile(libraryFile, false); + log.info("File uploaded successfully: {}", storageFile.getAbsolutePath()); + + } catch (IOException e) { + throw ApiError.FILE_READ_ERROR.createException(e.getMessage()); + } + } + + private String getFileType(String f) { + if (f.equalsIgnoreCase("application/pdf")) { + return "pdf"; + } else if (f.equalsIgnoreCase("application/epub+zip")) { + return "epub"; + } else { + return null; + } + } +} diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/LibraryService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/LibraryService.java index c4d33a31f..55932c225 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/LibraryService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/LibraryService.java @@ -8,7 +8,7 @@ import com.adityachandel.booklore.model.dto.*; import com.adityachandel.booklore.model.dto.request.CreateLibraryRequest; import com.adityachandel.booklore.model.entity.Book; import com.adityachandel.booklore.model.entity.Library; -import com.adityachandel.booklore.exception.ErrorCode; +import com.adityachandel.booklore.exception.ApiError; import com.adityachandel.booklore.repository.*; import com.adityachandel.booklore.transformer.BookTransformer; import com.adityachandel.booklore.transformer.LibraryTransformer; @@ -45,7 +45,7 @@ public class LibraryService { } public LibraryDTO getLibrary(long libraryId) { - Library library = libraryRepository.findById(libraryId).orElseThrow(() -> ErrorCode.LIBRARY_NOT_FOUND.createException(libraryId)); + Library library = libraryRepository.findById(libraryId).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId)); return LibraryTransformer.convertToLibraryDTO(library); } @@ -56,25 +56,25 @@ public class LibraryService { } public void deleteLibrary(long id) { - libraryRepository.findById(id).orElseThrow(() -> ErrorCode.LIBRARY_NOT_FOUND.createException(id)); + libraryRepository.findById(id).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(id)); libraryRepository.deleteById(id); } public BookDTO getBook(long libraryId, long bookId) { - libraryRepository.findById(libraryId).orElseThrow(() -> ErrorCode.LIBRARY_NOT_FOUND.createException(libraryId)); - Book book = bookRepository.findBookByIdAndLibraryId(bookId, libraryId).orElseThrow(() -> ErrorCode.BOOK_NOT_FOUND.createException(bookId)); + libraryRepository.findById(libraryId).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId)); + Book book = bookRepository.findBookByIdAndLibraryId(bookId, libraryId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId)); return BookTransformer.convertToBookDTO(book); } public Page getBooks(long libraryId, int page, int size) { - libraryRepository.findById(libraryId).orElseThrow(() -> ErrorCode.LIBRARY_NOT_FOUND.createException(libraryId)); + libraryRepository.findById(libraryId).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId)); PageRequest pageRequest = PageRequest.of(page, size); Page bookPage = bookRepository.findBooksByLibraryId(libraryId, pageRequest); return bookPage.map(BookTransformer::convertToBookDTO); } public SseEmitter parseLibraryBooks(long libraryId, boolean force) { - Library library = libraryRepository.findById(libraryId).orElseThrow(() -> ErrorCode.LIBRARY_NOT_FOUND.createException(libraryId)); + Library library = libraryRepository.findById(libraryId).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId)); SseEmitter emitter = new SseEmitter(); ExecutorService sseMvcExecutor = Executors.newSingleThreadExecutor(); sseMvcExecutor.execute(() -> { diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/transformer/LibraryTransformer.java b/booklore-api/src/main/java/com/adityachandel/booklore/transformer/LibraryTransformer.java index 1e2723407..1655502ee 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/transformer/LibraryTransformer.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/transformer/LibraryTransformer.java @@ -10,6 +10,7 @@ public class LibraryTransformer { return LibraryDTO.builder() .id(library.getId()) .name(library.getName()) + .paths(library.getPaths()) .build(); } } diff --git a/booklore-api/src/main/resources/application.yaml b/booklore-api/src/main/resources/application.yaml index 02f3ddb73..5cf7224fe 100644 --- a/booklore-api/src/main/resources/application.yaml +++ b/booklore-api/src/main/resources/application.yaml @@ -3,6 +3,11 @@ app: path-config: '/Users/aditya.chandel/my-library-temp/config' spring: + servlet: + multipart: + enabled: true + max-file-size: 100MB + max-request-size: 100MB mvc: async: request-timeout: 600000 diff --git a/booklore-ui/src/app/app.component.html b/booklore-ui/src/app/app.component.html index 0680b43f9..b6df4c168 100644 --- a/booklore-ui/src/app/app.component.html +++ b/booklore-ui/src/app/app.component.html @@ -1 +1,2 @@ + diff --git a/booklore-ui/src/app/app.module.ts b/booklore-ui/src/app/app.module.ts index 8f1e164b9..d60ee71a4 100644 --- a/booklore-ui/src/app/app.module.ts +++ b/booklore-ui/src/app/app.module.ts @@ -17,13 +17,11 @@ import {InputIconModule} from 'primeng/inputicon'; import {ToggleButtonModule} from 'primeng/togglebutton'; import {PasswordModule} from 'primeng/password'; import {ToastModule} from 'primeng/toast'; -import {LibraryBrowserComponent} from './book/component/library-browser/library-browser.component'; import {InfiniteScrollDirective} from 'ngx-infinite-scroll'; import {SearchComponent} from './book/component/search/search.component'; -import { BookMetadataComponent } from './book/component/book-metadata/book-metadata.component'; -import { BooksMetadataDialogComponent } from './book/component/books-metadata-dialog/books-metadata-dialog.component'; import {MessageService} from 'primeng/api'; -import { NotificationComponent } from './book/component/notification/notification.component'; +import {FileUploadComponent} from './book/component/file-upload/file-upload.component'; +import {DropdownModule} from 'primeng/dropdown'; @NgModule({ declarations: [ @@ -47,6 +45,7 @@ import { NotificationComponent } from './book/component/notification/notificatio ToastModule, InfiniteScrollDirective, SearchComponent, + DropdownModule, ], providers: [DialogService, MessageService], bootstrap: [AppComponent] diff --git a/booklore-ui/src/app/book/component/book-metadata/book-metadata.component.scss b/booklore-ui/src/app/book/component/book-metadata/book-metadata.component.scss index c3114f87a..3761f7940 100644 --- a/booklore-ui/src/app/book/component/book-metadata/book-metadata.component.scss +++ b/booklore-ui/src/app/book/component/book-metadata/book-metadata.component.scss @@ -1,10 +1,10 @@ .book-metadata-container { - height: calc(100vh - 10rem); /* Full height minus header and footer */ - overflow-y: auto; /* Enable vertical scrolling */ - transition: transform 0.2s, left 0.2s; /* Smooth transition for movements */ + height: calc(100vh - 10rem); + overflow-y: auto; + transition: transform 0.2s, left 0.2s; background-color: var(--surface-card); border-radius: 12px; - padding: 1.5rem 2rem; /* Adjust padding */ + padding: 1.5rem 2rem; } .book-metadata { @@ -33,7 +33,7 @@ .title-row { display: flex; - justify-content: space-between; /* Ensures title and buttons are spaced out */ + justify-content: space-between; align-items: center; margin-bottom: 0.6rem; } @@ -46,7 +46,7 @@ .button-group { display: flex; - gap: 1.5rem; /* Spacing between buttons */ + gap: 1.5rem; } .p-button-sm { diff --git a/booklore-ui/src/app/book/component/books-metadata-dialog/books-metadata-dialog.component.html b/booklore-ui/src/app/book/component/books-metadata-dialog/books-metadata-dialog.component.html index adae3d528..304a1ff3d 100644 --- a/booklore-ui/src/app/book/component/books-metadata-dialog/books-metadata-dialog.component.html +++ b/booklore-ui/src/app/book/component/books-metadata-dialog/books-metadata-dialog.component.html @@ -1,7 +1,10 @@
- Search + Search
diff --git a/booklore-ui/src/app/book/component/books-metadata-dialog/books-metadata-dialog.component.scss b/booklore-ui/src/app/book/component/books-metadata-dialog/books-metadata-dialog.component.scss index 3ff487b7b..2ba09c224 100644 --- a/booklore-ui/src/app/book/component/books-metadata-dialog/books-metadata-dialog.component.scss +++ b/booklore-ui/src/app/book/component/books-metadata-dialog/books-metadata-dialog.component.scss @@ -5,13 +5,13 @@ .input-group { display: flex; justify-content: space-between; - gap: 10px; /* Add space between the text box and button */ + gap: 10px; } input[type="text"] { padding: 10px; font-size: 1rem; - width: 90%; /* Adjust width as necessary */ + width: 90%; border: 1px solid #ddd; border-radius: 5px; } @@ -43,22 +43,22 @@ button:focus { .metadata-header { display: flex; - gap: 20px; /* Space between image and text */ + gap: 20px; } .book-image { - width: 100px; /* Adjust the width as needed */ - height: 150px; /* Adjust the height as needed */ - object-fit: cover; /* Ensure the image covers the area without distorting */ + width: 100px; + height: 150px; + object-fit: cover; border-radius: 5px; - margin-bottom: 10px; /* Add spacing between the image and the button */ + margin-bottom: 10px; } .select-button { - width: auto; /* Button width adjusts to content */ - padding: 8px 15px; /* Adjust the button padding */ - margin-top: 10px; /* Space between the image and button */ - text-align: center; /* Ensure text is centered in the button */ + width: auto; + padding: 8px 15px; + margin-top: 10px; + text-align: center; } .book-metadata { diff --git a/booklore-ui/src/app/book/component/books-metadata-dialog/books-metadata-dialog.component.ts b/booklore-ui/src/app/book/component/books-metadata-dialog/books-metadata-dialog.component.ts index 3ee01f224..ab72420ee 100644 --- a/booklore-ui/src/app/book/component/books-metadata-dialog/books-metadata-dialog.component.ts +++ b/booklore-ui/src/app/book/component/books-metadata-dialog/books-metadata-dialog.component.ts @@ -23,6 +23,7 @@ export class BooksMetadataDialogComponent implements OnInit { isLoading = true; errorMessage: string | null = null; searchText: string = ''; + isButtonDisabled = false; bookId: number = 0; libraryId: number = 0; @@ -64,15 +65,27 @@ export class BooksMetadataDialogComponent implements OnInit { } populateTitle() { + this.isButtonDisabled = true; + + setTimeout(() => { + if (this.isButtonDisabled) { + this.isButtonDisabled = false; + this.errorMessage = 'Request timed out. Please try again.'; + } + }, 5000); + this.bookService.fetchBookMetadataByTerm(this.searchText).subscribe({ next: (metadataList) => { this.bookMetadataList = metadataList; this.isLoading = false; + this.isButtonDisabled = false; }, error: (error) => { this.errorMessage = 'Failed to load metadata'; this.isLoading = false; + this.isButtonDisabled = false; } - }) + }); } } + diff --git a/booklore-ui/src/app/book/component/dashboard/dashboard.component.ts b/booklore-ui/src/app/book/component/dashboard/dashboard.component.ts index 078912dbe..f6f42214c 100644 --- a/booklore-ui/src/app/book/component/dashboard/dashboard.component.ts +++ b/booklore-ui/src/app/book/component/dashboard/dashboard.component.ts @@ -33,7 +33,7 @@ export class DashboardComponent { createNewLibrary(event: MouseEvent) { const buttonRect = (event.target as HTMLElement).getBoundingClientRect(); - const dialogWidthPercentage = 50; // Use percentage-based width + const dialogWidthPercentage = 50; const viewportWidth = window.innerWidth; const dialogWidth = (dialogWidthPercentage / 100) * viewportWidth; diff --git a/booklore-ui/src/app/book/component/file-upload/file-upload.component.html b/booklore-ui/src/app/book/component/file-upload/file-upload.component.html new file mode 100644 index 000000000..2b6c4fbde --- /dev/null +++ b/booklore-ui/src/app/book/component/file-upload/file-upload.component.html @@ -0,0 +1,52 @@ +
+ +
+ + +
+ +
+ + +
+ +
+
+ + + {{ selectedFileName }} +
+ +
+ +
+ + +
+ {{ errorMessage }} +
+
+ +
diff --git a/booklore-ui/src/app/book/component/file-upload/file-upload.component.scss b/booklore-ui/src/app/book/component/file-upload/file-upload.component.scss new file mode 100644 index 000000000..bc28b9039 --- /dev/null +++ b/booklore-ui/src/app/book/component/file-upload/file-upload.component.scss @@ -0,0 +1,28 @@ +.flex { + display: flex; +} + +.flex + .flex { + margin-top: 15px; +} + +.button-container { + display: flex; + align-items: center; + gap: 10px; +} + +.file-name { + font-weight: bold; + color: var(--text-color-secondary); +} + +.error-message-container { + display: flex; + align-items: center; +} + +.error-message { + color: var(--red-600); + margin-left: 10px; +} diff --git a/booklore-ui/src/app/book/component/file-upload/file-upload.component.ts b/booklore-ui/src/app/book/component/file-upload/file-upload.component.ts new file mode 100644 index 000000000..73a8693b9 --- /dev/null +++ b/booklore-ui/src/app/book/component/file-upload/file-upload.component.ts @@ -0,0 +1,89 @@ +import {Component, OnInit} from '@angular/core'; +import {Library} from '../../model/library.model'; +import {LibraryService} from '../../service/library.service'; +import {FileUploadService} from '../../../file-upload.service'; +import {Button} from 'primeng/button'; +import {NgIf} from '@angular/common'; +import {DropdownModule} from 'primeng/dropdown'; +import {FormsModule} from '@angular/forms'; +import {ToastService} from '../../../toast.service'; +import {DynamicDialogRef} from 'primeng/dynamicdialog'; + +@Component({ + selector: 'app-file-upload-v2', + templateUrl: './file-upload.component.html', + styleUrls: ['./file-upload.component.scss'], + imports: [ + Button, + NgIf, + DropdownModule, + FormsModule + ], +}) +export class FileUploadComponent implements OnInit { + libraries: Library[] = []; + selectedLibrary: Library | null = null; + selectedPath: string = ''; + selectedFileName: string = ''; + isUploading: boolean = false; + errorMessage: string | null = null; + + constructor( + private libraryService: LibraryService, + private fileUploadService: FileUploadService, + private toastService: ToastService, + private dynamicDialogRef: DynamicDialogRef + ) { + } + + ngOnInit() { + this.libraries = this.libraryService.libraries(); + } + + triggerFileInput(fileInput: HTMLInputElement): void { + fileInput.click(); + } + + onFileSelected(event: Event): void { + const input = event.target as HTMLInputElement; + if (input && input.files) { + const file = input.files[0]; + if (file) { + this.selectedFileName = file.name; + } + } + } + + isFormValid(): boolean { + return !!this.selectedLibrary && !!this.selectedPath && !!this.selectedFileName; + } + + uploadFile(): void { + this.errorMessage = null; + if (!this.isFormValid()) { + return; + } + const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement; + if (!fileInput || !fileInput.files || fileInput.files.length === 0) { + return; + } + const file = fileInput.files[0]; + const libraryId = this.selectedLibrary?.id ? this.selectedLibrary.id.toString() : ''; + const path = this.selectedPath || ''; + this.isUploading = true; + this.fileUploadService.uploadFile(file, libraryId, path) + .subscribe( + (response) => { + console.log('File uploaded successfully'); + this.toastService.showSuccess("File uploaded", file.name); + this.isUploading = false; + this.dynamicDialogRef.close(); + }, + (error) => { + this.isUploading = false; + this.toastService.showError("Upload failed", error.error.message); + this.errorMessage = "Upload failed. " + error.error.message + "."; + } + ); + } +} diff --git a/booklore-ui/src/app/book/component/layout/app.layout.component.html b/booklore-ui/src/app/book/component/layout/app.layout.component.html index 12ad1278f..85098700c 100644 --- a/booklore-ui/src/app/book/component/layout/app.layout.component.html +++ b/booklore-ui/src/app/book/component/layout/app.layout.component.html @@ -1,14 +1,14 @@
- -
- + +
+ +
+
+
+
-
-
- -
- -
- -
+ +
+ +
diff --git a/booklore-ui/src/app/book/component/layout/app.layout.component.ts b/booklore-ui/src/app/book/component/layout/app.layout.component.ts index 7cbd9fa68..2e5aa2cb8 100644 --- a/booklore-ui/src/app/book/component/layout/app.layout.component.ts +++ b/booklore-ui/src/app/book/component/layout/app.layout.component.ts @@ -7,6 +7,7 @@ import { AppTopBarComponent } from './app.topbar.component'; import {AppFooterComponent} from "./app.footer.component"; import {AppConfigComponent} from "./config/app.config.component"; import {NgClass} from '@angular/common'; +import {ToastModule} from 'primeng/toast'; @Component({ selector: 'app-layout', @@ -16,7 +17,8 @@ import {NgClass} from '@angular/common'; AppConfigComponent, AppSidebarComponent, AppTopBarComponent, - NgClass + NgClass, + ToastModule ], templateUrl: './app.layout.component.html' }) diff --git a/booklore-ui/src/app/book/component/layout/app.menuitem.component.ts b/booklore-ui/src/app/book/component/layout/app.menuitem.component.ts index 23119142a..fa159d363 100644 --- a/booklore-ui/src/app/book/component/layout/app.menuitem.component.ts +++ b/booklore-ui/src/app/book/component/layout/app.menuitem.component.ts @@ -123,22 +123,16 @@ export class AppMenuitemComponent implements OnInit, OnDestroy { } itemClick(event: Event) { - // avoid processing disabled items if (this.item.disabled) { event.preventDefault(); return; } - - // execute command if (this.item.command) { this.item.command({ originalEvent: event, item: this.item }); } - - // toggle active state if (this.item.items) { this.active = !this.active; } - this.menuService.onMenuStateChange({ key: this.key }); } diff --git a/booklore-ui/src/app/book/component/layout/app.topbar.component.html b/booklore-ui/src/app/book/component/layout/app.topbar.component.html index a1aa356a7..53720a29c 100644 --- a/booklore-ui/src/app/book/component/layout/app.topbar.component.html +++ b/booklore-ui/src/app/book/component/layout/app.topbar.component.html @@ -24,8 +24,10 @@ tooltipPosition="top"> -