From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: alina sireneva <alina@tei.pet>
Date: Thu, 30 Jul 2026 03:08:57 +0400
Subject: Save uncached documents to Downloads on the first attempt

---
 .../telegram/messenger/MediaController.java    | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java b/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java
index 0000000..0000000 100644
--- a/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java
+++ b/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java
@@ -5084,6 +5084,7 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener,
         private AlertDialog progressDialog;
         private ArrayList<MessageObject> messageObjects;
         private HashMap<String, MessageObject> loadingMessageObjects = new HashMap<>();
+        private HashMap<String, File> loadedFiles = new HashMap<>();
         private float finishedProgress;
         private boolean cancelled;
         private boolean finished;
@@ -5156,16 +5157,18 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener,
                                 path = file.toString();
                             }
                             File sourceFile = new File(path);
+                            File loadedFile = null;
                             if (!sourceFile.exists()) {
                                 waitingForFile = new CountDownLatch(1);
                                 addMessageToLoad(message);
                                 waitingForFile.await();
+                                loadedFile = loadedFiles.remove(FileLoader.getAttachFileName(document));
                             }
                             if (cancelled) {
                                 break;
                             }
                             if (!sourceFile.exists()) {
-                                sourceFile = FileLoader.getInstance(currentAccount.getCurrentAccount()).getPathToAttach(message.messageOwner, true);
+                                sourceFile = loadedFile;
                                 FileLog.d("saving file: correcting path from " + path + " to " + (sourceFile == null ? null : sourceFile.getAbsolutePath()));
                             }
                             if (sourceFile != null && sourceFile.exists()) {
@@ -5229,12 +5232,17 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener,
                                 }
                                 sourceFile = new File(path);
                             }
+                            File loadedFile = null;
                             if (!sourceFile.exists()) {
                                 waitingForFile = new CountDownLatch(1);
                                 addMessageToLoad(message);
                                 waitingForFile.await();
+                                loadedFile = loadedFiles.remove(FileLoader.getAttachFileName(document));
                             }
-                            if (sourceFile.exists()) {
+                            if (!sourceFile.exists()) {
+                                sourceFile = loadedFile;
+                            }
+                            if (sourceFile != null && sourceFile.exists()) {
                                 copyFile(sourceFile, destFile, message.getMimeType());
                                 copiedFiles++;
                             }
@@ -5268,6 +5276,7 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener,
                 currentAccount.getNotificationCenter().removeObserver(this, NotificationCenter.fileLoaded);
                 currentAccount.getNotificationCenter().removeObserver(this, NotificationCenter.fileLoadProgressChanged);
                 currentAccount.getNotificationCenter().removeObserver(this, NotificationCenter.fileLoadFailed);
+                loadedFiles.clear();
             });
         }
 
@@ -5307,6 +5316,8 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener,
                     }
                 });
                 waitingForFile.await();
+                loadedFiles.remove(FileLoader.getAttachFileName(photoSize));
+                loadedFiles.remove(FileLoader.getAttachFileName(media.document));
             }
             if (cancelled) {
                 return true;
@@ -5487,6 +5498,9 @@ public class MediaController implements AudioManager.OnAudioFocusChangeListener,
             if (id == NotificationCenter.fileLoaded || id == NotificationCenter.fileLoadFailed) {
                 String fileName = (String) args[0];
                 if (loadingMessageObjects.remove(fileName) != null) {
+                    if (id == NotificationCenter.fileLoaded && args.length > 1 && args[1] instanceof File) {
+                        loadedFiles.put(fileName, (File) args[1]);
+                    }
                     waitingForFile.countDown();
                 }
             } else if (id == NotificationCenter.fileLoadProgressChanged) {
