Skip to content

Commit

Permalink
[Issue #175] Return an image placeholder when one isn't found on the …
Browse files Browse the repository at this point in the history
…backend (#177)

* [Issue #175] Return the missing page image if a comic page could not be loaded.

* [Issue #175] Return the missing image placeholder when no cover is found during import.
  • Loading branch information
mcpierce committed Jun 21, 2020
1 parent 165dead commit a230e8f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
Expand Up @@ -28,6 +28,7 @@
import java.security.NoSuchAlgorithmException;
import javax.imageio.ImageIO;
import javax.persistence.*;
import org.apache.commons.io.IOUtils;
import org.codehaus.plexus.util.StringUtils;
import org.comixed.adaptors.archive.ArchiveAdaptorException;
import org.comixed.views.View.ComicList;
Expand Down Expand Up @@ -183,9 +184,18 @@ public byte[] getContent() {
return this.comic.archiveType.getArchiveAdaptor().loadSingleFile(this.comic, this.filename);
}
} catch (ArchiveAdaptorException error) {
this.logger.warn(
this.logger.error(
"failed to load entry: " + this.filename + " comic=" + this.comic.getFilename(), error);
}

// if we're here then return the missing page image since we didn't load the one we wanted
try {
return IOUtils.toByteArray(this.getClass().getResourceAsStream("/images/missing.png"));
} catch (IOException error) {
this.logger.error("failed to load missing page image", error);
}

// if we're here, we have nothing to return
return null;
}

Expand Down
Expand Up @@ -22,6 +22,7 @@
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.comixed.adaptors.archive.ArchiveAdaptorException;
import org.comixed.controller.library.ComicController;
import org.comixed.handlers.ComicFileHandlerException;
Expand Down Expand Up @@ -89,15 +90,30 @@ private void getAllFilesUnder(File root, List<FileDetails> result) throws IOExce
}

@RequestMapping(value = "/import/cover", method = RequestMethod.GET)
public byte[] getImportFileCover(@RequestParam("filename") String filename)
throws ComicFileHandlerException, ArchiveAdaptorException {
public byte[] getImportFileCover(@RequestParam("filename") String filename) {
// for some reason, during development, this value ALWAYS had a trailing
// space...
filename = filename.trim();

this.logger.info("Getting cover image for archive: filename={}", filename);

return this.fileService.getImportFileCover(filename);
byte[] result = null;

try {
result = this.fileService.getImportFileCover(filename);
} catch (ComicFileHandlerException | ArchiveAdaptorException error) {
this.logger.error("Failed to load cover from import file", error);
}

if (result == null) {
try {
result = IOUtils.toByteArray(this.getClass().getResourceAsStream("/images/missing.png"));
} catch (IOException error) {
this.logger.error("Failed to load the missing page image", error);
}
}

return result;
}

@RequestMapping(value = "/import/status", method = RequestMethod.GET)
Expand Down
Expand Up @@ -57,7 +57,7 @@ public class FileControllerTest {
@Mock private List<FileDetails> fileDetailsList;

@Test
public void testGetImportFileCover() throws ComicFileHandlerException, ArchiveAdaptorException {
public void testGetImportFileCover() throws ArchiveAdaptorException, ComicFileHandlerException {
Mockito.when(fileService.getImportFileCover(Mockito.anyString())).thenReturn(IMAGE_CONTENT);

final byte[] result = controller.getImportFileCover(COMIC_ARCHIVE);
Expand Down

0 comments on commit a230e8f

Please sign in to comment.