1 /*
2 * Copyright 2012-2025 CodeLibs Project and the Others.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13 * either express or implied. See the License for the specific language
14 * governing permissions and limitations under the License.
15 */
16 package org.codelibs.fess.job;
17
18 import org.apache.logging.log4j.LogManager;
19 import org.apache.logging.log4j.Logger;
20 import org.codelibs.fess.util.ComponentUtil;
21
22 /**
23 * Job for purging expired thumbnail files from the system.
24 * This job removes thumbnail files that have exceeded their configured expiration time
25 * to prevent disk space from being consumed by old thumbnails.
26 */
27 public class PurgeThumbnailJob {
28 /** Logger instance for this class */
29 private static final Logger logger = LogManager.getLogger(PurgeThumbnailJob.class);
30
31 /**
32 * Default constructor for PurgeThumbnailJob.
33 * Creates a new instance of the thumbnail purging job with default expiry time (30 days).
34 */
35 public PurgeThumbnailJob() {
36 // Default constructor
37 }
38
39 /** Expiration time for thumbnails in milliseconds (default: 30 days) */
40 private long expiry = 30 * 24 * 60 * 60 * 1000L;
41
42 /**
43 * Executes the thumbnail purging job.
44 * Removes thumbnail files that have exceeded the configured expiration time.
45 *
46 * @return a string containing the execution result with the number of deleted files or error message
47 */
48 public String execute() {
49 try {
50 final long count = ComponentUtil.getThumbnailManager().purge(getExpiry());
51 return "Deleted " + count + " thumbnail files.";
52 } catch (final Exception e) {
53 logger.error("Failed to purge thumbnails.", e);
54 return e.getMessage();
55 }
56 }
57
58 /**
59 * Gets the expiration time for thumbnails.
60 *
61 * @return the expiration time in milliseconds
62 */
63 public long getExpiry() {
64 return expiry;
65 }
66
67 /**
68 * Sets the expiration time for thumbnails.
69 *
70 * @param expiry the expiration time in milliseconds (must be positive)
71 * @return this PurgeThumbnailJob instance for method chaining
72 */
73 public PurgeThumbnailJob expiry(final long expiry) {
74 if (expiry > 0) {
75 this.expiry = expiry;
76 }
77 return this;
78 }
79 }