Skip to content

Commit

Permalink
Merge pull request #3137 from ControlSystemStudio/CSSTUDIIO-2628
Browse files Browse the repository at this point in the history
Bug fixes for logbook image attachments
  • Loading branch information
georgweiss authored Sep 13, 2024
2 parents ac32ab6 + 2c62080 commit 1dd9b5b
Showing 1 changed file with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,21 +210,27 @@ protected void finalize() {
fileAttachment.setContentType(attachment.getContentType());
fileAttachment.setThumbnail(false);
fileAttachment.setFileName(attachment.getName());
// Determine file extension, needed to support transition to Image Viewer app for image attachments
String fileExtension = "";
int indexOfLastDot = attachment.getName().lastIndexOf('.');
if(indexOfLastDot > -1){
fileExtension = attachment.getName().substring(indexOfLastDot);
}
// A bit of a hack here. The idea is to create a temporary file with a known name,
// i.e. without the random file name part.
// Files.createdTempFile does not support it, so a bit of workaround is needed.
try {
// This creates a temp file with a random part
Path random = Files.createTempFile(attachment.getId(), attachment.getName());
// This does NOT create a file
Path nonRandom = random.resolveSibling(attachment.getId());
Path nonRandom = random.resolveSibling(attachment.getId() + fileExtension);
if(!Files.exists(nonRandom.toAbsolutePath())){
// Moves the temp file with random part to file with non-random part.
nonRandom = Files.move(random, nonRandom);
Files.copy(logClient.getAttachment(logEntry.getId(), attachment.getName()), nonRandom, StandardCopyOption.REPLACE_EXISTING);
fileAttachment.setFile(nonRandom.toFile());
nonRandom.toFile().deleteOnExit();
}
fileAttachment.setFile(nonRandom.toFile());
} catch (LogbookException | IOException e) {
Logger.getLogger(SingleLogEntryDisplayController.class.getName())
.log(Level.WARNING, "Failed to retrieve attachment " + fileAttachment.getFileName(), e);
Expand Down

0 comments on commit 1dd9b5b

Please sign in to comment.