Commit 1b78f7ae by xzl

文件下载失败删除不完整文件

parent bc644603
...@@ -32,6 +32,7 @@ void HttpDownloader::startDownload(const string& url, const string& filePath,boo ...@@ -32,6 +32,7 @@ void HttpDownloader::startDownload(const string& url, const string& filePath,boo
auto strErr = StrPrinter << "打开文件失败:" << filePath << endl; auto strErr = StrPrinter << "打开文件失败:" << filePath << endl;
throw std::runtime_error(strErr); throw std::runtime_error(strErr);
} }
_bDownloadSuccess = false;
if(bAppend){ if(bAppend){
auto currentLen = ftell(_saveFile); auto currentLen = ftell(_saveFile);
if(currentLen){ if(currentLen){
...@@ -67,6 +68,7 @@ void HttpDownloader::onResponseBody(const char* buf, size_t size, size_t recvedS ...@@ -67,6 +68,7 @@ void HttpDownloader::onResponseBody(const char* buf, size_t size, size_t recvedS
void HttpDownloader::onResponseCompleted() { void HttpDownloader::onResponseCompleted() {
closeFile(); closeFile();
_bDownloadSuccess = true;
if(_onResult){ if(_onResult){
_onResult(Err_success,"success",_filePath.data()); _onResult(Err_success,"success",_filePath.data());
_onResult = nullptr; _onResult = nullptr;
...@@ -75,6 +77,9 @@ void HttpDownloader::onResponseCompleted() { ...@@ -75,6 +77,9 @@ void HttpDownloader::onResponseCompleted() {
void HttpDownloader::onDisconnect(const SockException &ex) { void HttpDownloader::onDisconnect(const SockException &ex) {
closeFile(); closeFile();
if(!_bDownloadSuccess){
File::delete_file(_filePath.data());
}
if(_onResult){ if(_onResult){
_onResult(ex.getErrCode(),ex.what(),_filePath.data()); _onResult(ex.getErrCode(),ex.what(),_filePath.data());
_onResult = nullptr; _onResult = nullptr;
......
...@@ -38,6 +38,7 @@ private: ...@@ -38,6 +38,7 @@ private:
FILE *_saveFile = nullptr; FILE *_saveFile = nullptr;
string _filePath; string _filePath;
onDownloadResult _onResult; onDownloadResult _onResult;
bool _bDownloadSuccess = false;
}; };
} /* namespace Http */ } /* namespace Http */
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论