Skip to content

Commit a576673

Browse files
committed
check if PDF tempfile was created by another process.
1 parent 17c4cd2 commit a576673

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

pdf/src/main/java/digilib/conf/PDFServletConfiguration.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515

1616
/*
1717
* #%L
18-
* DigilibConfiguration -- Holding all parameters for digilib servlet.
18+
* PDFServletConfiguration -- Holding all parameters for digilib PDF servlet.
1919
*
2020
* Digital Image Library servlet components
2121
*
2222
* %%
23-
* Copyright (C) 2001 MPIWG Berlin
23+
* Copyright (C) 2009 MPIWG Berlin
2424
* %%
2525
* This program is free software: you can redistribute it and/or modify
2626
* it under the terms of the GNU Lesser General Public License as
@@ -36,7 +36,7 @@
3636
* License along with this program. If not, see
3737
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
3838
* #L%
39-
* Author: Robert Casties (robcast@berlios.de)
39+
* Author: Robert Casties ([email protected].de)
4040
*/
4141

4242
/**

pdf/src/main/java/digilib/servlet/PDFGenerator.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@
2424
*/
2525

2626
import java.io.File;
27-
import java.io.FileNotFoundException;
2827
import java.io.IOException;
2928
import java.io.UnsupportedEncodingException;
3029
import java.net.URLDecoder;
30+
import java.nio.file.FileAlreadyExistsException;
3131
import java.util.Base64;
3232
import java.util.concurrent.Future;
3333

34-
import jakarta.json.Json;
35-
import jakarta.json.stream.JsonGenerator;
3634
import javax.servlet.RequestDispatcher;
3735
import javax.servlet.ServletConfig;
3836
import javax.servlet.ServletContext;
@@ -53,6 +51,8 @@
5351
import digilib.image.ImageOpException;
5452
import digilib.pdf.PDFFileWorker;
5553
import digilib.util.DigilibJobCenter;
54+
import jakarta.json.Json;
55+
import jakarta.json.stream.JsonGenerator;
5656

5757
/**
5858
* A class for handling user requests for pdf documents made from digilib
@@ -223,12 +223,18 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr
223223
// start PDF creation thread
224224
createNewPdfDocument(pdfji, docid);
225225
// redirect client with docid parameter
226-
String url = "";
227-
url += "?docid=" + encodeDocid(docid);
226+
String url = "?docid=" + encodeDocid(docid);
228227
logger.debug("redirecting to {}", url);
229228
response.sendRedirect(url);
230229
return;
231-
} catch (FileNotFoundException e) {
230+
} catch (FileAlreadyExistsException e) {
231+
// temp file actually exists - assume WIP
232+
logger.warn("Temp file seems to exist: {} - assume WIP.", e.getMessage());
233+
String url = "?docid=" + encodeDocid(docid);
234+
logger.debug("redirecting to {}", url);
235+
response.sendRedirect(url);
236+
return;
237+
} catch (IOException e) {
232238
// error in pdf creation
233239
logger.error(e.getMessage());
234240
notifyUser(PDFStatus.ERROR, docid, request, response);
@@ -365,16 +371,19 @@ public PDFStatus getStatus(String documentid) {
365371
}
366372

367373
/**
368-
* create new thread for pdf generation.
374+
* Create new thread for pdf generation.
369375
*
370376
* @param pdfji
371377
* @param filename
372378
* @return
373-
* @throws FileNotFoundException
379+
* @throws IOException
374380
*/
375-
public Future<File> createNewPdfDocument(PDFRequest pdfji, String filename) throws FileNotFoundException {
381+
public Future<File> createNewPdfDocument(PDFRequest pdfji, String filename) throws IOException {
376382
// start new worker
377383
File tempf = this.getTempFile(filename);
384+
if (!tempf.createNewFile()) {
385+
throw new FileAlreadyExistsException(tempf.toString());
386+
}
378387
File finalf = this.getCacheFile(filename);
379388
PDFFileWorker job = new PDFFileWorker(dlConfig, tempf, finalf, pdfji, pdfImageJobCenter);
380389
// start job

0 commit comments

Comments
 (0)