Skip to content

Commit 46d680f

Browse files
Fix PDF attachment issue (#1537)
1 parent b395094 commit 46d680f

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

app/src/main/java/org/thoughtcrime/securesms/providers/PartAndBlobProvider.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@
3636
import org.thoughtcrime.securesms.mms.PartUriParser;
3737
import org.thoughtcrime.securesms.service.KeyCachingService;
3838

39+
import java.io.File;
40+
import java.io.FileDescriptor;
3941
import java.io.FileNotFoundException;
42+
import java.io.FileOutputStream;
4043
import java.io.IOException;
4144
import java.io.InputStream;
4245
import java.io.OutputStream;
46+
import java.nio.file.Files;
4347

4448
import network.loki.messenger.BuildConfig;
4549

@@ -198,28 +202,24 @@ private ParcelFileDescriptor getParcelStreamForAttachment(AttachmentId attachmen
198202

199203
private ParcelFileDescriptor getStreamingParcelFileDescriptor(InputStreamProvider provider) throws IOException {
200204
try {
201-
ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe();
202-
ParcelFileDescriptor readSide = pipe[0];
203-
ParcelFileDescriptor writeSide = pipe[1];
204-
205-
new Thread(() -> {
206-
try (InputStream inputStream = provider.getInputStream();
207-
OutputStream outputStream = new ParcelFileDescriptor.AutoCloseOutputStream(writeSide)) {
208-
209-
Util.copy(inputStream, outputStream);
210-
211-
} catch (IOException e) {
212-
Log.w(TAG, "Error streaming data", e);
213-
try {
214-
writeSide.closeWithError("Error streaming data: " + e.getMessage());
215-
} catch (IOException closeException) {
216-
Log.w(TAG, "Error closing write side of pipe", closeException);
217-
}
205+
final File tmpFile = Files.createTempFile("attachment", ".part").toFile();
206+
207+
// Write decrypted file into a temporary file
208+
try (final InputStream inputStream = provider.getInputStream()) {
209+
try (final FileOutputStream os = new FileOutputStream(tmpFile)) {
210+
final long copied = Util.copy(inputStream, os);
211+
Log.d(TAG, "Copied " + copied + " bytes to " + tmpFile);
218212
}
219-
}).start();
213+
}
220214

221-
return readSide;
215+
ParcelFileDescriptor descriptor = ParcelFileDescriptor.open(tmpFile, ParcelFileDescriptor.MODE_READ_ONLY);
222216

217+
// We can actually delete the tmp file now - Linux will keep the file because we opened it before.
218+
// Once that file description is closed, the file will be removed automatically.
219+
if (!tmpFile.delete()) {
220+
Log.w(TAG, "Unable to delete temp file " + tmpFile);
221+
}
222+
return descriptor;
223223
} catch (IOException e) {
224224
Log.w(TAG, "Error creating streaming pipe", e);
225225
throw new FileNotFoundException("Error creating streaming pipe: " + e.getMessage());

0 commit comments

Comments
 (0)