forked from comixed/comixed
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Issue comixed#27] Add the PageCacheService class and tests.
- Loading branch information
Showing
5 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
comixed-services/src/main/java/org/comixed/ComiXedTestContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.comixed; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.test.context.TestPropertySource; | ||
|
||
@Configuration | ||
@TestPropertySource("classpath:application.properties") | ||
public class ComiXedTestContext {} |
79 changes: 79 additions & 0 deletions
79
comixed-services/src/main/java/org/comixed/service/library/PageCacheService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* ComiXed - A digital comic book library management application. | ||
* Copyright (C) 2019, The ComiXed Project | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses> | ||
*/ | ||
|
||
package org.comixed.service.library; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import org.apache.commons.io.IOUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class PageCacheService { | ||
protected final Logger logger = LoggerFactory.getLogger(this.getClass()); | ||
|
||
@Value("${comixed.images.cache.location}") | ||
private String cacheDirectory; | ||
|
||
public byte[] findByHash(final String hash) { | ||
this.logger.debug("Searching for cached image: hash={}", hash); | ||
|
||
final File file = this.getFileForHash(hash); | ||
if (file.exists() && !file.isDirectory()) { | ||
this.logger.debug("Loading cached image content: {} bytes", file.length()); | ||
try { | ||
return IOUtils.readFully(new FileInputStream(file), (int) file.length()); | ||
} catch (Exception error) { | ||
this.logger.error("Failed to load cached image", error); | ||
} | ||
} | ||
|
||
this.logger.debug("No image in cache"); | ||
return null; | ||
} | ||
|
||
File getFileForHash(final String hash) { | ||
if (hash.length() != 32) { | ||
return null; | ||
} | ||
|
||
final String path = | ||
this.cacheDirectory | ||
+ File.separator | ||
+ hash.substring(0, 8) | ||
+ File.separator | ||
+ hash.substring(8, 16) | ||
+ File.separator | ||
+ hash.substring(16, 24) | ||
+ File.separator | ||
+ hash.substring(24, 32); | ||
return new File(path); | ||
} | ||
|
||
public void saveByHash(final String hash, final byte[] content) throws IOException { | ||
this.logger.debug("Saving image to cache: hash={}", hash); | ||
final File file = this.getFileForHash(hash); | ||
file.getParentFile().mkdirs(); | ||
IOUtils.write(content, new FileOutputStream(file, false)); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
comixed-services/src/test/java/org/comixed/service/library/PageCacheServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* ComiXed - A digital comic book library management application. | ||
* Copyright (C) 2019, The ComiXed Project | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses> | ||
*/ | ||
|
||
package org.comixed.service.library; | ||
|
||
import static junit.framework.TestCase.*; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.TestPropertySource; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
@RunWith(SpringRunner.class) | ||
@ContextConfiguration(classes = {PageCacheService.class}) | ||
@TestPropertySource("classpath:application.properties") | ||
public class PageCacheServiceTest { | ||
private static final String TEST_MISSING_PAGE_HASH = "4C6DD238138491B89A3DB9BC6E3A3E2D"; | ||
private static final String TEST_PAGE_HASH = "D5397C1B6053B093CB133CA3B7081C9E"; | ||
|
||
@Autowired private PageCacheService pageCacheService; | ||
|
||
@Before | ||
public void setUp() { | ||
// clean up any remnant | ||
final File file = pageCacheService.getFileForHash(TEST_MISSING_PAGE_HASH); | ||
if (file.exists()) { | ||
file.delete(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testFindByHashNotCached() throws IOException { | ||
final byte[] result = pageCacheService.findByHash(TEST_MISSING_PAGE_HASH); | ||
|
||
assertNull(result); | ||
} | ||
|
||
@Test | ||
public void testFindByHash() throws IOException { | ||
final byte[] result = pageCacheService.findByHash(TEST_PAGE_HASH); | ||
|
||
assertNotNull(result); | ||
} | ||
|
||
@Test | ||
public void testSaveByHash() throws IOException { | ||
pageCacheService.saveByHash(TEST_MISSING_PAGE_HASH, TEST_MISSING_PAGE_HASH.getBytes()); | ||
|
||
assertTrue(pageCacheService.getFileForHash(TEST_MISSING_PAGE_HASH).exists()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+56 KB
comixed-services/src/test/resources/image-cache/D5397C1B/6053B093/CB133CA3/B7081C9E
Binary file not shown.