diff options
author | George Hazan <george.hazan@gmail.com> | 2024-08-14 18:14:57 +0300 |
---|---|---|
committer | George Hazan <george.hazan@gmail.com> | 2024-08-14 18:14:57 +0300 |
commit | 260af6626bc7681e3c92590d5a09c3019d15142c (patch) | |
tree | 3900a83ef5c0391b2e2e66f438e2f9bbc0f0a559 /src | |
parent | 59dcbfe729b33c924d94913f00534ebf2acf6d54 (diff) |
Netlib_DownloadFile not to create an empty file if smth going wrong
Diffstat (limited to 'src')
-rw-r--r-- | src/mir_app/src/netlib.h | 3 | ||||
-rw-r--r-- | src/mir_app/src/netlib_http.cpp | 15 |
2 files changed, 13 insertions, 5 deletions
diff --git a/src/mir_app/src/netlib.h b/src/mir_app/src/netlib.h index bd122db088..26e82a91c8 100644 --- a/src/mir_app/src/netlib.h +++ b/src/mir_app/src/netlib.h @@ -92,8 +92,9 @@ public: class MFileChunkStorage : public MChunkHandler
{
- int fileId;
+ int fileId = -1;
size_t prevBlocks = 0;
+ const MFilePath &wszPath;
pfnDownloadCallback pCallback;
void *pCallbackInfo;
diff --git a/src/mir_app/src/netlib_http.cpp b/src/mir_app/src/netlib_http.cpp index ee05831d17..96c70867f5 100644 --- a/src/mir_app/src/netlib_http.cpp +++ b/src/mir_app/src/netlib_http.cpp @@ -1131,10 +1131,10 @@ MIR_APP_DLL(MHttpResponse *) Netlib_HttpTransaction(HNETLIBUSER nlu, MHttpReques /////////////////////////////////////////////////////////////////////////////////////////
MFileChunkStorage::MFileChunkStorage(const MFilePath &_1, pfnDownloadCallback _2, void *_3) :
+ wszPath(_1),
pCallback(_2),
pCallbackInfo(_3)
{
- fileId = _wopen(_1, _O_WRONLY | _O_TRUNC | _O_BINARY | _O_CREAT, _S_IREAD | _S_IWRITE);
}
MFileChunkStorage::~MFileChunkStorage()
@@ -1156,6 +1156,12 @@ void MFileChunkStorage::apply(MHttpResponse *nlhr) bool MFileChunkStorage::updateChunk(const void *pData, size_t cbLen)
{
+ if (fileId == -1) {
+ fileId = _wopen(wszPath, _O_WRONLY | _O_TRUNC | _O_BINARY | _O_CREAT, _S_IREAD | _S_IWRITE);
+ if (fileId == -1)
+ return false;
+ }
+
if (cbLen != (unsigned)_write(fileId, pData, unsigned(cbLen))) {
_close(fileId);
fileId = -1;
@@ -1180,9 +1186,10 @@ MIR_APP_DLL(MHttpResponse *) Netlib_DownloadFile( pfnDownloadCallback pCallback,
void *pCallbackInfo)
{
- MFileChunkStorage storage(wszFileName, pCallback, pCallbackInfo);
- if (!storage)
- return nullptr;
+ // prevent server from bothering with gzip/deflate while saving to file
+ if (!nlhr->FindHeader("Accept-Encoding"))
+ nlhr->AddHeader("Accept-Encoding", "none");
+ MFileChunkStorage storage(wszFileName, pCallback, pCallbackInfo);
return HttpTransactionWorker(nlu, nlhr, storage);
}
|