77#include < optional>
88#include < ostream>
99#include < utility>
10- #include " download_service.h"
1110#include " utils/format_utils.h"
1211#include " utils/huggingface_utils.h"
1312#include " utils/logging_utils.h"
@@ -148,11 +147,10 @@ cpp::result<bool, std::string> DownloadService::Download(
148147 std::string mode = " wb" ;
149148 if (std::filesystem::exists (download_item.localPath ) &&
150149 download_item.bytes .has_value ()) {
151- curl_off_t existing_file_size = GetLocalFileSize (download_item.localPath );
152- if (existing_file_size == -1 ) {
153- CLI_LOG (" Cannot get file size: " << download_item.localPath .string ()
154- << " . Start download over!" );
155- } else {
150+ try {
151+ curl_off_t existing_file_size =
152+ std::filesystem::file_size (download_item.localPath );
153+
156154 CTL_INF (" Existing file size: " << download_item.downloadUrl << " - "
157155 << download_item.localPath .string ()
158156 << " - " << existing_file_size);
@@ -186,6 +184,9 @@ cpp::result<bool, std::string> DownloadService::Download(
186184 return false ;
187185 }
188186 }
187+ } catch (const std::filesystem::filesystem_error& e) {
188+ CLI_LOG (" Cannot get file size: "
189+ << e.what () << download_item.localPath .string () << " \n " );
189190 }
190191 }
191192
@@ -205,12 +206,12 @@ cpp::result<bool, std::string> DownloadService::Download(
205206 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L );
206207
207208 if (mode == " ab" ) {
208- auto local_file_size = GetLocalFileSize (download_item. localPath );
209- if (local_file_size != - 1 ) {
210- curl_easy_setopt (curl, CURLOPT_RESUME_FROM_LARGE,
211- GetLocalFileSize (download_item. localPath ) );
212- } else {
213- CTL_ERR (" Cannot get file size: " << download_item. localPath . string () );
209+ try {
210+ curl_off_t local_file_size =
211+ std::filesystem::file_size (download_item. localPath );
212+ curl_easy_setopt (curl, CURLOPT_RESUME_FROM_LARGE, local_file_size );
213+ } catch ( const std::filesystem::filesystem_error& e) {
214+ CTL_ERR (" Cannot get file size: " << e. what () << ' \n ' );
214215 }
215216 }
216217
@@ -225,23 +226,6 @@ cpp::result<bool, std::string> DownloadService::Download(
225226 curl_easy_cleanup (curl);
226227 return true ;
227228}
228-
229- curl_off_t DownloadService::GetLocalFileSize (
230- const std::filesystem::path& path) const {
231- auto file = fopen (path.string ().c_str (), " r" );
232- if (!file) {
233- return -1 ;
234- }
235-
236- if (fseek64 (file, 0 , SEEK_END) != 0 ) {
237- return -1 ;
238- }
239-
240- auto file_size = ftell64 (file);
241- fclose (file);
242- return file_size;
243- }
244-
245229void DownloadService::WorkerThread () {
246230 while (!stop_flag_) {
247231 DownloadTask task;
0 commit comments