Clean code

This commit is contained in:
2025-08-15 00:31:26 +03:30
parent 3412648c7f
commit e344facb20
4 changed files with 15 additions and 50 deletions
@@ -3824,12 +3824,11 @@ public class ActionHandler {
long declaredSize = att.optLong("file_size", 0L);
// ~/Downloads/TeleSock/<Account>/<Chat>/
String accFolder = accountFolderName(); // ← از یوزر لاگین‌شده
String chatFolder = chatFolderName(chat); // ← برای پرایوت: اسم طرف مقابل
String accFolder = accountFolderName();
String chatFolder = chatFolderName(chat);
Path saveDir = Paths.get(System.getProperty("user.home"),
"Downloads", "TeleSock", accFolder, chatFolder);
// دیباگ اختیاری
System.out.println("👤 AccountFolder = " + accFolder);
System.out.println("💬 ChatFolder = " + chatFolder);
System.out.println("📁 SaveDir = " + saveDir);
@@ -3840,7 +3839,6 @@ public class ActionHandler {
return;
}
// اگر قبلاً دانلود شده (و فایل واقعاً وجود دارد) — ایندکس مخصوص همین اکانت
DownloadsIndex di = Session.downloadsIndex;
if (di != null) {
Path existing = di.find(mediaKey);
@@ -3849,11 +3847,8 @@ public class ActionHandler {
return;
}
}
// جلوگیری از overwrite
Path target = uniquePath(saveDir, fileName);
// دانلود روی همان سوکت
TelegramClient.mediaBusy.set(true);
try {
Path saved = TelegramClient.getDownloader()
@@ -3873,7 +3868,6 @@ public class ActionHandler {
// نام یکتا اگر فایل موجود است: name.png -> name (1).png
private static Path uniquePath(Path dir, String fileName) {
Path p = dir.resolve(fileName);
if (!Files.exists(p)) return p;
@@ -3894,12 +3888,9 @@ public class ActionHandler {
}
private static String sanitizeFileName(String s) {
// حذف مسیر و کاراکترهای غیرمجاز (برای ویندوز/یونیکس)
s = s.replace("\\", "/");
if (s.contains("/")) s = s.substring(s.lastIndexOf('/') + 1);
// کاراکترهای نامعتبر ویندوز: \ / : * ? " < > |
s = s.replaceAll("[\\\\/:*?\"<>|]", "_");
// جلوگیری از parent traversal
if (s.equals(".") || s.equals("..") || s.isBlank()) s = "file";
return s;
}
@@ -4183,7 +4174,6 @@ public class ActionHandler {
public void sendMediaMessage(UUID receiverId, String receiverType, String type /* IMAGE/AUDIO */, File file, String caption) {
// اعتبارسنجی ورودی فایل (اگر ورودی از یوزر میاد، قبل از ساخت File کوتیشن‌ها رو حذف کن)
if (file == null) {
System.out.println("❌ File is null");
return;
@@ -4216,21 +4206,19 @@ public class ActionHandler {
byte[] headerBytes = header.toString().getBytes(java.nio.charset.StandardCharsets.UTF_8);
long contentLen = file.length();
// صف ACK
BlockingQueue<JSONObject> q = new LinkedBlockingQueue<>(1);
TelegramClient.pendingResponses.put(messageId.toString(), q);
try {
// ⛔ مهم: فقط از outBin استفاده کن تا بافرها قاطی نشن
// 1) خط سوئیچ به MEDIA
outBin.write("MEDIA\n".getBytes(java.nio.charset.StandardCharsets.US_ASCII));
outBin.flush();
// 2) فریم باینری: magic + headerLen + header + contentLen + content
// 2) binary frame: magic + headerLen + header + contentLen + content
outBin.writeInt(0x4D444D31); // "MDM1"
outBin.writeInt(headerBytes.length); // headerLen (int)
outBin.write(headerBytes); // header
outBin.writeLong(contentLen); // contentLen (long) ← مطمئن شو سرور هم Long می‌خونه
outBin.writeLong(contentLen); // contentLen (long)
try (InputStream fis = new BufferedInputStream(new FileInputStream(file))) {
byte[] buf = new byte[8192];
@@ -4241,7 +4229,6 @@ public class ActionHandler {
}
outBin.flush();
// 3) منتظر ACK با تایم‌اوت (فقط یکی!)
JSONObject ack = q.poll(20, java.util.concurrent.TimeUnit.SECONDS);
if (ack == null) {
System.out.println("❌ Media ACK timeout for " + messageId);
@@ -4294,7 +4281,6 @@ public class ActionHandler {
}
private static String accountFolderName() {
// اولویت: username → user_id → profile_name → internal_uuid
JSONObject me = Session.currentUser;
String acc = me.optString("username",
me.optString("user_id",
@@ -4304,10 +4290,8 @@ public class ActionHandler {
}
private static String chatFolderName(ChatEntry chat) {
// پرایوت: اسم طرف مقابل؛ گروه/کانال: اسم چت
String name = chat.getName();
if (name == null || name.isBlank()) {
// fallback به displayId یا id
name = chat.getDisplayId() != null && !chat.getDisplayId().isBlank()
? chat.getDisplayId()
: String.valueOf(chat.getId());
@@ -22,7 +22,6 @@ public final class DownloadsIndex {
}
/** مسیر فایل ایندکس مخصوص هر اکانت */
private static Path resolveIndexPath(String accountId) {
String os = System.getProperty("os.name", "").toLowerCase();
String home = System.getProperty("user.home");
@@ -40,7 +39,6 @@ public final class DownloadsIndex {
return dir.resolve("downloads-index-" + accountId + ".json");
}
/** لود از دیسک (اگر فایل وجود داشته باشد) */
private synchronized void load() {
map.clear();
try {
@@ -65,7 +63,6 @@ public final class DownloadsIndex {
}
}
/** ذخیره اتمیک روی دیسک */
private synchronized void save() throws IOException {
JSONObject items = new JSONObject();
for (Map.Entry<UUID, Entry> it : map.entrySet()) {
@@ -86,12 +83,10 @@ public final class DownloadsIndex {
}
}
/** ذخیره‌ی بی‌سر‌وصدا */
public void saveQuietly() {
try { save(); } catch (Exception ignored) {}
}
/** اگر قبلاً دانلود شده و فایلش هست، مسیر را برمی‌گرداند؛ وگرنه رکورد کهنه پاک می‌شود. */
public Path find(UUID mediaKey) {
Entry e = map.get(mediaKey);
if (e == null) return null;
@@ -102,13 +97,11 @@ public final class DownloadsIndex {
return null;
}
/** بعد از دانلود موفق */
public void put(UUID mediaKey, Path path, long size) {
map.put(mediaKey, new Entry(path.toString(), size, System.currentTimeMillis()));
saveQuietly();
}
/** اختیاری */
public void remove(UUID mediaKey) {
map.remove(mediaKey);
saveQuietly();
@@ -18,11 +18,9 @@ public final class SocketMediaDownloader {
public java.nio.file.Path download(java.util.UUID mediaKey, java.nio.file.Path saveDir, String fileNameHint) throws Exception {
// 1) سوییچ مود با PrintWriter
outText.print("MEDIA_DL\n");
outText.flush();
// 2) هدر باینری درخواست
org.json.JSONObject req = new org.json.JSONObject()
.put("op","download")
.put("media_key", mediaKey.toString())
@@ -34,7 +32,6 @@ public final class SocketMediaDownloader {
outBin.write(hb);
outBin.flush();
// 3) پاسخ
int magic = inBin.readInt();
if (magic != MAGIC_DL) throw new java.io.IOException("bad magic");