Improve attachment download failure handling

Fixes #2116
This commit is contained in:
AsamK 2026-09-22 21:04:34 +02:00
parent dc2a74e824
commit 29dcac23cf
2 changed files with 21 additions and 4 deletions

View File

@ -168,7 +168,7 @@ public class AttachmentHelper {
try { try {
attachmentStore.storeAttachment(pointer, outputStream -> this.retrieveAttachment(pointer, outputStream)); attachmentStore.storeAttachment(pointer, outputStream -> this.retrieveAttachment(pointer, outputStream));
} catch (IOException e) { } catch (IOException e) {
logger.warn("Failed to download attachment ({}), ignoring: {}", pointer.getRemoteId(), e.getMessage()); logger.warn("Failed to download attachment ({}), ignoring", pointer.getRemoteId(), e);
} }
} }

View File

@ -8,9 +8,11 @@ import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemo
import org.whispersystems.signalservice.api.util.StreamDetails; import org.whispersystems.signalservice.api.util.StreamDetails;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Optional; import java.util.Optional;
public class AttachmentStore { public class AttachmentStore {
@ -51,8 +53,23 @@ public class AttachmentStore {
private void storeAttachment(final File attachmentFile, final AttachmentStorer storer) throws IOException { private void storeAttachment(final File attachmentFile, final AttachmentStorer storer) throws IOException {
createAttachmentsDir(); createAttachmentsDir();
try (OutputStream output = new FileOutputStream(attachmentFile)) { final var temporaryFile = Files.createTempFile(attachmentsPath.toPath(),
storer.store(output); attachmentFile.getName() + ".",
".tmp");
try {
try (OutputStream output = Files.newOutputStream(temporaryFile)) {
storer.store(output);
}
try {
Files.move(temporaryFile,
attachmentFile.toPath(),
StandardCopyOption.ATOMIC_MOVE,
StandardCopyOption.REPLACE_EXISTING);
} catch (AtomicMoveNotSupportedException e) {
Files.move(temporaryFile, attachmentFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
} finally {
Files.deleteIfExists(temporaryFile);
} }
} }