fix #610 purge old thumbnail files
This commit is contained in:
parent
53664259b7
commit
663e018049
6 changed files with 118 additions and 0 deletions
|
@ -0,0 +1,11 @@
|
|||
package org.codelibs.fess.exception;
|
||||
|
||||
public class JobProcessingException extends FessSystemException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public JobProcessingException(Throwable e) {
|
||||
super(e);
|
||||
}
|
||||
|
||||
}
|
30
src/main/java/org/codelibs/fess/job/PurgeThumbnailJob.java
Normal file
30
src/main/java/org/codelibs/fess/job/PurgeThumbnailJob.java
Normal file
|
@ -0,0 +1,30 @@
|
|||
package org.codelibs.fess.job;
|
||||
|
||||
import org.codelibs.fess.util.ComponentUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class PurgeThumbnailJob {
|
||||
private static final Logger logger = LoggerFactory.getLogger(PurgeThumbnailJob.class);
|
||||
|
||||
private long expiry;
|
||||
|
||||
public String execute() {
|
||||
try {
|
||||
final long count = ComponentUtil.getThumbnailManager().purge(getExpiry());
|
||||
return "Deleted " + count + " thumbnail files.";
|
||||
} catch (final Exception e) {
|
||||
logger.error("Failed to purge user info.", e);
|
||||
return e.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public long getExpiry() {
|
||||
return expiry;
|
||||
}
|
||||
|
||||
public PurgeThumbnailJob expiry(long expiry) {
|
||||
this.expiry = expiry;
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -16,6 +16,12 @@
|
|||
package org.codelibs.fess.thumbnail;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.FileVisitor;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
@ -33,6 +39,7 @@ import org.codelibs.core.collection.LruHashMap;
|
|||
import org.codelibs.core.lang.StringUtil;
|
||||
import org.codelibs.fess.Constants;
|
||||
import org.codelibs.fess.exception.FessSystemException;
|
||||
import org.codelibs.fess.exception.JobProcessingException;
|
||||
import org.codelibs.fess.mylasta.direction.FessConfig;
|
||||
import org.codelibs.fess.util.ComponentUtil;
|
||||
import org.codelibs.fess.util.DocumentUtil;
|
||||
|
@ -200,6 +207,65 @@ public class ThumbnailManager {
|
|||
}
|
||||
}
|
||||
|
||||
public long purge(final long expiry) {
|
||||
try {
|
||||
final FilePurgeVisitor visitor = new FilePurgeVisitor(expiry);
|
||||
Files.walkFileTree(baseDir.toPath(), visitor);
|
||||
return visitor.getCount();
|
||||
} catch (final Exception e) {
|
||||
throw new JobProcessingException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static class FilePurgeVisitor implements FileVisitor<Path> {
|
||||
|
||||
private long expiry;
|
||||
|
||||
private long count;
|
||||
|
||||
FilePurgeVisitor(final long expiry) {
|
||||
this.expiry = expiry;
|
||||
}
|
||||
|
||||
public long getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException {
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
|
||||
if (System.currentTimeMillis() - Files.getLastModifiedTime(file).toMillis() > expiry) {
|
||||
Files.delete(file);
|
||||
count++;
|
||||
}
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult visitFileFailed(final Path file, final IOException e) throws IOException {
|
||||
if (e != null) {
|
||||
logger.warn("I/O exception on " + file, e);
|
||||
}
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult postVisitDirectory(final Path dir, final IOException e) throws IOException {
|
||||
if (e != null) {
|
||||
logger.warn("I/O exception on " + dir, e);
|
||||
}
|
||||
if (dir.toFile().list().length == 0) {
|
||||
Files.delete(dir);
|
||||
}
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected static class ThumbnailTask {
|
||||
|
||||
String url;
|
||||
|
|
|
@ -60,6 +60,7 @@ import org.codelibs.fess.job.JobExecutor;
|
|||
import org.codelibs.fess.ldap.LdapManager;
|
||||
import org.codelibs.fess.mylasta.direction.FessConfig;
|
||||
import org.codelibs.fess.sso.SsoManager;
|
||||
import org.codelibs.fess.thumbnail.ThumbnailManager;
|
||||
import org.lastaflute.core.message.MessageManager;
|
||||
import org.lastaflute.di.core.SingletonLaContainer;
|
||||
import org.lastaflute.di.core.factory.SingletonLaContainerFactory;
|
||||
|
@ -74,6 +75,8 @@ public final class ComponentUtil {
|
|||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ComponentUtil.class);
|
||||
|
||||
private static final String THUMBNAIL_MANAGER = "thumbnailManager";
|
||||
|
||||
private static final String SSO_MANAGER = "ssoManager";
|
||||
|
||||
private static final String PERMISSION_HELPER = "permissionHelper";
|
||||
|
@ -369,6 +372,10 @@ public final class ComponentUtil {
|
|||
return getComponent(SSO_MANAGER);
|
||||
}
|
||||
|
||||
public static ThumbnailManager getThumbnailManager() {
|
||||
return getComponent(THUMBNAIL_MANAGER);
|
||||
}
|
||||
|
||||
public static CrawlerClientFactory getCrawlerClientFactory() {
|
||||
return getComponent(CrawlerClientFactory.class);
|
||||
}
|
||||
|
|
|
@ -8,3 +8,5 @@
|
|||
{"name":"Log Purger","target":"all","cronExpression":"0 0 * * *","scriptType":"groovy","scriptData":"return container.getComponent(\"purgeLogJob\").execute();","jobLogging":false,"crawler":false,"available":true,"sortOrder":4,"createdBy":"system","createdTime":0,"updatedBy":"system","updatedTime":0}
|
||||
{"index":{"_index":".fess_config","_type":"scheduled_job","_id":"doc_purger"}}
|
||||
{"name":"Doc Purger","target":"all","cronExpression":"* * * * *","scriptType":"groovy","scriptData":"return container.getComponent(\"purgeDocJob\").execute();","jobLogging":false,"crawler":false,"available":true,"sortOrder":5,"createdBy":"system","createdTime":0,"updatedBy":"system","updatedTime":0}
|
||||
{"index":{"_index":".fess_config","_type":"scheduled_job","_id":"thumbnail_purger"}}
|
||||
{"name":"Thumbnail Purger","target":"all","cronExpression":"0 0 * * *","scriptType":"groovy","scriptData":"return container.getComponent(\"purgeThumbnailJob\").expiry(30 * 24 * 60 * 60 * 1000).execute();","jobLogging":true,"crawler":false,"available":true,"sortOrder":6,"createdBy":"system","createdTime":0,"updatedBy":"system","updatedTime":0}
|
||||
|
|
|
@ -16,4 +16,6 @@
|
|||
</component>
|
||||
<component name="purgeLogJob" class="org.codelibs.fess.job.PurgeLogJob" instance="prototype">
|
||||
</component>
|
||||
<component name="purgeThumbnailJob" class="org.codelibs.fess.job.PurgeThumbnailJob" instance="prototype">
|
||||
</component>
|
||||
</components>
|
||||
|
|
Loading…
Add table
Reference in a new issue