Implement file downloading functionality

This commit is contained in:
aditya.chandel
2025-01-16 14:27:14 -07:00
parent 3ced7472e9
commit c7069132b6
9 changed files with 85 additions and 2 deletions
@@ -12,6 +12,7 @@ public class WebConfig implements WebMvcConfigurer {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("*")
.exposedHeaders("Content-Disposition")
.allowedHeaders("*");
}
}
@@ -73,4 +73,9 @@ public class BookController {
booksService.updateReadProgress(request);
return ResponseEntity.noContent().build();
}
@GetMapping("/{bookId}/download")
public ResponseEntity<Resource> downloadFile(@PathVariable("bookId") Long bookId) {
return booksService.prepareFileForDownload(bookId);
}
}
@@ -22,7 +22,8 @@ public enum ApiError {
SCHEDULE_REFRESH_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to schedule metadata refresh job. Error: %s"),
ANOTHER_METADATA_JOB_RUNNING(HttpStatus.CONFLICT, "A metadata refresh job is currently running. Please wait for it to complete before initiating a new one."),
METADATA_SOURCE_NOT_IMPLEMENT_OR_DOES_NOT_EXIST(HttpStatus.BAD_REQUEST, "Metadata source not implement or does not exist"),
INVALID_REFRESH_TYPE(HttpStatus.BAD_REQUEST, "The refresh type is invalid"),;
FAILED_TO_DOWNLOAD_FILE(HttpStatus.INTERNAL_SERVER_ERROR, "Error while downloading file, bookId: %s"),
INVALID_REFRESH_TYPE(HttpStatus.BAD_REQUEST, "The refresh type is invalid");
private final HttpStatus status;
private final String message;
@@ -20,14 +20,19 @@ import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Instant;
import java.util.List;
import java.util.Set;
@@ -145,4 +150,23 @@ public class BooksService {
}
bookRepository.save(book);
}
public ResponseEntity<Resource> prepareFileForDownload(Long bookId) {
try {
BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
String filePath = bookEntity.getPath();
Path file = Paths.get(filePath).toAbsolutePath().normalize();
Resource resource = new UrlResource(file.toUri());
String contentType = Files.probeContentType(file);
if (contentType == null) {
contentType = "application/octet-stream";
}
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(contentType))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFileName() + "\"")
.body(resource);
} catch (Exception e) {
throw ApiError.FAILED_TO_DOWNLOAD_FILE.createException(bookId);
}
}
}