MCPcopy Create free account
hub / github.com/DiscordMessenger/dm / DownloadFileResponse

Function DownloadFileResponse

src/windows/WinUtils.cpp:852–909  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

850}
851
852void DownloadFileResponse(NetRequest* pRequest)
853{
854 HWND hWnd = (HWND)pRequest->key;
855
856 if (pRequest->result == HTTP_PROGRESS)
857 {
858 // N.B. totally safe to access because corresponding networker thread is locked up waiting for us
859 pRequest->m_bCancelOp = ProgressDialog::Update(pRequest->key, pRequest->GetOffset(), pRequest->GetTotalBytes());
860 return;
861 }
862
863 if (pRequest->result != HTTP_OK)
864 {
865 DownloadOnRequestFail(hWnd, pRequest);
866 return;
867 }
868
869 // Since the response is passed in as a string, this should do for getting the binary stuff out of it
870 const uint8_t* pData = (const uint8_t*)pRequest->response.data();
871 const size_t nSize = pRequest->response.size();
872
873 LPTSTR fileName = ConvertCppStringToTString(pRequest->additional_data);
874 HANDLE hnd = CreateFile(fileName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
875 DWORD written = 0;
876 if (hnd == INVALID_HANDLE_VALUE)
877 goto _error;
878
879 if (!WriteFile(hnd, pData, (DWORD) nSize, &written, NULL))
880 goto _error;
881
882 if (written != (DWORD)nSize)
883 goto _error;
884
885 CloseHandle(hnd);
886 hnd = INVALID_HANDLE_VALUE;
887
888 // yay!
889 DbgPrintW("File saved: %s", pRequest->additional_data.c_str());
890 ProgressDialog::Done(pRequest->key);
891 SendMessage(hWnd, WM_IMAGESAVED, 0, (LPARAM)fileName);
892 free(fileName);
893 return;
894
895_error:
896 if (hnd != INVALID_HANDLE_VALUE)
897 CloseHandle(hnd);
898
899 TCHAR buff[4096];
900 LPTSTR errorStr = GetTStringFromHResult(GetLastError());
901 WAsnprintf(buff, _countof(buff), TmGetTString(IDS_ERROR_SAVING_FILE), fileName, errorStr);
902 buff[_countof(buff) - 1] = 0;
903 free(errorStr);
904 free(fileName);
905 MessageBox(hWnd, buff, TmGetTString(IDS_PROGRAM_NAME), MB_ICONERROR);
906 ProgressDialog::Done(pRequest->key);
907 SendMessage(hWnd, WM_IMAGECLEARSAVE, 0, 0);
908 return;
909}

Callers

nothing calls this directly

Calls 8

DownloadOnRequestFailFunction · 0.85
DbgPrintWFunction · 0.85
GetTStringFromHResultFunction · 0.85
GetOffsetMethod · 0.80
GetTotalBytesMethod · 0.80
dataMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected