Skip to content

Commit

Permalink
Cache tempFiles when printing raw (qzind#1120)
Browse files Browse the repository at this point in the history
Cache tempFiles when printing raw
Closes qzind#918
  • Loading branch information
tresf authored Oct 4, 2023
1 parent ee94ad6 commit 730e31b
Showing 1 changed file with 56 additions and 36 deletions.
92 changes: 56 additions & 36 deletions src/qz/printer/action/PrintRaw.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -320,32 +321,63 @@ public void print(PrintOutput output, PrintOptions options) throws PrintExceptio
pages.add(commands);
}

List<File> tempFiles = null;
for(int i = 0; i < rawOpts.getCopies(); i++) {
for(ByteArrayBuilder bab : pages) {
for(int j = 0; j < pages.size(); j++) {
ByteArrayBuilder bab = pages.get(j);
try {
if (output.isSetHost()) {
printToHost(output.getHost(), output.getPort(), bab.getByteArray());
} else if (output.isSetFile()) {
printToFile(output.getFile(), bab.getByteArray());
} else {
if (rawOpts.isForceRaw()) {
if(tempFiles == null) {
tempFiles = new ArrayList<>(pages.size());
}
File tempFile;
if(tempFiles.size() <= j) {
tempFile = File.createTempFile("qz_raw_", null);
tempFiles.add(j, tempFile);
printToFile(tempFile, bab.getByteArray());
} else {
tempFile = tempFiles.get(j);
}
if(SystemUtilities.isWindows()) {
// Placeholder only; not yet supported
printToBackend(output.getNativePrinter(), bab.getByteArray(), rawOpts.isRetainTemp(), Backend.WIN32_WMI);
printToBackend(output.getNativePrinter(), tempFile, Backend.WIN32_WMI);
} else {
// Try CUPS backend first, fallback to LPR
printToBackend(output.getNativePrinter(), bab.getByteArray(), rawOpts.isRetainTemp(), Backend.CUPS_RSS, Backend.CUPS_LPR);
printToBackend(output.getNativePrinter(), tempFile, Backend.CUPS_RSS, Backend.CUPS_LPR);
}
} else {
printToPrinter(output.getPrintService(), bab.getByteArray(), rawOpts);
}
}
}
catch(IOException e) {
cleanupTempFiles(rawOpts.isRetainTemp(), tempFiles);
throw new PrintException(e);
}
}
}
cleanupTempFiles(rawOpts.isRetainTemp(), tempFiles);
}

private void cleanupTempFiles(boolean retainTemp, List<File> tempFiles) {
if(tempFiles != null) {
if (!retainTemp) {
for(File tempFile : tempFiles) {
if(tempFile != null) {
if(!tempFile.delete()) {
tempFile.deleteOnExit();
}
}
}
} else {
log.warn("Temp file(s) retained: {}", Arrays.toString(tempFiles.toArray()));
}
}
}

/**
Expand Down Expand Up @@ -447,44 +479,32 @@ public void printJobRequiresAttention(PrintJobEvent printJobEvent) {
/**
* Direct/backend printing modes for forced raw printing
*/
public void printToBackend(NativePrinter printer, byte[] cmds, boolean retainTemp, Backend... backends) throws IOException, PrintException {
File tmp = File.createTempFile("qz_raw_", null);
public void printToBackend(NativePrinter printer, File tempFile, Backend... backends) throws IOException, PrintException {
boolean success = false;
try {
printToFile(tmp, cmds);
for(Backend backend : backends) {
switch(backend) {
case CUPS_LPR:
// Use command line "lp" on Linux, BSD, Solaris, OSX, etc.
String[] lpCmd = new String[] {"lp", "-d", printer.getPrinterId(), "-o", "raw", tmp.getAbsolutePath()};
if (!(success = ShellUtilities.execute(lpCmd))) {
log.debug(StringUtils.join(lpCmd, ' '));
}
break;
case CUPS_RSS:
// Submit job via cupsDoRequest(...) via JNA against localhost:631\
success = CupsUtils.sendRawFile(printer, tmp);
break;
case WIN32_WMI:
default:
throw new UnsupportedOperationException("Raw backend \"" + backend + "\" is not yet supported.");
}
if(success) {

for(Backend backend : backends) {
switch(backend) {
case CUPS_LPR:
// Use command line "lp" on Linux, BSD, Solaris, OSX, etc.
String[] lpCmd = new String[] {"lp", "-d", printer.getPrinterId(), "-o", "raw", tempFile.getAbsolutePath()};
if (!(success = ShellUtilities.execute(lpCmd))) {
log.debug(StringUtils.join(lpCmd, ' '));
}
break;
}
case CUPS_RSS:
// Submit job via cupsDoRequest(...) via JNA against localhost:631\
success = CupsUtils.sendRawFile(printer, tempFile);
break;
case WIN32_WMI:
default:
throw new UnsupportedOperationException("Raw backend \"" + backend + "\" is not yet supported.");
}
if (!success) {
throw new PrintException("Forced raw printing failed");
if(success) {
break;
}
}
finally {
if(!retainTemp) {
if (!tmp.delete()) {
tmp.deleteOnExit();
}
} else{
log.warn("Temp file retained: {}", tmp);
}
if (!success) {
throw new PrintException("Forced raw printing failed");
}
}

Expand Down

0 comments on commit 730e31b

Please sign in to comment.