fix #611 improve no image handling
This commit is contained in:
parent
6af869dfdf
commit
ac85634d25
4 changed files with 100 additions and 29 deletions
|
@ -20,7 +20,7 @@ import java.util.Map;
|
|||
|
||||
public interface ThumbnailGenerator {
|
||||
|
||||
void generate(String url, File outputFile);
|
||||
boolean generate(String url, File outputFile);
|
||||
|
||||
boolean isTarget(Map<String, Object> docMap);
|
||||
|
||||
|
|
|
@ -43,6 +43,8 @@ import org.slf4j.LoggerFactory;
|
|||
public class ThumbnailManager {
|
||||
private static final String DEFAULT_SCREENSHOT_DIR = "/WEB-INF/thumbnails";
|
||||
|
||||
private static final String NOIMAGE_FILE_SUFFIX = ".txt";
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ThumbnailManager.class);
|
||||
|
||||
@Resource
|
||||
|
@ -50,22 +52,24 @@ public class ThumbnailManager {
|
|||
|
||||
protected File baseDir;
|
||||
|
||||
public long shutdownTimeout = 5 * 60 * 1000L; // 5min
|
||||
|
||||
public int thumbnailPathCacheSize = 10;
|
||||
|
||||
private final List<ThumbnailGenerator> generatorList = new ArrayList<>();
|
||||
|
||||
public String imageExtention = "png";
|
||||
private BlockingQueue<ThumbnailTask> thumbnailTaskQueue;
|
||||
|
||||
public int splitSize = 5;
|
||||
|
||||
private final BlockingQueue<ThumbnailTask> thumbnailTaskQueue = new LinkedBlockingQueue<>();
|
||||
|
||||
private boolean generating;
|
||||
private volatile boolean generating;
|
||||
|
||||
private Thread thumbnailGeneratorThread;
|
||||
|
||||
protected int thumbnailPathCacheSize = 10;
|
||||
|
||||
protected String imageExtention = "png";
|
||||
|
||||
protected int splitSize = 5;
|
||||
|
||||
protected int thumbnailTaskQueueSize = 10000;
|
||||
|
||||
protected long noImageExpired = 24 * 60 * 60 * 1000; // 24 hours
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
final String varPath = System.getProperty("fess.var.path");
|
||||
|
@ -90,6 +94,7 @@ public class ThumbnailManager {
|
|||
logger.debug("Thumbnail Directory: " + baseDir.getAbsolutePath());
|
||||
}
|
||||
|
||||
thumbnailTaskQueue = new LinkedBlockingQueue<>(thumbnailTaskQueueSize);
|
||||
generating = true;
|
||||
thumbnailGeneratorThread = new Thread((Runnable) () -> {
|
||||
while (generating) {
|
||||
|
@ -117,8 +122,17 @@ public class ThumbnailManager {
|
|||
if (generator.isTarget(docMap)) {
|
||||
final String url = DocumentUtil.getValue(docMap, fessConfig.getIndexFieldUrl(), String.class);
|
||||
final String path = getImageFilename(docMap);
|
||||
if (!thumbnailTaskQueue.offer(new ThumbnailTask(url, new File(baseDir, path), generator))) {
|
||||
logger.warn("Failed to offer a thumbnail task: " + url + " -> " + path);
|
||||
final File outputFile = new File(baseDir, path);
|
||||
final File noImageFile = new File(outputFile.getAbsolutePath() + NOIMAGE_FILE_SUFFIX);
|
||||
if (!noImageFile.isFile() || System.currentTimeMillis() - noImageFile.lastModified() > noImageExpired) {
|
||||
if (noImageFile.isFile() && !noImageFile.delete()) {
|
||||
logger.warn("Failed to delete " + noImageFile.getAbsolutePath());
|
||||
}
|
||||
if (!thumbnailTaskQueue.offer(new ThumbnailTask(url, outputFile, generator))) {
|
||||
logger.warn("Failed to offer a thumbnail task: " + url + " -> " + path);
|
||||
}
|
||||
} else if (logger.isDebugEnabled()) {
|
||||
logger.debug("No image file exists: " + noImageFile.getAbsolutePath());
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
@ -187,6 +201,7 @@ public class ThumbnailManager {
|
|||
}
|
||||
|
||||
protected static class ThumbnailTask {
|
||||
|
||||
String url;
|
||||
|
||||
File outputFile;
|
||||
|
@ -200,7 +215,9 @@ public class ThumbnailManager {
|
|||
}
|
||||
|
||||
public void generate() {
|
||||
generator.generate(url, outputFile);
|
||||
if (!generator.generate(url, outputFile)) {
|
||||
new File(outputFile.getAbsolutePath() + NOIMAGE_FILE_SUFFIX).setLastModified(System.currentTimeMillis());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -243,4 +260,24 @@ public class ThumbnailManager {
|
|||
|
||||
}
|
||||
|
||||
public void setThumbnailPathCacheSize(int thumbnailPathCacheSize) {
|
||||
this.thumbnailPathCacheSize = thumbnailPathCacheSize;
|
||||
}
|
||||
|
||||
public void setImageExtention(String imageExtention) {
|
||||
this.imageExtention = imageExtention;
|
||||
}
|
||||
|
||||
public void setSplitSize(int splitSize) {
|
||||
this.splitSize = splitSize;
|
||||
}
|
||||
|
||||
public void setThumbnailTaskQueueSize(int thumbnailTaskQueueSize) {
|
||||
this.thumbnailTaskQueueSize = thumbnailTaskQueueSize;
|
||||
}
|
||||
|
||||
public void setNoImageExpired(long noImageExpired) {
|
||||
this.noImageExpired = noImageExpired;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -57,7 +57,7 @@ public class CommandGenerator extends BaseThumbnailGenerator {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void generate(final String url, final File outputFile) {
|
||||
public boolean generate(final String url, final File outputFile) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Generate Thumbnail: " + url);
|
||||
}
|
||||
|
@ -66,7 +66,7 @@ public class CommandGenerator extends BaseThumbnailGenerator {
|
|||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("The thumbnail file exists: " + outputFile.getAbsolutePath());
|
||||
}
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
|
||||
final File parentFile = outputFile.getParentFile();
|
||||
|
@ -75,7 +75,7 @@ public class CommandGenerator extends BaseThumbnailGenerator {
|
|||
}
|
||||
if (!parentFile.isDirectory()) {
|
||||
logger.warn("Not found: " + parentFile.getAbsolutePath());
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
final String outputPath = outputFile.getAbsolutePath();
|
||||
|
@ -133,11 +133,13 @@ public class CommandGenerator extends BaseThumbnailGenerator {
|
|||
if (outputFile.delete()) {
|
||||
logger.info("Deleted: " + outputFile.getAbsolutePath());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Thumbnail File: " + outputPath);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected static class ProcessDestroyer extends TimerTask {
|
||||
|
|
|
@ -39,17 +39,19 @@ public class WebDriverGenerator extends BaseThumbnailGenerator {
|
|||
|
||||
private static final Logger logger = LoggerFactory.getLogger(WebDriverGenerator.class);
|
||||
|
||||
public WebDriver webDriver;
|
||||
protected WebDriver webDriver;
|
||||
|
||||
public Capabilities webDriverCapabilities;
|
||||
protected Capabilities webDriverCapabilities;
|
||||
|
||||
public int windowWidth = 1200;
|
||||
protected int windowWidth = 1200;
|
||||
|
||||
public int windowHeight = 800;
|
||||
protected int windowHeight = 800;
|
||||
|
||||
public int thumbnailWidth = 160;
|
||||
protected int thumbnailWidth = 160;
|
||||
|
||||
public String imageFormatName = "png";
|
||||
protected int thumbnailHeight = 160;
|
||||
|
||||
protected String imageFormatName = "png";
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
|
@ -87,7 +89,7 @@ public class WebDriverGenerator extends BaseThumbnailGenerator {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void generate(final String url, final File outputFile) {
|
||||
public boolean generate(final String url, final File outputFile) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Generate Thumbnail: " + url);
|
||||
}
|
||||
|
@ -96,7 +98,7 @@ public class WebDriverGenerator extends BaseThumbnailGenerator {
|
|||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("The thumbnail file exists: " + outputFile.getAbsolutePath());
|
||||
}
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
|
||||
final File parentFile = outputFile.getParentFile();
|
||||
|
@ -105,15 +107,17 @@ public class WebDriverGenerator extends BaseThumbnailGenerator {
|
|||
}
|
||||
if (!parentFile.isDirectory()) {
|
||||
logger.warn("Not found: " + parentFile.getAbsolutePath());
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (webDriver instanceof TakesScreenshot) {
|
||||
webDriver.get(url);
|
||||
final File thumbnail = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
|
||||
convert(thumbnail, outputFile);
|
||||
return true;
|
||||
} else {
|
||||
logger.warn("WebDriver is not instance of TakesScreenshot: " + webDriver);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -128,10 +132,10 @@ public class WebDriverGenerator extends BaseThumbnailGenerator {
|
|||
protected void convert(final File inputFile, final File outputFile) {
|
||||
try {
|
||||
final BufferedImage image = loadImage(inputFile);
|
||||
final int thumbnailHeight = thumbnailWidth * image.getHeight() / windowWidth;
|
||||
final int height = thumbnailWidth * image.getHeight() / windowWidth;
|
||||
final BufferedImage thumbnailImage = new BufferedImage(thumbnailWidth, thumbnailHeight, image.getType());
|
||||
thumbnailImage.getGraphics().drawImage(image.getScaledInstance(thumbnailWidth, thumbnailHeight, Image.SCALE_AREA_AVERAGING), 0,
|
||||
0, thumbnailWidth, thumbnailHeight, null);
|
||||
thumbnailImage.getGraphics().drawImage(image.getScaledInstance(thumbnailWidth, height, Image.SCALE_AREA_AVERAGING), 0, 0,
|
||||
thumbnailWidth, thumbnailHeight, null);
|
||||
|
||||
ImageIO.write(thumbnailImage, imageFormatName, outputFile);
|
||||
} catch (final Exception e) {
|
||||
|
@ -146,4 +150,32 @@ public class WebDriverGenerator extends BaseThumbnailGenerator {
|
|||
}
|
||||
}
|
||||
|
||||
public void setWebDriver(WebDriver webDriver) {
|
||||
this.webDriver = webDriver;
|
||||
}
|
||||
|
||||
public void setWebDriverCapabilities(Capabilities webDriverCapabilities) {
|
||||
this.webDriverCapabilities = webDriverCapabilities;
|
||||
}
|
||||
|
||||
public void setWindowWidth(int windowWidth) {
|
||||
this.windowWidth = windowWidth;
|
||||
}
|
||||
|
||||
public void setWindowHeight(int windowHeight) {
|
||||
this.windowHeight = windowHeight;
|
||||
}
|
||||
|
||||
public void setThumbnailWidth(int thumbnailWidth) {
|
||||
this.thumbnailWidth = thumbnailWidth;
|
||||
}
|
||||
|
||||
public void setImageFormatName(String imageFormatName) {
|
||||
this.imageFormatName = imageFormatName;
|
||||
}
|
||||
|
||||
public void setThumbnailHeight(int thumbnailHeight) {
|
||||
this.thumbnailHeight = thumbnailHeight;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue