Skip to content

Commit

Permalink
replacing log4j with slf4j api. code cleanup and improvements. docume…
Browse files Browse the repository at this point in the history
…ntation improvements.
  • Loading branch information
Matt committed Sep 15, 2024
1 parent ab988b2 commit dbb9ffa
Show file tree
Hide file tree
Showing 28 changed files with 183 additions and 199 deletions.
5 changes: 2 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ dependencies {
implementation "org.apache.poi:poi:${apachepoi_ver}"
implementation "org.apache.poi:poi-scratchpad:${apachepoi_ver}"
implementation "commons-io:commons-io:${commonsio_ver}"
implementation "org.apache.logging.log4j:log4j-api:${log4j_ver}"
implementation "org.apache.logging.log4j:log4j-core:${log4j_ver}"
implementation "org.slf4j:slf4j-api:${slf4j_ver}"
implementation "org.jodconverter:jodconverter-local:${jod_ver}"
implementation "org.apache.pdfbox:pdfbox:${pdfbox_ver}"
implementation "org.bytedeco:javacv:${javacv_ver}"
Expand Down Expand Up @@ -73,7 +72,7 @@ publishing {

groupId = 'io.github.makbn'
artifactId = 'jthumbnail'
version = '2.1.0'
version = '2.2.0'
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apachepoi_ver=5.2.2
commonsio_ver=2.11.0
jod_ver=4.4.2
pdfbox_ver=2.0.26
log4j_ver=2.18.0
slf4j_ver=2.0.16
javacv_ver=1.5.7
opencv_ver=4.5.5-1.5.7
thumb_ver=0.4.17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public ResponseEntity<JThumbnailApiResponse<String>> checkThumbnailStatus(@Param
)
)
})
public @ResponseBody ResponseEntity<Resource> downloadThumbnail(@Parameter(
public ResponseEntity<Resource> downloadThumbnail(@Parameter(
name = "uid",
description = "The unique ID of the thumbnail to download",
required = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public String requestThumbnail(@NonNull MultipartFile multipartFile) throws IOEx
File file = createTempFile(multipartFile);
CompletableFuture<Thumbnail> completableFuture = new CompletableFuture<>();
String uid = UUID.randomUUID().toString();
ThumbnailCandidate candidate = new ThumbnailCandidate(file, uid);
ThumbnailCandidate candidate = ThumbnailCandidate.of(file, uid);
waitingMap.put(uid, completableFuture);
temporaryFilesMap.put(uid, file);
// after keeping the references, let's create the thumbnail
Expand Down
28 changes: 16 additions & 12 deletions src/main/java/io/github/makbn/jthumbnail/core/JThumbnailer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
import lombok.experimental.FieldDefaults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

import java.io.Closeable;
import java.io.File;

@Configuration
@Component
@EnableAsync
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
public class JThumbnailer implements Closeable {
Expand All @@ -32,21 +32,15 @@ public JThumbnailer(ThumbnailerManager manager, ApplicationEventPublisher events
this.typeDetector = new MimeTypeDetector();
}

@Async("async_thread_pool_task_executor")
@Async("asyncThreadPoolTaskExecutor")
public void run(ThumbnailCandidate candidate, ThumbnailListener listener) {
try {
candidate.setThumbExt(typeDetector.getOutputExt(candidate.getFile()));
File out = manager.createThumbnail(candidate.getFile(), candidate.getThumbExt());
listener.onThumbnailReady(candidate.getUid(), out);
} catch (ThumbnailerRuntimeException | ThumbnailerException re) {
listener.onThumbnailFailed(candidate.getUid(), re.getMessage(), 500);
}
internalRun(candidate, listener);

}

@Async("async_thread_pool_task_executor")
@Async("asyncThreadPoolTaskExecutor")
public void run(ThumbnailCandidate candidate) {
run(candidate, new ThumbnailListener() {
this.internalRun(candidate, new ThumbnailListener() {
@Override
public void onThumbnailReady(String hash, File thumbnail) {
events.publishEvent(ThumbnailEvent.builder()
Expand All @@ -67,6 +61,16 @@ public void onThumbnailFailed(String hash, String message, int code) {
});
}

private void internalRun(ThumbnailCandidate candidate, ThumbnailListener listener) {
try {
candidate.setThumbExt(typeDetector.getOutputExt(candidate.getFile()));
File out = manager.createThumbnail(candidate.getFile(), candidate.getThumbExt());
listener.onThumbnailReady(candidate.getUid(), out);
} catch (ThumbnailerRuntimeException | ThumbnailerException re) {
listener.onThumbnailFailed(candidate.getUid(), re.getMessage(), 500);
}
}

@Override
public synchronized void close() {
manager.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import io.github.makbn.jthumbnail.core.model.ExecutionResult;
import io.github.makbn.jthumbnail.core.thumbnailers.Thumbnailer;
import io.github.makbn.jthumbnail.core.util.mime.MimeTypeDetector;
import lombok.extern.log4j.Log4j2;
import lombok.AccessLevel;
import lombok.experimental.FieldDefaults;
import lombok.experimental.NonFinal;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
Expand Down Expand Up @@ -35,9 +38,10 @@
*/
@Component
@DependsOn({"DWGThumbnailer", "JODExcelThumbnailer", "PDFBoxThumbnailer", "MPEGThumbnailer",
"openOfficeThumbnailer", "jod_converter", "MP3Thumbnailer", "powerpointConverterThumbnailer",
"openOfficeThumbnailer", "jodConverter", "MP3Thumbnailer", "powerpointConverterThumbnailer",
"JODHtmlConverterThumbnailer", "nativeImageThumbnailer", "textThumbnailer", "imageThumbnailer", "wordConverterThumbnailer"})
@Log4j2
@Slf4j
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
public class ThumbnailerManager implements Thumbnailer {

/**
Expand All @@ -48,16 +52,17 @@ public class ThumbnailerManager implements Thumbnailer {
/**
* Magic Mime Detection ... a wrapper class to Aperature's Mime thingies.
*/
private final MimeTypeDetector mimeTypeDetector;
MimeTypeDetector mimeTypeDetector;
/**
* Thumbnailers per MIME-Type they accept (ALL_MIME_WILDCARD for all)
*/
private final Map<String, List<Thumbnailer>> thumbnailers;
Map<String, List<Thumbnailer>> thumbnailers;

/**
* Folder under which new thumbnails should be filed
*/
private File thumbnailFolder;
@NonFinal
File thumbnailFolder;

/**
* Initialise Thumbnail Manager
Expand Down Expand Up @@ -85,17 +90,7 @@ public File chooseThumbnailFilename(File input, String ext) throws ThumbnailerEx
if (input == null)
throw new IllegalArgumentException("Input file may not be null");

File output;

String name = FilenameUtils.getBaseName(input.getName()) + "_thumb";

name = name + "." + ext;


output = new File(thumbnailFolder, name);


return output;
return new File(thumbnailFolder, String.format("%s%s.%s", FilenameUtils.getBaseName(input.getName()), "_thumb", ext));
}

/**
Expand Down Expand Up @@ -182,7 +177,7 @@ public synchronized void close() {
*/
@Override
public void generateThumbnail(File input, File output, String mimeType) throws ThumbnailerRuntimeException, ThumbnailerException {
if(!Files.exists(input.toPath())){
if (!Files.exists(input.toPath())) {
throw new ThumbnailerException("the input file does not exist");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@
import io.github.makbn.jthumbnail.core.thumbnailers.JODConverterThumbnailer;
import io.github.makbn.jthumbnail.core.thumbnailers.JODHtmlConverterThumbnailer;
import io.github.makbn.jthumbnail.core.thumbnailers.OpenOfficeThumbnailer;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.jodconverter.core.office.OfficeManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration("JTApplicationConfig")
@Log4j2
@Configuration("jtApplicationConfig")
@Slf4j
public class ApplicationConfig {

@Bean
public AppSettings getSettings() {
return new AppSettings();
}

@Bean("jod_converter")
@Bean("jodConverter")
public JODConverterThumbnailer getJodConverterThumbnailer(AppSettings settings, OpenOfficeThumbnailer openOfficeThumbnailer, OfficeManager manager) {
log.debug("jod_converter bean created");
return new JODHtmlConverterThumbnailer(settings, openOfficeThumbnailer, manager);
}

@Bean(name = "async_thread_pool_task_executor")
@Bean(name = "asyncThreadPoolTaskExecutor")
public Executor threadPoolTaskExecutor(AppSettings settings) {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(settings.getAsyncCorePoolSize());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import java.io.File;
import java.io.IOException;

@Configuration("JTOfficeManagerConfiguration")
@DependsOn("JTApplicationConfig")
@Configuration("jtOfficeManagerConfiguration")
@DependsOn("jtApplicationConfig")
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
public class OfficeManagerConfiguration {
OfficeManager officeManager;
Expand All @@ -43,7 +43,7 @@ public OfficeManagerConfiguration(AppSettings settings) throws IOException {
}
}

@Bean("office_manager")
@Bean("officeManager")
public OfficeManager getOfficeManager() {
return officeManager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,7 @@
import java.io.IOException;

public class UnsupportedInputFileFormatException extends IOException {

public UnsupportedInputFileFormatException() {
}

public UnsupportedInputFileFormatException(String arg0) {
super(arg0);
}

public UnsupportedInputFileFormatException(Throwable arg0) {
super(arg0);
}

public UnsupportedInputFileFormatException(String arg0, Throwable arg1) {
super(arg0, arg1);
}

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package io.github.makbn.jthumbnail.core.model;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
import lombok.experimental.NonFinal;

@Data
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@AllArgsConstructor
@RequiredArgsConstructor
public class ExecutionResult {
private final boolean generated;
private Throwable exception;

public ExecutionResult(boolean generated) {
this.generated = generated;
}

public ExecutionResult(boolean generated, Throwable exception) {
this.generated = generated;
this.exception = exception;
}
boolean generated;
@NonFinal
Throwable exception;

public static ExecutionResult success() {
return new ExecutionResult(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,22 @@
package io.github.makbn.jthumbnail.core.model;

import lombok.*;
import lombok.experimental.FieldDefaults;
import lombok.experimental.NonFinal;

import java.io.File;

/**
* @author Mehdi Akbarian-Rastaghi 2018-10-23
*/
@Data
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@RequiredArgsConstructor(staticName = "of", access = AccessLevel.PUBLIC)
public class ThumbnailCandidate {
private File file;
private String uid;
private String thumbExt;

public ThumbnailCandidate(File file, String uid) {
this.file = file;
this.uid = uid;
}

public File getFile() {
return file;
}

public void setFile(File file) {
this.file = file;
}

public String getThumbExt() {
return thumbExt;
}

public void setThumbExt(String thumbExt) {
this.thumbExt = thumbExt;
}

public String getUid() {
return uid;
}

public void setUid(String uid) {
this.uid = uid;
}
@NonNull
File file;
@NonNull
String uid;
@NonFinal
String thumbExt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io.github.makbn.jthumbnail.core.exception.ThumbnailerException;
import io.github.makbn.jthumbnail.core.util.IOUtil;
import io.github.makbn.jthumbnail.core.util.mime.MimeTypeDetector;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.tika.utils.SystemUtils;
import org.jodconverter.core.DocumentConverter;
Expand All @@ -20,9 +20,9 @@
import java.nio.file.Files;
import java.nio.file.Path;

@Component("jod_converter")
@DependsOn("office_manager")
@Log4j2
@Slf4j
@Component("jodConverter")
@DependsOn("officeManager")
public abstract class JODConverterThumbnailer extends AbstractThumbnailer {

/**
Expand Down Expand Up @@ -88,8 +88,8 @@ public void generateThumbnail(File input, File output) throws ThumbnailerExcepti
if (SystemUtils.IS_OS_WINDOWS)
workingFile = new File(workingFile.getAbsolutePath().replace("\\\\", "\\"));

if(!officeManager.isRunning()){
for (int i=0; i<10;i++){
if (!officeManager.isRunning()) {
for (int i = 0; i < 10; i++) {
Thread.sleep(1000);
log.info("waiting for office manager");
if (officeManager.isRunning())
Expand All @@ -107,7 +107,6 @@ public void generateThumbnail(File input, File output) throws ThumbnailerExcepti
.execute();



if (outputTmp.length() == 0) {
throw new ThumbnailerException("Could not convert into OpenOffice-File (file was empty)...");
}
Expand All @@ -117,7 +116,7 @@ public void generateThumbnail(File input, File output) throws ThumbnailerExcepti
} catch (IOException e) {
throw new ThumbnailerException(e);
} catch (OfficeException e) {
log.warn(e);
log.warn(e.getMessage(), e);
throw new ThumbnailerException(e.getMessage());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Expand Down
Loading

0 comments on commit dbb9ffa

Please sign in to comment.