Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArchiveImportJob: Improvements #1953

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/main/java/sirius/biz/jobs/batch/file/ArchiveImportJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import sirius.kernel.commons.Producer;
import sirius.kernel.commons.Strings;
import sirius.kernel.health.Exceptions;
import sirius.kernel.health.HandledException;
import sirius.kernel.nls.NLS;

import javax.annotation.Nullable;
Expand All @@ -31,6 +32,7 @@
import java.util.Enumeration;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

Expand Down Expand Up @@ -115,10 +117,7 @@ protected Optional<ExtractedFile> fetchEntry(String fileName) throws IOException

protected void handleMissingFile(String fileName, boolean isRequired) {
if (isRequired) {
throw Exceptions.createHandled()
.withNLSKey("ArchiveImportJob.errorMsg.requiredFileMissing")
.set("fileName", fileName)
.handle();
throw createMissingFileException(fileName);
} else {
process.log(ProcessLog.info()
.withNLSKey("ArchiveImportJob.errorMsg.optionalFileMissing")
Expand All @@ -127,6 +126,13 @@ protected void handleMissingFile(String fileName, boolean isRequired) {
}
}

protected HandledException createMissingFileException(String fileName) {
return Exceptions.createHandled()
.withNLSKey("ArchiveImportJob.errorMsg.requiredFileMissing")
.set("fileName", fileName)
.handle();
}

/**
* Checks if all given files are found in the archive.
*
Expand All @@ -140,6 +146,19 @@ protected boolean containsEntries(String... fileNamesToCheck) {
.allMatch(fileName -> zipEntries.stream().anyMatch(entry -> entry.getName().equals(fileName)));
}

/**
* Streams all entries of the archive.
* <p>
* Technically, this method just wraps the method {@link #extractAllFiles(Consumer)} into a {@link Stream}.
*
* @return a stream of all entries in the archive
*/
protected Stream<ExtractedZipFile> streamEntries() {
Stream.Builder<ExtractedZipFile> builder = Stream.builder();
extractAllFiles(builder);
return builder.build();
}

/**
* Extracts all files from the archive
*
Expand Down