From 6b347e7310c501b826785e9639d962ba1d448b4b Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 23 Sep 2020 00:56:54 +0200 Subject: Add matrix image upload --- src/Storage.cpp | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) (limited to 'src/Storage.cpp') diff --git a/src/Storage.cpp b/src/Storage.cpp index 0c3479a..f82aba3 100644 --- a/src/Storage.cpp +++ b/src/Storage.cpp @@ -121,6 +121,16 @@ namespace QuickMedia { return 0; } + int file_get_size(const Path &path, size_t *size) { + struct stat file_stat; + if(stat(path.data.c_str(), &file_stat) == 0 && S_ISREG(file_stat.st_mode)) { + *size = file_stat.st_size; + return 0; + } + *size = 0; + return -1; + } + int file_overwrite(const Path &path, const std::string &data) { FILE *file = fopen(path.data.c_str(), "wb"); if(!file) @@ -135,20 +145,38 @@ namespace QuickMedia { } void for_files_in_dir(const Path &path, FileIteratorCallback callback) { - for(auto &p : std::filesystem::directory_iterator(path.data)) { - if(!callback(p.path())) - break; + try { + for(auto &p : std::filesystem::directory_iterator(path.data)) { + if(!callback(p.path())) + break; + } + } catch(const std::filesystem::filesystem_error &err) { + fprintf(stderr, "Failed to list files in directory %s, error: %s\n", path.data.c_str(), err.what()); + return; + } + } + + static std::filesystem::file_time_type file_get_filetime_or(const std::filesystem::directory_entry &path, std::filesystem::file_time_type default_value) { + try { + return path.last_write_time(); + } catch(const std::filesystem::filesystem_error &err) { + return default_value; } } void for_files_in_dir_sort_last_modified(const Path &path, FileIteratorCallback callback) { std::vector paths; - for(auto &p : std::filesystem::directory_iterator(path.data)) { - paths.push_back(p); + try { + for(auto &p : std::filesystem::directory_iterator(path.data)) { + paths.push_back(p); + } + } catch(const std::filesystem::filesystem_error &err) { + fprintf(stderr, "Failed to list files in directory %s, error: %s\n", path.data.c_str(), err.what()); + return; } std::sort(paths.begin(), paths.end(), [](const std::filesystem::directory_entry &path1, std::filesystem::directory_entry &path2) { - return path1.last_write_time() > path2.last_write_time(); + return file_get_filetime_or(path1, std::filesystem::file_time_type::min()) > file_get_filetime_or(path2, std::filesystem::file_time_type::min()); }); for(auto &p : paths) { -- cgit v1.2.3