From 6559569f389289f3a6dad391859c1ad3585cbac4 Mon Sep 17 00:00:00 2001
From: taynpg <taynpg@163.com>
Date: Fri, 28 Mar 2025 22:56:23 +0800
Subject: [PATCH] =?UTF-8?q?add=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=8F=AF?=
 =?UTF-8?q?=E4=BB=A5=E7=9B=B4=E6=8E=A5send=E6=96=87=E4=BB=B6=E8=80=8C?=
 =?UTF-8?q?=E9=9D=9E=E5=BF=85=E9=A1=BB=E6=8F=90=E4=BA=A4=E4=B8=80=E4=B8=AA?=
 =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E6=96=87=E4=BB=B6=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 client/client.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++++++-
 client/client.h   |  1 +
 2 files changed, 81 insertions(+), 1 deletion(-)

diff --git a/client/client.cpp b/client/client.cpp
index b23f300..61b1365 100644
--- a/client/client.cpp
+++ b/client/client.cpp
@@ -126,6 +126,10 @@ void CClient::run(const std::string& ip, const std::string& port, const std::str
             request_update_list(param);
             continue;
         }
+        if (scmd == "Send" || scmd == "send") {
+            send_files(param);
+            continue;
+        }
         if (scmd == "Down" || scmd == "down") {
             down_task(param);
             continue;
@@ -195,7 +199,6 @@ bool CClient::down_task(const std::string& param)
 
 bool CClient::up_task(const std::string& param)
 {
-
     {
         std::lock_guard<std::mutex> lock(mutex_);
         for (const auto& item : up_) {
@@ -263,6 +266,82 @@ bool CClient::cancel_task()
     return send_frame(buf.get());
 }
 
+bool CClient::send_files(const std::string& param)
+{
+    auto tvec = COfStr::split(param, " ");
+    if (tvec.size() < 3) {
+        TLOGE("{} invalid param format [{}]", __FUNCTION__, param);
+        return false;
+    }
+    int index = std::stoi(tvec[0]);
+    std::string list_file = tvec[1];
+    std::string pur = tvec[2];
+
+    if (downloading_) {
+        TLOGW("Have Task Downloading, Please wait.....");
+        return false;
+    }
+    if (!task_list_.count(index)) {
+        TLOGE("No Index Found {}.", index);
+        return false;
+    }
+
+    const auto& sr = task_list_[index];
+    if (sr->id == own_id_) {
+        TLOGW("You can't send file to yourself!!!");
+        return false;
+    }
+
+    // 校验格式是否正确
+    auto vec = COfStr::split(list_file, "|");
+    bool valid = true;
+    int line = 1;
+    std::unordered_map<int, std::string> mre{};
+    std::string handled_content;
+    for (const auto& item : vec) {
+        std::string hitem = COfStr::trim(item);
+        if (hitem.empty()) {
+            continue;
+        }
+        auto real_path = COfPath::to_full(hitem);
+        if (!fs::exists(real_path)) {
+            TLOGE("file {} not exist.", real_path);
+            valid = false;
+            break;
+        }
+        TLOGI("--->check pass {}:{}", line, real_path);
+        mre[line++] = real_path + "|" + pur;
+    }
+
+    if (!valid) {
+        TLOGE("Judge File Not Passed.");
+        return false;
+    }
+
+    auto handel_ret = handle_user_select(mre);
+    if (handel_ret.empty()) {
+        TLOGE("handle_user_select not pass, abort action!");
+        return false;
+    }
+
+#if defined(_WIN32)
+    handel_ret = CCodec::ansi_to_u8(handel_ret);
+#endif
+
+    list_file_ = "auto_list";
+    std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
+    buf->type_ = TYPE_REQUEST_UPDATE_LIST;
+    buf->data_ = new char[handel_ret.size() + 1]();
+    buf->len_ = std::snprintf(buf->data_, handel_ret.size() + 1, "%s", handel_ret.c_str());
+    buf->tid_ = task_list_[index]->id;
+
+    if (!send_frame(buf.get())) {
+        TLOGE("Send Failed {}", __LINE__);
+        return false;
+    }
+    return true;
+}
+
 bool CClient::down_one_file(const std::string& id, const std::string& file, const std::string& local_dir)
 {
     std::string back_file(file);
diff --git a/client/client.h b/client/client.h
index d0b6ef2..49617a4 100644
--- a/client/client.h
+++ b/client/client.h
@@ -51,6 +51,7 @@ public:
     bool down_task(const std::string& param);
     bool up_task(const std::string& param);
     bool cancel_task();
+    bool send_files(const std::string& param);
     bool down_one_file(const std::string& id, const std::string& file, const std::string& local_dir = "");
     void report_trans_ret(TransState state, const std::string& key = "");
     bool request_update_list(const std::string& param);