diff --git a/server/2015Remote/FileManagerDlg.cpp b/server/2015Remote/FileManagerDlg.cpp index 8c0396c..44a29fe 100644 --- a/server/2015Remote/FileManagerDlg.cpp +++ b/server/2015Remote/FileManagerDlg.cpp @@ -27,6 +27,18 @@ static uint64_t FileTimeToUnixMs(const FILETIME& ft) return (u.QuadPart - kEpochDiff) / 10000ULL; } +static std::string HexSha256(const BYTE* sha) +{ + static const char* hex = "0123456789abcdef"; + std::string out; + out.reserve(64); + for (int i = 0; i < 32; ++i) { + out += hex[sha[i] >> 4]; + out += hex[sha[i] & 0x0F]; + } + return out; +} + #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE @@ -253,6 +265,7 @@ BEGIN_MESSAGE_MAP(CFileManagerDlg, CDialog) ON_MESSAGE(WM_RECVFILEV2_CHUNK, &CFileManagerDlg::OnRecvFileV2Chunk) ON_MESSAGE(WM_RECVFILEV2_COMPLETE, &CFileManagerDlg::OnRecvFileV2Complete) ON_MESSAGE(WM_PREVIEW_RESULT, &CFileManagerDlg::OnPreviewResult) + ON_MESSAGE(WM_PREVIEW_HASH_RESP, &CFileManagerDlg::OnPreviewHashResult) #ifdef _DEBUG ON_MESSAGE(WM_APP + 0x123, &CFileManagerDlg::OnPreviewTest) #endif @@ -908,7 +921,18 @@ BOOL CFileManagerDlg::PreTranslateMessage(MSG* pMsg) void CFileManagerDlg::OnTimer(UINT_PTR nIDEvent) { - // TODO: Add your message handler code here and/or call default + if (nIDEvent == kPreviewHashTimer) { + KillTimer(kPreviewHashTimer); + if (!m_pendingHash.fullPath.IsEmpty()) { + CString full = m_pendingHash.fullPath; + uint64_t size = m_pendingHash.size; + uint64_t mtime = m_pendingHash.mtimeMs; + m_pendingHash = PendingHashPreview(); + Mprintf("[Preview] hash timeout; fallback to metadata\n"); + ContinueRemotePreview(full, size, mtime, std::string()); + } + return; + } m_ProgressCtrl->StepIt(); __super::OnTimer(nIDEvent); } @@ -1015,6 +1039,8 @@ void CFileManagerDlg::OnClose() return; } CFilePreviewMgr::Instance().CancelForHwnd(m_hWnd); + KillTimer(kPreviewHashTimer); + m_pendingHash = PendingHashPreview(); CoUninitialize(); DialogBase::OnClose(); @@ -1043,6 +1069,25 @@ CString CFileManagerDlg::GetParentDirectory(CString strPath) void CFileManagerDlg::OnReceiveComplete() { switch (m_ContextObject->m_DeCompressionBuffer.GetBuffer(0)[0]) { + case TOKEN_FILE_HASH: { // 远程文件 SHA-256 查询å“�应(å�Œå‡»é¢„览 P2) + LPBYTE buf = m_ContextObject->m_DeCompressionBuffer.GetBuffer(0); + ULONG len = m_ContextObject->m_DeCompressionBuffer.GetBufferLen(); + if (len < sizeof(FileHashResponseHeader)) break; + const FileHashResponseHeader* h = (const FileHashResponseHeader*)buf; + const size_t nameOff = sizeof(FileHashResponseHeader); + size_t nameLen = strnlen((const char*)buf + nameOff, len - nameOff); + FilePreviewHashResult* r = new FilePreviewHashResult(); + r->path.assign((const char*)buf + nameOff, nameLen); + r->status = h->status; + r->err = h->err; + r->size = h->size; + r->mtimeMs = h->mtimeMs; + if (h->status == FILE_HASH_OK) + r->sha256 = HexSha256(h->sha256); + if (!PostMessage(WM_PREVIEW_HASH_RESP, 0, (LPARAM)r)) + delete r; + break; + } case TOKEN_FILE_LIST: // 文件列表 try { FixedRemoteFileList @@ -1325,6 +1370,39 @@ void CFileManagerDlg::BeginRemotePreview(const CString& fullPath) return; } + // P2:支æŒ�哈希查询的客户端先å�–当å‰�内容的 SHA-256,命中已有缓存å�³å�¯ä¸�传输。 + if (mainCtx->SupportsFileHash()) { + if (!m_pendingHash.fullPath.IsEmpty()) { + ::MessageBox(m_hWnd, _TR("正在预览å�¦ä¸€ä¸ªæ–‡ä»¶ï¼Œè¯·ç¨�候"), + _TR("æ��示"), MB_OK | MB_ICONINFORMATION); + return; + } + std::string remotePath((LPCSTR)fullPath); + m_pendingHash.fullPath = fullPath; + m_pendingHash.clientID = clientID; + m_pendingHash.size = meta.size; + m_pendingHash.mtimeMs = meta.mtimeMs; + + std::vector pkt; + pkt.push_back(COMMAND_FILE_HASH); + uint64_t maxBytes = FILE_PREVIEW_MAX_BYTES; + const BYTE* mb = (const BYTE*)&maxBytes; + pkt.insert(pkt.end(), mb, mb + sizeof(maxBytes)); + pkt.insert(pkt.end(), remotePath.begin(), remotePath.end()); + pkt.push_back(0); + m_ContextObject->Send2Client(pkt.data(), (DWORD)pkt.size()); + SetTimer(kPreviewHashTimer, 5000, NULL); + ShowMessage(_TR("正在获å�–远程文件信æ�¯â€¦")); + return; + } + + ContinueRemotePreview(fullPath, meta.size, meta.mtimeMs, std::string()); +} + +void CFileManagerDlg::ContinueRemotePreview(const CString& fullPath, uint64_t size, + uint64_t mtimeMs, const std::string& sha256) +{ + uint64_t clientID = GetClientID(); std::string remotePath((LPCSTR)fullPath); size_t pos = remotePath.find_last_of("\\/"); std::string basename = (pos == std::string::npos) ? remotePath : remotePath.substr(pos + 1); @@ -1332,12 +1410,13 @@ void CFileManagerDlg::BeginRemotePreview(const CString& fullPath) std::string outPath, error, cachedSha; CFilePreviewMgr::PrepareResult pr = CFilePreviewMgr::Instance().Prepare( - clientID, clientKey, remotePath, basename, meta.size, meta.mtimeMs, - std::string(), m_hWnd, outPath, error, &cachedSha); - Mprintf("[Preview] prepare result=%d out=%s err=%s\n", (int)pr, outPath.c_str(), error.c_str()); + clientID, clientKey, remotePath, basename, size, mtimeMs, + sha256, m_hWnd, outPath, error, &cachedSha); + Mprintf("[Preview] prepare result=%d out=%s err=%s sha=%s\n", + (int)pr, outPath.c_str(), error.c_str(), sha256.c_str()); if (pr == CFilePreviewMgr::PrepareCached) { - CFilePreviewMgr::OpenLocalFile(m_hWnd, outPath, remotePath, meta.size, cachedSha); + CFilePreviewMgr::OpenLocalFile(m_hWnd, outPath, remotePath, size, cachedSha); return; } if (pr != CFilePreviewMgr::PrepareTransfer) { @@ -1373,6 +1452,46 @@ LRESULT CFileManagerDlg::OnPreviewResult(WPARAM /*wParam*/, LPARAM lParam) return 0; } +LRESULT CFileManagerDlg::OnPreviewHashResult(WPARAM /*wParam*/, LPARAM lParam) +{ + FilePreviewHashResult* r = (FilePreviewHashResult*)lParam; + if (!r) return 0; + KillTimer(kPreviewHashTimer); + + if (!m_pendingHash.fullPath.IsEmpty()) { + std::string expected((LPCSTR)m_pendingHash.fullPath); + if (r->path == expected) { + CString full = m_pendingHash.fullPath; + uint64_t size = m_pendingHash.size; + uint64_t mtime = m_pendingHash.mtimeMs; + m_pendingHash = PendingHashPreview(); + + if (r->status == FILE_HASH_TOO_LARGE || + (r->status == FILE_HASH_OK && r->size > FILE_PREVIEW_MAX_BYTES)) { + CString msg; + msg.FormatL("文件较大,ä¸�支æŒ�预览:%s(%llu MB,上é™� %u MB)", full, + (unsigned long long)(r->size / 1024 / 1024), + (unsigned)(FILE_PREVIEW_MAX_BYTES / 1024 / 1024)); + ::MessageBox(m_hWnd, msg, _TR("æ��示"), MB_OK | MB_ICONINFORMATION); + } else if (r->status == FILE_HASH_OK) { + ContinueRemotePreview(full, r->size, r->mtimeMs, r->sha256); + } else if (r->status == FILE_HASH_ERROR) { + // 明确的错误(ä¸�存在/æ— æ�ƒé™�/读失败):æ��示,ä¸�é�™é»˜é™�级下载。 + CString msg; + msg.FormatL("获å�–远程文件信æ�¯å¤±è´¥ï¼ˆé”™è¯¯ç � %u)", r->err); + ::MessageBox(m_hWnd, msg, _TR("æ��示"), MB_OK | MB_ICONWARNING); + } else { + // 未知状æ€�/哈希超时按 P1 é™�级:用列表里的 size+mtime 继续。 + Mprintf("[Preview] hash error status=%u err=%u; fallback to metadata\n", + r->status, r->err); + ContinueRemotePreview(full, size, mtime, std::string()); + } + } + } + delete r; + return 0; +} + #ifdef _DEBUG // 无鼠标端到端测试:直接以 NM_DBLCLK è°ƒå�Œä¸€ä¸ªå¤„ç�†å‡½æ•°ï¼Œé�¿å…�跨进程鼠标注入。 LRESULT CFileManagerDlg::OnPreviewTest(WPARAM wParam, LPARAM lParam) @@ -3035,6 +3154,8 @@ void CFileManagerDlg::PostNcDestroy() } m_FileRecvDlgs.clear(); CFilePreviewMgr::Instance().CancelForHwnd(m_hWnd); + KillTimer(kPreviewHashTimer); + m_pendingHash = PendingHashPreview(); __super::PostNcDestroy(); } diff --git a/server/2015Remote/FileManagerDlg.h b/server/2015Remote/FileManagerDlg.h index d50de5a..b5113bb 100644 --- a/server/2015Remote/FileManagerDlg.h +++ b/server/2015Remote/FileManagerDlg.h @@ -129,12 +129,23 @@ public: bool m_bRemoteSearchResults = false; struct RemoteMeta { uint64_t size = 0; uint64_t mtimeMs = 0; }; std::map m_RemoteMeta; // key = 完整远程路径(GBK) + struct PendingHashPreview { + CString fullPath; // 空 = 没有待å“�应的哈希请求 + uint64_t clientID = 0; + uint64_t size = 0; + uint64_t mtimeMs = 0; + }; + PendingHashPreview m_pendingHash; + enum { kPreviewHashTimer = 0x51 }; afx_msg LRESULT OnPreviewResult(WPARAM wParam, LPARAM lParam); + afx_msg LRESULT OnPreviewHashResult(WPARAM wParam, LPARAM lParam); #ifdef _DEBUG // E2E 测试钩å­�:WM_APP+0x123,wParam=行å�·ï¼ŒlParam!=0 表示远程列表。 afx_msg LRESULT OnPreviewTest(WPARAM wParam, LPARAM lParam); #endif void BeginRemotePreview(const CString& fullPath); + void ContinueRemotePreview(const CString& fullPath, uint64_t size, uint64_t mtimeMs, + const std::string& sha256); void OpenLocalPath(const CString& path); bool GetRemoteFileMeta(const CString& fullPath, RemoteMeta& meta); std::string GetPreviewClientKey(uint64_t clientID); diff --git a/server/2015Remote/FilePreview.cpp b/server/2015Remote/FilePreview.cpp index ffc913e..7ec3c0c 100644 --- a/server/2015Remote/FilePreview.cpp +++ b/server/2015Remote/FilePreview.cpp @@ -157,6 +157,22 @@ CFilePreviewMgr::PrepareResult CFilePreviewMgr::Prepare( if (m.remotePath == hit.remotePath && m.localName == hit.localName) m.lastUsed = now; } + // 按 SHA 命中å�¦ä¸€æ�¡è¿œç¨‹è·¯å¾„(跨路径去é‡�):把当å‰�路径补进索引, + // 下次å�Œä¸€è·¯å¾„å�¯ç›´æŽ¥æŒ‰ remotePath/size/mtime 命中,ä¸�å¿…å†�查哈希。 + if (hit.remotePath != remotePath) { + Meta m; + m.remotePath = remotePath; + m.size = hit.size; + m.mtimeMs = mtimeMs; + m.sha256 = hit.sha256; + m.localName = hit.localName; + m.lastUsed = now; + bool replaced = false; + for (auto& e : index) { + if (e.remotePath == remotePath) { e = m; replaced = true; break; } + } + if (!replaced) index.push_back(m); + } SaveIndex(clientDir, index); outPath = localPath; if (outSha) *outSha = hit.sha256; diff --git a/server/2015Remote/FilePreview.h b/server/2015Remote/FilePreview.h index ae704f1..0bd9163 100644 --- a/server/2015Remote/FilePreview.h +++ b/server/2015Remote/FilePreview.h @@ -21,6 +21,18 @@ class CONTEXT_OBJECT; // 预览完æˆ�/失败:lParam 是 new 出æ�¥çš„ CFilePreviewMgr::Result,由对è¯�框 delete。 #define WM_PREVIEW_RESULT (WM_USER + 0x306) +// 哈希查询å“�应:lParam 是 new 出æ�¥çš„ FilePreviewHashResult,由对è¯�框 delete。 +#define WM_PREVIEW_HASH_RESP (WM_USER + 0x307) + +// COMMAND_FILE_HASH å“�应的解æž�结果(对è¯�框 UI 线程消费)。 +struct FilePreviewHashResult { + std::string path; // 请求里原样回显的路径(GBK) + uint32_t status = 0; + uint32_t err = 0; + uint64_t size = 0; + uint64_t mtimeMs = 0; + std::string sha256; // 64 ä½�å°�写å��六进制;status != FILE_HASH_OK 时为空 +}; // 设备级文件传输闸门:预览与 MCP çš„ download/readã€�uploadã€�C2C 都在å�Œä¸€ä¸´ç•ŒåŒºè®¤é¢†è®¾å¤‡ã€‚ // C2C 需è¦�原å­�认领 src+dst 两个键。owner å�ªç”¨äºŽé‡Šæ”¾æ ¡éªŒï¼Œé�¿å…�旧会è¯�误放新会è¯�。 diff --git a/server/2015Remote/lang/de_DE.ini b/server/2015Remote/lang/de_DE.ini index a043f9e..9699804 100644 --- a/server/2015Remote/lang/de_DE.ini +++ b/server/2015Remote/lang/de_DE.ini @@ -2370,3 +2370,4 @@ MCP ÎÞ·¨»ñÈ¡ÎļþÐÅÏ¢£¬ÇëË¢ÐÂĿ¼ºóÖØÊÔ=Dateiinformationen nicht verfuegbar; Verzeichnis aktualisieren und erneut versuchen Îļþ½Ï´ó£¬²»Ö§³ÖÔ¤ÀÀ£º%s£¨%llu MB£¬ÉÏÏÞ %u MB£©=Datei zu gross fuer die Vorschau: %s (%llu MB, Grenze %u MB) ÕýÔÚÔ¤ÀÀ %s ...=Vorschau von %s ... +»ñȡԶ³ÌÎļþÐÅϢʧ°Ü£¨´íÎóÂë %u£©=Dateiinformationen konnten nicht abgerufen werden (Fehler %u) diff --git a/server/2015Remote/lang/en_US.ini b/server/2015Remote/lang/en_US.ini index fe2ef51..02cef4f 100644 --- a/server/2015Remote/lang/en_US.ini +++ b/server/2015Remote/lang/en_US.ini @@ -2381,3 +2381,4 @@ MCP ÎÞ·¨»ñÈ¡ÎļþÐÅÏ¢£¬ÇëË¢ÐÂĿ¼ºóÖØÊÔ=Cannot get file information; refresh the directory and retry Îļþ½Ï´ó£¬²»Ö§³ÖÔ¤ÀÀ£º%s£¨%llu MB£¬ÉÏÏÞ %u MB£©=File too large to preview: %s (%llu MB, limit %u MB) ÕýÔÚÔ¤ÀÀ %s ...=Previewing %s ... +»ñȡԶ³ÌÎļþÐÅϢʧ°Ü£¨´íÎóÂë %u£©=Failed to get remote file information (error %u) diff --git a/server/2015Remote/lang/zh_TW.ini b/server/2015Remote/lang/zh_TW.ini index 7db9890..8f4937c 100644 --- a/server/2015Remote/lang/zh_TW.ini +++ b/server/2015Remote/lang/zh_TW.ini @@ -2374,3 +2374,4 @@ MCP ÎÞ·¨»ñÈ¡ÎļþÐÅÏ¢£¬ÇëË¢ÐÂĿ¼ºóÖØÊÔ=Ÿo·¨È¡µÃ™n°¸ÙYÓ�£¬ÕˆÖØÐÂÕûÀíÄ¿ä›ááÖØÔ‡ Îļþ½Ï´ó£¬²»Ö§³ÖÔ¤ÀÀ£º%s£¨%llu MB£¬ÉÏÏÞ %u MB£©=™n°¸ß^´ó£¬²»Ö§Ô®îAÓ[£º%s£¨%llu MB£¬ÉÏÏÞ %u MB£© ÕýÔÚÔ¤ÀÀ %s ...=ÕýÔÚîAÓ[ %s ... +»ñȡԶ³ÌÎļþÐÅϢʧ°Ü£¨´íÎóÂë %u£©=È¡µÃßh¶Ë™n°¸ÙYÓ�ʧ”¡£¨åeÕ`´a %u£©