Skip to content

Commit

Permalink
refactor: expose adventurous code, dealing with log files
Browse files Browse the repository at this point in the history
  • Loading branch information
ivy-rew committed Oct 31, 2023
1 parent 4f182bd commit 5f75f94
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package ch.ivyteam.ivy.addons.docfactory.aspose;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.List;

import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -74,18 +77,23 @@ public void convertToPdfA(String filePath, PdfAType pdfAType)
if (StringUtils.isBlank(filePath) || !filePath.toLowerCase().endsWith(PDF_EXTENSION)) {
throw new IllegalArgumentException("The filePath parameter must point to a PDF file.");
}
com.aspose.pdf.Document pdfDocument = null;
try (InputStream in = new FileInputStream(filePath)) {
pdfDocument = new com.aspose.pdf.Document(in);
pdfDocument.convert(getAsposePdfFactoryLogFile().getAbsolutePath(), getAsposePDFAType(pdfAType),
ConvertErrorAction.Delete);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (InputStream in = new FileInputStream(filePath);
var pdfDocument = new com.aspose.pdf.Document(in)) {
pdfDocument.convert(bos, getAsposePDFAType(pdfAType), ConvertErrorAction.Delete);
pdfDocument.setLinearized(true);
pdfDocument.save(filePath);
} catch (Exception ex) {
throw new DocFactoryException(ex);
} finally {
if (pdfDocument != null) {
pdfDocument.close();
try {
var logs = new String(bos.toByteArray());
if (!logs.isBlank()) {
var file = getAsposePdfFactoryLogFile().toPath();
Files.writeString(file, logs, StandardOpenOption.APPEND);
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import java.util.Collection;

import javax.faces.bean.ApplicationScoped;

import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.filefilter.SuffixFileFilter;

import ch.ivyteam.di.restricted.DiCore;
import ch.ivyteam.ivy.config.IFileAccess;
import ch.ivyteam.ivy.environment.Ivy;
Expand All @@ -27,7 +29,8 @@ public File getLogDirectory() {
}
try {
IOFileFilter filter = new SuffixFileFilter(".log");
Collection<File> logs = DiCore.getGlobalInjector().getInstance(IFileAccess.class).getLogFiles(filter);
IFileAccess files = DiCore.getGlobalInjector().getInstance(IFileAccess.class);
Collection<File> logs = files.getLogFiles(filter);
File log = logs.stream().findFirst().orElse(null);
if (log != null) {
ivyLogDirectory = log.getParentFile();
Expand Down

0 comments on commit 5f75f94

Please sign in to comment.