Skip to content

Commit

Permalink
Fixed loading comic pages from OPDS [#738]
Browse files Browse the repository at this point in the history
Had to also disable storing the response body in the database
as it was causing a lot of performance issues.
  • Loading branch information
mcpierce committed May 10, 2021
1 parent 29a2f93 commit e63ae26
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
Expand Up @@ -64,7 +64,6 @@ public void loadContent(
page.setHash(hash);
page.setWidth(width);
page.setHeight(height);
page.setContent(content);
page.setComic(comic);
comic.getPages().add(page);
page.setPageNumber(comic.getPages().size());
Expand Down
Expand Up @@ -111,8 +111,6 @@ public class Page {
@Getter
private boolean blocked;

@Transient @Getter @Setter private byte[] content;

/**
* Returns the offset's index within the comic.
*
Expand Down
Expand Up @@ -56,14 +56,14 @@ public class AuditableEndpointAspect {
*/
@Around("@annotation(org.comixedproject.auditlog.AuditableEndpoint)")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
Throwable error = null;
Throwable thrownError = null;
Object response = null;

final Date started = new Date();
try {
response = joinPoint.proceed();
} catch (Throwable throwable) {
error = throwable;
thrownError = throwable;
}
final Date ended = new Date();
final WebAuditLogEntry entry = new WebAuditLogEntry();
Expand All @@ -82,17 +82,19 @@ public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
entry.setEndTime(ended);
entry.setRequestContent(
new String(((ContentCachingRequestWrapper) request).getContentAsByteArray()));
// if (response != null) {
// try {
// entry.setResponseContent(this.objectMapper.writeValueAsString(response));
// } catch (JsonProcessingException error) {
// log.error("Failed to encode object as JSON", error);
// }
// }

if (response != null) {
log.debug("Storing response content");
entry.setResponseContent(this.objectMapper.writeValueAsString(response));
}

if (error != null) {
if (thrownError != null) {
log.debug("Storing method exception stacktrace");
final StringWriter stringWriter = new StringWriter();
final PrintWriter printWriter = new PrintWriter(stringWriter);
error.printStackTrace(printWriter);
thrownError.printStackTrace(printWriter);
entry.setException(stringWriter.toString());
entry.setSuccessful(false);
} else {
Expand All @@ -101,7 +103,7 @@ public Object around(ProceedingJoinPoint joinPoint) throws Throwable {

this.webAuditLogService.save(entry);

if (error != null) throw error;
if (thrownError != null) throw thrownError;
return response;
}
}
Expand Up @@ -28,7 +28,9 @@
import lombok.extern.log4j.Log4j2;
import marvin.image.MarvinImage;
import marvinplugins.MarvinPluginCollection;
import org.comixedproject.adaptors.archive.ArchiveAdaptorException;
import org.comixedproject.auditlog.AuditableEndpoint;
import org.comixedproject.handlers.ComicFileHandler;
import org.comixedproject.model.comic.Comic;
import org.comixedproject.model.comic.Page;
import org.comixedproject.repositories.comic.ComicRepository;
Expand All @@ -54,6 +56,7 @@ public class OPDSController {
@Autowired private ComicRepository comicRepository;
@Autowired private ReadingListService readingListService;
@Autowired private FileTypeIdentifier fileTypeIdentifier;
@Autowired private ComicFileHandler comicFileHandler;

@ResponseBody
@GetMapping(value = "/opds-comics", produces = MediaType.APPLICATION_XML_VALUE)
Expand Down Expand Up @@ -136,14 +139,15 @@ public ResponseEntity<byte[]> getImageInComicByIndex(
@PathVariable("id") long id,
@PathVariable("index") int index,
@PathVariable("maxWidth") int maxWidth)
throws IOException {
throws IOException, ArchiveAdaptorException {
log.debug("Getting the image for comic: id={} index={}", id, index);

Optional<Comic> record = this.comicRepository.findById(id);

if (record.isPresent() && (index < record.get().getPageCount())) {
Page page = record.get().getPage(index);
byte[] content = page.getContent();
var adaptor = this.comicFileHandler.getArchiveAdaptorFor(page.getComic().getArchiveType());
var content = adaptor.loadSingleFile(page.getComic(), page.getFilename());

ByteArrayInputStream bais = new ByteArrayInputStream(content);
BufferedImage image = ImageIO.read(bais);
Expand Down

0 comments on commit e63ae26

Please sign in to comment.