Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Rakambda committed Sep 30, 2023
1 parent 4319542 commit 4a4f0c4
Show file tree
Hide file tree
Showing 26 changed files with 389 additions and 369 deletions.
10 changes: 8 additions & 2 deletions src/main/java/fr/rakambda/mediaconverter/file/FileProcessor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.rakambda.mediaconverter.file;

import com.github.kokorin.jaffree.ffmpeg.FFmpeg;
import fr.rakambda.mediaconverter.mediaprocessor.MediaProcessorTask;
import fr.rakambda.mediaconverter.progress.ProgressBarSupplier;
import lombok.NonNull;
import lombok.extern.log4j.Log4j2;
Expand All @@ -9,8 +10,10 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Objects;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
Expand All @@ -29,6 +32,7 @@ public class FileProcessor implements Runnable{
private final boolean deleteInput;

private final CountDownLatch countDownLatch;
private final Collection<MediaProcessorTask> tasks;
private boolean shutdown;

public FileProcessor(@NonNull ExecutorService executor,
Expand All @@ -51,6 +55,7 @@ public FileProcessor(@NonNull ExecutorService executor,
this.deleteInput = deleteInput;

countDownLatch = new CountDownLatch(1);
tasks = new ConcurrentLinkedDeque<>();
shutdown = false;
}

Expand Down Expand Up @@ -103,8 +108,8 @@ private void processFile(@NonNull FileProber.ProbeResult probeResultt){
converterProgressBarSupplier,
deleteInput
);

executor.submit(task);
tasks.add(task);
task.execute(executor);
}

private Path buildOutFile(@NonNull Path file, @Nullable String desiredExtension){
Expand All @@ -125,6 +130,7 @@ private Path buildOutFile(@NonNull Path file, @Nullable String desiredExtension)

public void shutdown() throws InterruptedException{
shutdown = true;
tasks.forEach(MediaProcessorTask::cancel);
countDownLatch.await();
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package fr.rakambda.mediaconverter.itemprocessor;

import fr.rakambda.mediaconverter.progress.ProgressBarHandle;
import fr.rakambda.mediaconverter.progress.ProgressBarSupplier;
import lombok.NonNull;
import lombok.extern.log4j.Log4j2;
import java.nio.file.Path;
import java.util.Objects;
import java.util.concurrent.ExecutorService;

@Log4j2
public abstract class CommandConverter extends ConverterRunnable{
private final ProgressBarSupplier converterProgressBarSupplier;

private ProgressBarHandle progressBar;
private Process process;

protected CommandConverter(@NonNull Path input, @NonNull Path output, @NonNull Path temporary, boolean deleteInput, ProgressBarSupplier converterProgressBarSupplier){
super(input, output, temporary, deleteInput);
this.converterProgressBarSupplier = converterProgressBarSupplier;
}

@Override
protected void convert(@NonNull ExecutorService executorService) throws Exception{
log.info("Converting {} to {}", getInput(), getOutput());
progressBar = converterProgressBarSupplier.get();

ProcessBuilder builder = new ProcessBuilder(getCommand());
executorService.execute(() -> {
try{
progressBar.getProgressBar().stepTo(0);
progressBar.getProgressBar().setExtraMessage(getOutput().getFileName().toString());
progressBar.getProgressBar().maxHint(1);

process = builder.start();
process.onExit()
.thenAccept(p -> progressBar.getProgressBar().stepTo(1))
.thenAccept(p -> close());
process.waitFor();
}
catch(Exception e){
log.error("Failed to convert", e);
}
});
}

@Override
public void cancel(){
if(Objects.nonNull(process)){
process.destroy();
}
}

@Override
public void close(){
if(Objects.nonNull(progressBar)){
progressBar.close();
}
}

protected abstract String[] getCommand();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Objects;
import java.util.concurrent.ExecutorService;

@Log4j2
@Getter
Expand Down Expand Up @@ -60,9 +61,9 @@ protected void copyFileAttributes(@NonNull Path from, @NonNull Path to) throws I
}

@Override
public void run(){
public void execute(@NonNull ExecutorService executorService){
try{
convert();
convert(executorService);

if(Files.exists(getTemporary())){
var inputAttributes = Files.getFileAttributeView(getInput(), BasicFileAttributeView.class).readAttributes();
Expand Down Expand Up @@ -114,5 +115,5 @@ protected void copyFileAttributes(@NonNull BasicFileAttributes baseAttributes, P
attributes.setTimes(baseAttributes.lastModifiedTime(), baseAttributes.lastAccessTime(), baseAttributes.creationTime());
}

protected abstract void convert() throws Exception;
protected abstract void convert(@NonNull ExecutorService executorService) throws Exception;
}
Loading

0 comments on commit 4a4f0c4

Please sign in to comment.