|
36 | 36 | import org.thoughtcrime.securesms.mms.PartUriParser;
|
37 | 37 | import org.thoughtcrime.securesms.service.KeyCachingService;
|
38 | 38 |
|
| 39 | +import java.io.File; |
| 40 | +import java.io.FileDescriptor; |
39 | 41 | import java.io.FileNotFoundException;
|
| 42 | +import java.io.FileOutputStream; |
40 | 43 | import java.io.IOException;
|
41 | 44 | import java.io.InputStream;
|
42 | 45 | import java.io.OutputStream;
|
| 46 | +import java.nio.file.Files; |
43 | 47 |
|
44 | 48 | import network.loki.messenger.BuildConfig;
|
45 | 49 |
|
@@ -198,28 +202,24 @@ private ParcelFileDescriptor getParcelStreamForAttachment(AttachmentId attachmen
|
198 | 202 |
|
199 | 203 | private ParcelFileDescriptor getStreamingParcelFileDescriptor(InputStreamProvider provider) throws IOException {
|
200 | 204 | 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); |
218 | 212 | }
|
219 |
| - }).start(); |
| 213 | + } |
220 | 214 |
|
221 |
| - return readSide; |
| 215 | + ParcelFileDescriptor descriptor = ParcelFileDescriptor.open(tmpFile, ParcelFileDescriptor.MODE_READ_ONLY); |
222 | 216 |
|
| 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; |
223 | 223 | } catch (IOException e) {
|
224 | 224 | Log.w(TAG, "Error creating streaming pipe", e);
|
225 | 225 | throw new FileNotFoundException("Error creating streaming pipe: " + e.getMessage());
|
|
0 commit comments