Add file upload functionality
This commit is contained in:
+24
@@ -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<Void> uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("libraryId") long libraryId, @RequestParam("filePath") String filePath) {
|
||||||
|
fileUploadService.uploadFile(file, libraryId, filePath);
|
||||||
|
return ResponseEntity.noContent().build();
|
||||||
|
}
|
||||||
|
}
|
||||||
+12
-8
@@ -23,6 +23,16 @@ public class LibraryController {
|
|||||||
private LibraryService libraryService;
|
private LibraryService libraryService;
|
||||||
private BooksService booksService;
|
private BooksService booksService;
|
||||||
|
|
||||||
|
@GetMapping("/{libraryId}")
|
||||||
|
public ResponseEntity<LibraryDTO> getLibrary(@PathVariable long libraryId) {
|
||||||
|
return ResponseEntity.ok(libraryService.getLibrary(libraryId));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public ResponseEntity<Page<LibraryDTO>> 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
|
@PostMapping
|
||||||
public ResponseEntity<LibraryDTO> createLibraryNew(@RequestBody CreateLibraryRequest request) {
|
public ResponseEntity<LibraryDTO> createLibraryNew(@RequestBody CreateLibraryRequest request) {
|
||||||
return ResponseEntity.ok(libraryService.createLibrary(request));
|
return ResponseEntity.ok(libraryService.createLibrary(request));
|
||||||
@@ -33,10 +43,7 @@ public class LibraryController {
|
|||||||
return libraryService.parseLibraryBooks(libraryId, force);
|
return libraryService.parseLibraryBooks(libraryId, force);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{libraryId}")
|
|
||||||
public ResponseEntity<LibraryDTO> getLibrary(@PathVariable long libraryId) {
|
|
||||||
return ResponseEntity.ok(libraryService.getLibrary(libraryId));
|
|
||||||
}
|
|
||||||
|
|
||||||
@DeleteMapping("/{libraryId}")
|
@DeleteMapping("/{libraryId}")
|
||||||
public ResponseEntity<?> deleteLibrary(@PathVariable long libraryId) {
|
public ResponseEntity<?> deleteLibrary(@PathVariable long libraryId) {
|
||||||
@@ -44,10 +51,7 @@ public class LibraryController {
|
|||||||
return ResponseEntity.noContent().build();
|
return ResponseEntity.noContent().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
|
||||||
public ResponseEntity<Page<LibraryDTO>> 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")
|
@GetMapping("/{libraryId}/book/{bookId}/withNeighbors")
|
||||||
public ResponseEntity<BookWithNeighborsDTO> getBookWithNeighbours(@PathVariable long libraryId, @PathVariable long bookId) {
|
public ResponseEntity<BookWithNeighborsDTO> getBookWithNeighbours(@PathVariable long libraryId, @PathVariable long bookId) {
|
||||||
|
|||||||
+7
-3
@@ -5,20 +5,24 @@ import org.springframework.http.HttpStatus;
|
|||||||
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
public enum ErrorCode {
|
public enum ApiError {
|
||||||
AUTHOR_NOT_FOUND(HttpStatus.NOT_FOUND, "Author not found with ID: %d"),
|
AUTHOR_NOT_FOUND(HttpStatus.NOT_FOUND, "Author not found with ID: %d"),
|
||||||
BOOK_NOT_FOUND(HttpStatus.NOT_FOUND, "Book 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"),
|
FILE_READ_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "Error reading files from path"),
|
||||||
IMAGE_NOT_FOUND(HttpStatus.NOT_FOUND, "Image not found or not readable"),
|
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"),
|
LIBRARY_NOT_FOUND(HttpStatus.NOT_FOUND, "Library not found with ID: %d"),
|
||||||
BAD_REQUEST(HttpStatus.BAD_REQUEST, "Bad request"),
|
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.");
|
INVALID_QUERY_PARAMETERS(HttpStatus.BAD_REQUEST, "Query parameters are required for the search.");
|
||||||
|
|
||||||
private final HttpStatus status;
|
private final HttpStatus status;
|
||||||
private final String message;
|
private final String message;
|
||||||
|
|
||||||
ErrorCode(HttpStatus status, String message) {
|
ApiError(HttpStatus status, String message) {
|
||||||
this.status = status;
|
this.status = status;
|
||||||
this.message = message;
|
this.message = message;
|
||||||
}
|
}
|
||||||
@@ -2,8 +2,10 @@ package com.adityachandel.booklore.model;
|
|||||||
|
|
||||||
import com.adityachandel.booklore.model.entity.Library;
|
import com.adityachandel.booklore.model.entity.Library;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Builder
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class LibraryFile {
|
public class LibraryFile {
|
||||||
|
|||||||
@@ -4,11 +4,14 @@ import com.fasterxml.jackson.annotation.JsonInclude;
|
|||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Builder
|
@Builder
|
||||||
@Data
|
@Data
|
||||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
public class LibraryDTO {
|
public class LibraryDTO {
|
||||||
private Long id;
|
private Long id;
|
||||||
private String name;
|
private String name;
|
||||||
|
private List<String> paths;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+11
@@ -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;
|
||||||
|
}
|
||||||
+10
@@ -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;
|
||||||
|
}
|
||||||
@@ -2,7 +2,7 @@ package com.adityachandel.booklore.service;
|
|||||||
|
|
||||||
import com.adityachandel.booklore.model.dto.AuthorDTO;
|
import com.adityachandel.booklore.model.dto.AuthorDTO;
|
||||||
import com.adityachandel.booklore.model.entity.Author;
|
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.AuthorRepository;
|
||||||
import com.adityachandel.booklore.repository.BookRepository;
|
import com.adityachandel.booklore.repository.BookRepository;
|
||||||
import com.adityachandel.booklore.transformer.AuthorTransformer;
|
import com.adityachandel.booklore.transformer.AuthorTransformer;
|
||||||
@@ -19,12 +19,12 @@ public class AuthorService {
|
|||||||
private final BookRepository bookRepository;
|
private final BookRepository bookRepository;
|
||||||
|
|
||||||
public AuthorDTO getAuthorById(Long id) {
|
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);
|
return AuthorTransformer.toAuthorDTO(author);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<AuthorDTO> getAuthorsByBookId(Long bookId) {
|
public List<AuthorDTO> getAuthorsByBookId(Long bookId) {
|
||||||
bookRepository.findById(bookId).orElseThrow(() -> ErrorCode.BOOK_NOT_FOUND.createException(bookId));
|
bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||||
List<Author> authors = authorRepository.findAuthorsByBookId(bookId);
|
List<Author> authors = authorRepository.findAuthorsByBookId(bookId);
|
||||||
return authors.stream().map(AuthorTransformer::toAuthorDTO).toList();
|
return authors.stream().map(AuthorTransformer::toAuthorDTO).toList();
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-10
@@ -44,23 +44,21 @@ public class BookCreatorService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void addAuthorsToBook(Set<String> authors, Book book) {
|
public void addAuthorsToBook(Set<String> authors, Book book) {
|
||||||
for (String authorSrt : authors) {
|
for (String authorStr : authors) {
|
||||||
Optional<Author> authorOptional = authorRepository.findByName(authorSrt);
|
Optional<Author> authorOptional = authorRepository.findByName(authorStr);
|
||||||
Author author;
|
Author author;
|
||||||
if (authorOptional.isPresent()) {
|
if (authorOptional.isPresent()) {
|
||||||
author = authorOptional.get();
|
author = authorOptional.get();
|
||||||
if(book.getMetadata().getAuthors() == null) {
|
|
||||||
book.getMetadata().setAuthors(new ArrayList<>());
|
|
||||||
}
|
|
||||||
book.getMetadata().getAuthors().add(author);
|
|
||||||
} else {
|
} else {
|
||||||
author = Author.builder()
|
author = Author.builder()
|
||||||
.name(authorSrt)
|
.name(authorStr)
|
||||||
.build();
|
.build();
|
||||||
author.setBookMetadataList(new ArrayList<>());
|
author = authorRepository.save(author);
|
||||||
book.getMetadata().setAuthors(new ArrayList<>());
|
|
||||||
book.getMetadata().getAuthors().add(author);
|
|
||||||
}
|
}
|
||||||
|
if (book.getMetadata().getAuthors() == null) {
|
||||||
|
book.getMetadata().setAuthors(new ArrayList<>());
|
||||||
|
}
|
||||||
|
book.getMetadata().getAuthors().add(author);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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.response.GoogleBooksMetadata;
|
||||||
import com.adityachandel.booklore.model.dto.BookViewerSettingDTO;
|
import com.adityachandel.booklore.model.dto.BookViewerSettingDTO;
|
||||||
import com.adityachandel.booklore.model.dto.request.SetMetadataRequest;
|
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.model.entity.*;
|
||||||
import com.adityachandel.booklore.repository.*;
|
import com.adityachandel.booklore.repository.*;
|
||||||
import com.adityachandel.booklore.transformer.BookSettingTransformer;
|
import com.adityachandel.booklore.transformer.BookSettingTransformer;
|
||||||
@@ -52,7 +52,7 @@ public class BooksService {
|
|||||||
|
|
||||||
|
|
||||||
public BookDTO getBook(long bookId) {
|
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);
|
return BookTransformer.convertToBookDTO(book);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,7 +72,7 @@ public class BooksService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void saveBookViewerSetting(long bookId, BookViewerSettingDTO bookViewerSettingDTO) {
|
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.setPageNumber(bookViewerSettingDTO.getPageNumber());
|
||||||
bookViewerSetting.setZoom(bookViewerSettingDTO.getZoom());
|
bookViewerSetting.setZoom(bookViewerSettingDTO.getZoom());
|
||||||
bookViewerSetting.setSpread(bookViewerSettingDTO.getSpread());
|
bookViewerSetting.setSpread(bookViewerSettingDTO.getSpread());
|
||||||
@@ -81,7 +81,7 @@ public class BooksService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Resource getBookCover(long bookId) {
|
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";
|
String thumbPath = appProperties.getPathConfig() + "/thumbs/" + getFileNameWithoutExtension(book.getFileName()) + ".jpg";
|
||||||
Path filePath = Paths.get(thumbPath);
|
Path filePath = Paths.get(thumbPath);
|
||||||
try {
|
try {
|
||||||
@@ -89,15 +89,15 @@ public class BooksService {
|
|||||||
if (resource.exists() && resource.isReadable()) {
|
if (resource.exists() && resource.isReadable()) {
|
||||||
return resource;
|
return resource;
|
||||||
} else {
|
} else {
|
||||||
throw ErrorCode.IMAGE_NOT_FOUND.createException(thumbPath);
|
throw ApiError.IMAGE_NOT_FOUND.createException(thumbPath);
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw ErrorCode.IMAGE_NOT_FOUND.createException(thumbPath);
|
throw ApiError.IMAGE_NOT_FOUND.createException(thumbPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ResponseEntity<byte[]> getBookData(long bookId) throws IOException {
|
public ResponseEntity<byte[]> 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());
|
byte[] pdfBytes = Files.readAllBytes(new File(book.getPath()).toPath());
|
||||||
return ResponseEntity.ok()
|
return ResponseEntity.ok()
|
||||||
.header(HttpHeaders.CONTENT_TYPE, "application/pdf")
|
.header(HttpHeaders.CONTENT_TYPE, "application/pdf")
|
||||||
@@ -110,18 +110,18 @@ public class BooksService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public BookViewerSettingDTO getBookViewerSetting(long bookId) {
|
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);
|
return BookSettingTransformer.convertToDTO(bookViewerSetting);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateLastReadTime(long bookId) {
|
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());
|
book.setLastReadTime(Instant.now());
|
||||||
bookRepository.save(book);
|
bookRepository.save(book);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<GoogleBooksMetadata> fetchProspectiveMetadataListByBookId(long bookId) {
|
public List<GoogleBooksMetadata> 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();
|
StringBuilder searchString = new StringBuilder();
|
||||||
if (!book.getMetadata().getTitle().isEmpty()) {
|
if (!book.getMetadata().getTitle().isEmpty()) {
|
||||||
searchString.append(book.getMetadata().getTitle());
|
searchString.append(book.getMetadata().getTitle());
|
||||||
@@ -145,7 +145,7 @@ public class BooksService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setMetadata(SetMetadataRequest setMetadataRequest, long bookId) {
|
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());
|
GoogleBooksMetadata gMetadata = googleBookMetadataService.getByGoogleBookId(setMetadataRequest.getGoogleBookId());
|
||||||
BookMetadata metadata = book.getMetadata();
|
BookMetadata metadata = book.getMetadata();
|
||||||
metadata.setGoogleBookId(gMetadata.getGoogleBookId());
|
metadata.setGoogleBookId(gMetadata.getGoogleBookId());
|
||||||
@@ -214,8 +214,8 @@ public class BooksService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public BookWithNeighborsDTO getBookWithNeighbours(long libraryId, long bookId) {
|
public BookWithNeighborsDTO getBookWithNeighbours(long libraryId, long bookId) {
|
||||||
libraryRepository.findById(libraryId).orElseThrow(() -> ErrorCode.LIBRARY_NOT_FOUND.createException(libraryId));
|
libraryRepository.findById(libraryId).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId));
|
||||||
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 previousBook = bookRepository.findFirstByLibraryIdAndIdLessThanOrderByIdDesc(libraryId, bookId).orElse(null);
|
Book previousBook = bookRepository.findFirstByLibraryIdAndIdLessThanOrderByIdDesc(libraryId, bookId).orElse(null);
|
||||||
Book nextBook = bookRepository.findFirstByLibraryIdAndIdGreaterThanOrderByIdAsc(libraryId, bookId).orElse(null);
|
Book nextBook = bookRepository.findFirstByLibraryIdAndIdGreaterThanOrderByIdAsc(libraryId, bookId).orElse(null);
|
||||||
return BookWithNeighborsDTO.builder()
|
return BookWithNeighborsDTO.builder()
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,7 +8,7 @@ import com.adityachandel.booklore.model.dto.*;
|
|||||||
import com.adityachandel.booklore.model.dto.request.CreateLibraryRequest;
|
import com.adityachandel.booklore.model.dto.request.CreateLibraryRequest;
|
||||||
import com.adityachandel.booklore.model.entity.Book;
|
import com.adityachandel.booklore.model.entity.Book;
|
||||||
import com.adityachandel.booklore.model.entity.Library;
|
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.repository.*;
|
||||||
import com.adityachandel.booklore.transformer.BookTransformer;
|
import com.adityachandel.booklore.transformer.BookTransformer;
|
||||||
import com.adityachandel.booklore.transformer.LibraryTransformer;
|
import com.adityachandel.booklore.transformer.LibraryTransformer;
|
||||||
@@ -45,7 +45,7 @@ public class LibraryService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public LibraryDTO getLibrary(long libraryId) {
|
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);
|
return LibraryTransformer.convertToLibraryDTO(library);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,25 +56,25 @@ public class LibraryService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void deleteLibrary(long id) {
|
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);
|
libraryRepository.deleteById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public BookDTO getBook(long libraryId, long bookId) {
|
public BookDTO getBook(long libraryId, long bookId) {
|
||||||
libraryRepository.findById(libraryId).orElseThrow(() -> ErrorCode.LIBRARY_NOT_FOUND.createException(libraryId));
|
libraryRepository.findById(libraryId).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId));
|
||||||
Book book = bookRepository.findBookByIdAndLibraryId(bookId, libraryId).orElseThrow(() -> ErrorCode.BOOK_NOT_FOUND.createException(bookId));
|
Book book = bookRepository.findBookByIdAndLibraryId(bookId, libraryId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||||
return BookTransformer.convertToBookDTO(book);
|
return BookTransformer.convertToBookDTO(book);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Page<BookDTO> getBooks(long libraryId, int page, int size) {
|
public Page<BookDTO> 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);
|
PageRequest pageRequest = PageRequest.of(page, size);
|
||||||
Page<Book> bookPage = bookRepository.findBooksByLibraryId(libraryId, pageRequest);
|
Page<Book> bookPage = bookRepository.findBooksByLibraryId(libraryId, pageRequest);
|
||||||
return bookPage.map(BookTransformer::convertToBookDTO);
|
return bookPage.map(BookTransformer::convertToBookDTO);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SseEmitter parseLibraryBooks(long libraryId, boolean force) {
|
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();
|
SseEmitter emitter = new SseEmitter();
|
||||||
ExecutorService sseMvcExecutor = Executors.newSingleThreadExecutor();
|
ExecutorService sseMvcExecutor = Executors.newSingleThreadExecutor();
|
||||||
sseMvcExecutor.execute(() -> {
|
sseMvcExecutor.execute(() -> {
|
||||||
|
|||||||
+1
@@ -10,6 +10,7 @@ public class LibraryTransformer {
|
|||||||
return LibraryDTO.builder()
|
return LibraryDTO.builder()
|
||||||
.id(library.getId())
|
.id(library.getId())
|
||||||
.name(library.getName())
|
.name(library.getName())
|
||||||
|
.paths(library.getPaths())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,11 @@ app:
|
|||||||
path-config: '/Users/aditya.chandel/my-library-temp/config'
|
path-config: '/Users/aditya.chandel/my-library-temp/config'
|
||||||
|
|
||||||
spring:
|
spring:
|
||||||
|
servlet:
|
||||||
|
multipart:
|
||||||
|
enabled: true
|
||||||
|
max-file-size: 100MB
|
||||||
|
max-request-size: 100MB
|
||||||
mvc:
|
mvc:
|
||||||
async:
|
async:
|
||||||
request-timeout: 600000
|
request-timeout: 600000
|
||||||
|
|||||||
@@ -1 +1,2 @@
|
|||||||
|
<p-toast></p-toast>
|
||||||
<router-outlet></router-outlet>
|
<router-outlet></router-outlet>
|
||||||
|
|||||||
@@ -17,13 +17,11 @@ import {InputIconModule} from 'primeng/inputicon';
|
|||||||
import {ToggleButtonModule} from 'primeng/togglebutton';
|
import {ToggleButtonModule} from 'primeng/togglebutton';
|
||||||
import {PasswordModule} from 'primeng/password';
|
import {PasswordModule} from 'primeng/password';
|
||||||
import {ToastModule} from 'primeng/toast';
|
import {ToastModule} from 'primeng/toast';
|
||||||
import {LibraryBrowserComponent} from './book/component/library-browser/library-browser.component';
|
|
||||||
import {InfiniteScrollDirective} from 'ngx-infinite-scroll';
|
import {InfiniteScrollDirective} from 'ngx-infinite-scroll';
|
||||||
import {SearchComponent} from './book/component/search/search.component';
|
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 {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({
|
@NgModule({
|
||||||
declarations: [
|
declarations: [
|
||||||
@@ -47,6 +45,7 @@ import { NotificationComponent } from './book/component/notification/notificatio
|
|||||||
ToastModule,
|
ToastModule,
|
||||||
InfiniteScrollDirective,
|
InfiniteScrollDirective,
|
||||||
SearchComponent,
|
SearchComponent,
|
||||||
|
DropdownModule,
|
||||||
],
|
],
|
||||||
providers: [DialogService, MessageService],
|
providers: [DialogService, MessageService],
|
||||||
bootstrap: [AppComponent]
|
bootstrap: [AppComponent]
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
.book-metadata-container {
|
.book-metadata-container {
|
||||||
height: calc(100vh - 10rem); /* Full height minus header and footer */
|
height: calc(100vh - 10rem);
|
||||||
overflow-y: auto; /* Enable vertical scrolling */
|
overflow-y: auto;
|
||||||
transition: transform 0.2s, left 0.2s; /* Smooth transition for movements */
|
transition: transform 0.2s, left 0.2s;
|
||||||
background-color: var(--surface-card);
|
background-color: var(--surface-card);
|
||||||
border-radius: 12px;
|
border-radius: 12px;
|
||||||
padding: 1.5rem 2rem; /* Adjust padding */
|
padding: 1.5rem 2rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.book-metadata {
|
.book-metadata {
|
||||||
@@ -33,7 +33,7 @@
|
|||||||
|
|
||||||
.title-row {
|
.title-row {
|
||||||
display: flex;
|
display: flex;
|
||||||
justify-content: space-between; /* Ensures title and buttons are spaced out */
|
justify-content: space-between;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
margin-bottom: 0.6rem;
|
margin-bottom: 0.6rem;
|
||||||
}
|
}
|
||||||
@@ -46,7 +46,7 @@
|
|||||||
|
|
||||||
.button-group {
|
.button-group {
|
||||||
display: flex;
|
display: flex;
|
||||||
gap: 1.5rem; /* Spacing between buttons */
|
gap: 1.5rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.p-button-sm {
|
.p-button-sm {
|
||||||
|
|||||||
+4
-1
@@ -1,7 +1,10 @@
|
|||||||
<div *ngIf="!isLoading && bookMetadataList.length > 0" class="search-container">
|
<div *ngIf="!isLoading && bookMetadataList.length > 0" class="search-container">
|
||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
<input type="text" [(ngModel)]="searchText" placeholder="Enter book title"/>
|
<input type="text" [(ngModel)]="searchText" placeholder="Enter book title"/>
|
||||||
<p-button severity="success" (click)="populateTitle()">Search</p-button>
|
<p-button
|
||||||
|
severity="success"
|
||||||
|
[disabled]="isButtonDisabled"
|
||||||
|
(click)="populateTitle()">Search</p-button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
+11
-11
@@ -5,13 +5,13 @@
|
|||||||
.input-group {
|
.input-group {
|
||||||
display: flex;
|
display: flex;
|
||||||
justify-content: space-between;
|
justify-content: space-between;
|
||||||
gap: 10px; /* Add space between the text box and button */
|
gap: 10px;
|
||||||
}
|
}
|
||||||
|
|
||||||
input[type="text"] {
|
input[type="text"] {
|
||||||
padding: 10px;
|
padding: 10px;
|
||||||
font-size: 1rem;
|
font-size: 1rem;
|
||||||
width: 90%; /* Adjust width as necessary */
|
width: 90%;
|
||||||
border: 1px solid #ddd;
|
border: 1px solid #ddd;
|
||||||
border-radius: 5px;
|
border-radius: 5px;
|
||||||
}
|
}
|
||||||
@@ -43,22 +43,22 @@ button:focus {
|
|||||||
|
|
||||||
.metadata-header {
|
.metadata-header {
|
||||||
display: flex;
|
display: flex;
|
||||||
gap: 20px; /* Space between image and text */
|
gap: 20px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.book-image {
|
.book-image {
|
||||||
width: 100px; /* Adjust the width as needed */
|
width: 100px;
|
||||||
height: 150px; /* Adjust the height as needed */
|
height: 150px;
|
||||||
object-fit: cover; /* Ensure the image covers the area without distorting */
|
object-fit: cover;
|
||||||
border-radius: 5px;
|
border-radius: 5px;
|
||||||
margin-bottom: 10px; /* Add spacing between the image and the button */
|
margin-bottom: 10px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.select-button {
|
.select-button {
|
||||||
width: auto; /* Button width adjusts to content */
|
width: auto;
|
||||||
padding: 8px 15px; /* Adjust the button padding */
|
padding: 8px 15px;
|
||||||
margin-top: 10px; /* Space between the image and button */
|
margin-top: 10px;
|
||||||
text-align: center; /* Ensure text is centered in the button */
|
text-align: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
.book-metadata {
|
.book-metadata {
|
||||||
|
|||||||
+14
-1
@@ -23,6 +23,7 @@ export class BooksMetadataDialogComponent implements OnInit {
|
|||||||
isLoading = true;
|
isLoading = true;
|
||||||
errorMessage: string | null = null;
|
errorMessage: string | null = null;
|
||||||
searchText: string = '';
|
searchText: string = '';
|
||||||
|
isButtonDisabled = false;
|
||||||
bookId: number = 0;
|
bookId: number = 0;
|
||||||
libraryId: number = 0;
|
libraryId: number = 0;
|
||||||
|
|
||||||
@@ -64,15 +65,27 @@ export class BooksMetadataDialogComponent implements OnInit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
populateTitle() {
|
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({
|
this.bookService.fetchBookMetadataByTerm(this.searchText).subscribe({
|
||||||
next: (metadataList) => {
|
next: (metadataList) => {
|
||||||
this.bookMetadataList = metadataList;
|
this.bookMetadataList = metadataList;
|
||||||
this.isLoading = false;
|
this.isLoading = false;
|
||||||
|
this.isButtonDisabled = false;
|
||||||
},
|
},
|
||||||
error: (error) => {
|
error: (error) => {
|
||||||
this.errorMessage = 'Failed to load metadata';
|
this.errorMessage = 'Failed to load metadata';
|
||||||
this.isLoading = false;
|
this.isLoading = false;
|
||||||
|
this.isButtonDisabled = false;
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ export class DashboardComponent {
|
|||||||
createNewLibrary(event: MouseEvent) {
|
createNewLibrary(event: MouseEvent) {
|
||||||
const buttonRect = (event.target as HTMLElement).getBoundingClientRect();
|
const buttonRect = (event.target as HTMLElement).getBoundingClientRect();
|
||||||
|
|
||||||
const dialogWidthPercentage = 50; // Use percentage-based width
|
const dialogWidthPercentage = 50;
|
||||||
const viewportWidth = window.innerWidth;
|
const viewportWidth = window.innerWidth;
|
||||||
const dialogWidth = (dialogWidthPercentage / 100) * viewportWidth;
|
const dialogWidth = (dialogWidthPercentage / 100) * viewportWidth;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
<div class="card">
|
||||||
|
|
||||||
|
<div class="flex">
|
||||||
|
<p-dropdown
|
||||||
|
[options]="libraries"
|
||||||
|
optionLabel="name"
|
||||||
|
placeholder="Select Library"
|
||||||
|
[(ngModel)]="selectedLibrary">
|
||||||
|
</p-dropdown>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="flex">
|
||||||
|
<p-dropdown
|
||||||
|
[options]="selectedLibrary?.paths"
|
||||||
|
placeholder="Select Upload Location"
|
||||||
|
[(ngModel)]="selectedPath"
|
||||||
|
[disabled]="!selectedLibrary">
|
||||||
|
</p-dropdown>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="flex">
|
||||||
|
<div class="button-container">
|
||||||
|
<p-button
|
||||||
|
severity="info"
|
||||||
|
label="Choose"
|
||||||
|
icon="pi pi-file"
|
||||||
|
(click)="triggerFileInput(fileInput)"
|
||||||
|
[disabled]="!selectedPath || isUploading">
|
||||||
|
</p-button>
|
||||||
|
<span *ngIf="selectedFileName" class="file-name">{{ selectedFileName }}</span>
|
||||||
|
</div>
|
||||||
|
<input
|
||||||
|
type="file"
|
||||||
|
#fileInput
|
||||||
|
style="display: none;"
|
||||||
|
(change)="onFileSelected($event)"
|
||||||
|
[disabled]="!selectedPath || isUploading"/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="flex">
|
||||||
|
<p-button
|
||||||
|
[label]="isUploading ? 'Uploading...' : 'Upload'"
|
||||||
|
[disabled]="!isFormValid() || isUploading"
|
||||||
|
icon="pi pi-cloud-upload"
|
||||||
|
(click)="uploadFile()">
|
||||||
|
</p-button>
|
||||||
|
<div class="error-message-container">
|
||||||
|
<span *ngIf="errorMessage" class="error-message">{{ errorMessage }}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -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 + ".";
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
<div class="layout-wrapper" [ngClass]="containerClass">
|
<div class="layout-wrapper" [ngClass]="containerClass">
|
||||||
<app-topbar></app-topbar>
|
<app-topbar></app-topbar>
|
||||||
<div class="layout-sidebar layout-sidebar-custom">
|
<div class="layout-sidebar layout-sidebar-custom">
|
||||||
<app-sidebar></app-sidebar>
|
<app-sidebar></app-sidebar>
|
||||||
|
</div>
|
||||||
|
<div class="layout-main-container layout-main-container-custom">
|
||||||
|
<div class="layout-main">
|
||||||
|
<router-outlet></router-outlet>
|
||||||
</div>
|
</div>
|
||||||
<div class="layout-main-container layout-main-container-custom">
|
<app-footer></app-footer>
|
||||||
<div class="layout-main" >
|
</div>
|
||||||
<router-outlet ></router-outlet>
|
<app-config></app-config>
|
||||||
</div>
|
<div class="layout-mask"></div>
|
||||||
<app-footer></app-footer>
|
|
||||||
</div>
|
|
||||||
<app-config></app-config>
|
|
||||||
<div class="layout-mask"></div>
|
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import { AppTopBarComponent } from './app.topbar.component';
|
|||||||
import {AppFooterComponent} from "./app.footer.component";
|
import {AppFooterComponent} from "./app.footer.component";
|
||||||
import {AppConfigComponent} from "./config/app.config.component";
|
import {AppConfigComponent} from "./config/app.config.component";
|
||||||
import {NgClass} from '@angular/common';
|
import {NgClass} from '@angular/common';
|
||||||
|
import {ToastModule} from 'primeng/toast';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-layout',
|
selector: 'app-layout',
|
||||||
@@ -16,7 +17,8 @@ import {NgClass} from '@angular/common';
|
|||||||
AppConfigComponent,
|
AppConfigComponent,
|
||||||
AppSidebarComponent,
|
AppSidebarComponent,
|
||||||
AppTopBarComponent,
|
AppTopBarComponent,
|
||||||
NgClass
|
NgClass,
|
||||||
|
ToastModule
|
||||||
],
|
],
|
||||||
templateUrl: './app.layout.component.html'
|
templateUrl: './app.layout.component.html'
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -123,22 +123,16 @@ export class AppMenuitemComponent implements OnInit, OnDestroy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
itemClick(event: Event) {
|
itemClick(event: Event) {
|
||||||
// avoid processing disabled items
|
|
||||||
if (this.item.disabled) {
|
if (this.item.disabled) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// execute command
|
|
||||||
if (this.item.command) {
|
if (this.item.command) {
|
||||||
this.item.command({ originalEvent: event, item: this.item });
|
this.item.command({ originalEvent: event, item: this.item });
|
||||||
}
|
}
|
||||||
|
|
||||||
// toggle active state
|
|
||||||
if (this.item.items) {
|
if (this.item.items) {
|
||||||
this.active = !this.active;
|
this.active = !this.active;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.menuService.onMenuStateChange({ key: this.key });
|
this.menuService.onMenuStateChange({ key: this.key });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,8 +24,10 @@
|
|||||||
tooltipPosition="top">
|
tooltipPosition="top">
|
||||||
<i class="pi pi-plus-circle"></i>
|
<i class="pi pi-plus-circle"></i>
|
||||||
</button>
|
</button>
|
||||||
<button class="p-link layout-topbar-button">
|
<button
|
||||||
<i class="pi pi-user"></i>
|
(click)="openFileUploadDialog()"
|
||||||
|
class="p-link layout-topbar-button">
|
||||||
|
<i class="pi pi-upload"></i>
|
||||||
<span>Profile</span>
|
<span>Profile</span>
|
||||||
</button>
|
</button>
|
||||||
<button class="p-link layout-topbar-button" [routerLink]="'/documentation'">
|
<button class="p-link layout-topbar-button" [routerLink]="'/documentation'">
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
import { Component, ElementRef, OnDestroy, ViewChild } from '@angular/core';
|
import {Component, ElementRef, OnDestroy, ViewChild} from '@angular/core';
|
||||||
import { MenuItem } from 'primeng/api';
|
import {MenuItem} from 'primeng/api';
|
||||||
import { LayoutService } from './service/app.layout.service';
|
import {LayoutService} from './service/app.layout.service';
|
||||||
import { RouterLink } from '@angular/router';
|
import {RouterLink} from '@angular/router';
|
||||||
import { DialogService as PrimeDialogService, DynamicDialogRef } from 'primeng/dynamicdialog';
|
import {DialogService as PrimeDialogService, DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||||
import { LibraryCreatorComponent } from '../library-creator/library-creator.component';
|
import {LibraryCreatorComponent} from '../library-creator/library-creator.component';
|
||||||
import { TooltipModule } from 'primeng/tooltip';
|
import {TooltipModule} from 'primeng/tooltip';
|
||||||
import { FormsModule } from '@angular/forms';
|
import {FormsModule} from '@angular/forms';
|
||||||
import { InputTextModule } from 'primeng/inputtext';
|
import {InputTextModule} from 'primeng/inputtext';
|
||||||
import { SearchComponent } from '../search/search.component';
|
import {SearchComponent} from '../search/search.component';
|
||||||
import { NotificationComponent } from '../notification/notification.component'; // Import the NotificationComponent
|
import {NotificationComponent} from '../notification/notification.component';
|
||||||
|
import {FileUploadComponent} from '../file-upload/file-upload.component';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-topbar',
|
selector: 'app-topbar',
|
||||||
@@ -18,7 +19,7 @@ import { NotificationComponent } from '../notification/notification.component';
|
|||||||
FormsModule,
|
FormsModule,
|
||||||
InputTextModule,
|
InputTextModule,
|
||||||
SearchComponent,
|
SearchComponent,
|
||||||
NotificationComponent // Add NotificationComponent to imports
|
NotificationComponent
|
||||||
],
|
],
|
||||||
templateUrl: './app.topbar.component.html',
|
templateUrl: './app.topbar.component.html',
|
||||||
})
|
})
|
||||||
@@ -30,7 +31,8 @@ export class AppTopBarComponent implements OnDestroy {
|
|||||||
@ViewChild('topbarmenubutton') topbarMenuButton!: ElementRef;
|
@ViewChild('topbarmenubutton') topbarMenuButton!: ElementRef;
|
||||||
@ViewChild('topbarmenu') menu!: ElementRef;
|
@ViewChild('topbarmenu') menu!: ElementRef;
|
||||||
|
|
||||||
constructor(public layoutService: LayoutService, public dialogService: PrimeDialogService) {}
|
constructor(public layoutService: LayoutService, public dialogService: PrimeDialogService) {
|
||||||
|
}
|
||||||
|
|
||||||
openLibraryCreatorDialog(): void {
|
openLibraryCreatorDialog(): void {
|
||||||
this.ref = this.dialogService.open(LibraryCreatorComponent, {
|
this.ref = this.dialogService.open(LibraryCreatorComponent, {
|
||||||
@@ -46,4 +48,14 @@ export class AppTopBarComponent implements OnDestroy {
|
|||||||
this.ref.close();
|
this.ref.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
openFileUploadDialog() {
|
||||||
|
this.ref = this.dialogService.open(FileUploadComponent, {
|
||||||
|
header: 'Upload Book',
|
||||||
|
modal: false,
|
||||||
|
width: '45%',
|
||||||
|
height: '40%',
|
||||||
|
style: { bottom: '20%' }
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,21 +15,20 @@
|
|||||||
height: auto;
|
height: auto;
|
||||||
background-color: var(--surface-card);
|
background-color: var(--surface-card);
|
||||||
border-radius: 8px;
|
border-radius: 8px;
|
||||||
position: relative; /* Positioning container for the buttons and overlay */
|
position: relative;
|
||||||
}
|
}
|
||||||
|
|
||||||
.book-cover {
|
.book-cover {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: 225px; /* Adjust this as per the image height */
|
height: 225px;
|
||||||
object-fit: cover;
|
object-fit: cover;
|
||||||
background-color: #444;
|
background-color: #444;
|
||||||
transition: background-color 0.3s ease, filter 0.3s ease; /* Add transition for blur effect */
|
transition: background-color 0.3s ease, filter 0.3s ease;
|
||||||
position: relative; /* Make this the reference point for the overlay and buttons */
|
position: relative;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Blur and overlay effect */
|
|
||||||
.book-item:hover .book-cover {
|
.book-item:hover .book-cover {
|
||||||
filter: blur(2px); /* Apply blur effect to the image */
|
filter: blur(2px);
|
||||||
}
|
}
|
||||||
|
|
||||||
.book-item:hover .book-cover::after {
|
.book-item:hover .book-cover::after {
|
||||||
@@ -39,8 +38,8 @@
|
|||||||
left: 0;
|
left: 0;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
background: rgba(0, 0, 0, 0.5); /* Semi-transparent black overlay */
|
background: rgba(0, 0, 0, 0.5);
|
||||||
z-index: 0; /* Put overlay behind the buttons */
|
z-index: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.book-info {
|
.book-info {
|
||||||
@@ -50,7 +49,7 @@
|
|||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
justify-content: flex-start;
|
justify-content: flex-start;
|
||||||
flex-grow: 1;
|
flex-grow: 1;
|
||||||
z-index: 1; /* Ensure the text stays above the overlay */
|
z-index: 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
h4 {
|
h4 {
|
||||||
@@ -85,23 +84,22 @@ p {
|
|||||||
font-style: italic;
|
font-style: italic;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* PrimeNG button styling */
|
|
||||||
.view-btn, .read-btn {
|
.view-btn, .read-btn {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
top: 50%;
|
top: 50%;
|
||||||
left: 50%;
|
left: 50%;
|
||||||
transform: translate(-50%, -20%); /* Center buttons within the book-cover */
|
transform: translate(-50%, -20%);
|
||||||
display: none; /* Hide by default */
|
display: none;
|
||||||
margin: -60px 0; /* Add spacing between the buttons */
|
margin: -60px 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.book-item:hover .view-btn {
|
.book-item:hover .view-btn {
|
||||||
display: block; /* Show the View button when the card is hovered */
|
display: block;
|
||||||
}
|
}
|
||||||
|
|
||||||
.book-item:hover .read-btn {
|
.book-item:hover .read-btn {
|
||||||
display: block; /* Show the Read button when the card is hovered */
|
display: block;
|
||||||
top: 75%; /* Slightly adjust the position to be below the View button */
|
top: 75%;
|
||||||
}
|
}
|
||||||
|
|
||||||
.infinite-scroll {
|
.infinite-scroll {
|
||||||
|
|||||||
@@ -4,12 +4,12 @@
|
|||||||
justify-content: center;
|
justify-content: center;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
position: relative;
|
position: relative;
|
||||||
height: 100%; /* Fills the parent container */
|
height: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
.textbox {
|
.textbox {
|
||||||
padding: 10px;
|
padding: 10px;
|
||||||
margin-bottom: 20px; /* Space between textbox and button */
|
margin-bottom: 20px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.button {
|
.button {
|
||||||
@@ -35,10 +35,10 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.mt-3 {
|
.mt-3 {
|
||||||
margin-top: 1rem; /* Adds spacing between the button and the table */
|
margin-top: 1rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.w-100 {
|
.w-100 {
|
||||||
width: 100%; /* Ensures full-width alignment for consistent layout */
|
width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,30 +1,28 @@
|
|||||||
.search-container {
|
.search-container {
|
||||||
margin-left: 20px;
|
margin-left: 20px;
|
||||||
position: relative; /* Ensure the dropdown is positioned relative to the container */
|
position: relative;
|
||||||
}
|
}
|
||||||
|
|
||||||
.search-dropdown {
|
.search-dropdown {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
left: 0; /* Align the dropdown to the left of the container */
|
left: 0;
|
||||||
background-color: var(--surface-overlay); /* Use theme's background color */
|
background-color: var(--surface-overlay);
|
||||||
border: 1px solid var(--surface-border);
|
border: 1px solid var(--surface-border);
|
||||||
border-radius: var(--border-radius);
|
border-radius: var(--border-radius);
|
||||||
z-index: 1000;
|
z-index: 1000;
|
||||||
max-height: 300px; /* Limit the height of the dropdown */
|
max-height: 300px;
|
||||||
overflow-y: auto; /* Scroll if the content exceeds max-height */
|
overflow-y: auto;
|
||||||
width: 600px; /* Ensures the dropdown has the same width as the search bar */
|
width: 600px;
|
||||||
opacity: 0;
|
opacity: 0;
|
||||||
height: 0;
|
height: 0;
|
||||||
transition: opacity 0.3s ease, height 0.3s ease;
|
transition: opacity 0.3s ease, height 0.3s ease;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Show the dropdown */
|
|
||||||
.search-dropdown.show {
|
.search-dropdown.show {
|
||||||
opacity: 1;
|
opacity: 1;
|
||||||
height: auto;
|
height: auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Dropdown item styles */
|
|
||||||
.search-dropdown-item {
|
.search-dropdown-item {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
@@ -32,32 +30,28 @@
|
|||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
transition: background-color 0.3s;
|
transition: background-color 0.3s;
|
||||||
|
|
||||||
/* Hover effect */
|
|
||||||
&:hover {
|
&:hover {
|
||||||
background-color: var(--surface-hover);
|
background-color: var(--surface-hover);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Book cover styling */
|
|
||||||
.search-book-cover {
|
.search-book-cover {
|
||||||
width: 25px; /* Set a fixed width */
|
width: 25px;
|
||||||
height: 37px; /* Set a fixed height */
|
height: 37px;
|
||||||
object-fit: cover; /* Ensure the image fits the area without distortion */
|
object-fit: cover;
|
||||||
max-width: 100px; /* Max width to prevent images from becoming too big */
|
max-width: 100px;
|
||||||
max-height: 150px; /* Max height to maintain consistency */
|
max-height: 150px;
|
||||||
border-radius: 4px; /* Rounded corners */
|
border-radius: 4px;
|
||||||
margin-right: 10px; /* Space between the image and the title */
|
margin-right: 10px;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Book title styling */
|
|
||||||
.search-book-name {
|
.search-book-name {
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
font-weight: 500;
|
font-weight: 500;
|
||||||
color: var(--text-color);
|
color: var(--text-color);
|
||||||
flex-grow: 1; /* Allow the title to take up the remaining space */
|
flex-grow: 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Optionally customize the transition duration or timing */
|
|
||||||
.search-dropdown.show {
|
.search-dropdown.show {
|
||||||
opacity: 1;
|
opacity: 1;
|
||||||
height: auto;
|
height: auto;
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
||||||
|
import { Observable } from 'rxjs';
|
||||||
|
import { catchError } from 'rxjs/operators';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root'
|
||||||
|
})
|
||||||
|
export class FileUploadService {
|
||||||
|
|
||||||
|
private uploadUrl = 'http://localhost:8080/api/v1/files/upload';
|
||||||
|
|
||||||
|
constructor(private http: HttpClient) { }
|
||||||
|
|
||||||
|
uploadFile(file: File, libraryId: string, filePath: string): Observable<any> {
|
||||||
|
const formData: FormData = new FormData();
|
||||||
|
formData.append('file', file);
|
||||||
|
formData.append('libraryId', libraryId);
|
||||||
|
formData.append('filePath', filePath);
|
||||||
|
return this.http.post(this.uploadUrl, formData).pipe(
|
||||||
|
catchError((error) => {
|
||||||
|
throw error;
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import {MessageService} from 'primeng/api';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root'
|
||||||
|
})
|
||||||
|
export class ToastService {
|
||||||
|
|
||||||
|
constructor(private messageService: MessageService) {}
|
||||||
|
|
||||||
|
showSuccess(summary: string, detail: string) {
|
||||||
|
this.messageService.add({ severity: 'success', summary, detail });
|
||||||
|
}
|
||||||
|
|
||||||
|
showError(summary: string, detail: string) {
|
||||||
|
this.messageService.add({ severity: 'error', summary, detail });
|
||||||
|
}
|
||||||
|
|
||||||
|
showInfo(summary: string, detail: string) {
|
||||||
|
this.messageService.add({ severity: 'info', summary, detail });
|
||||||
|
}
|
||||||
|
|
||||||
|
showWarning(summary: string, detail: string) {
|
||||||
|
this.messageService.add({ severity: 'warn', summary, detail });
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user