aboutsummaryrefslogtreecommitdiff
path: root/src/Storage.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2020-09-23 00:56:54 +0200
committerdec05eba <dec05eba@protonmail.com>2020-09-23 01:00:37 +0200
commit6b347e7310c501b826785e9639d962ba1d448b4b (patch)
tree6d84b547078d009565a75ac2df42423c06c578b7 /src/Storage.cpp
parenta8e0846a7c111a8d5b5cf8592ecb9b9bbd15ce26 (diff)
Add matrix image upload
Diffstat (limited to 'src/Storage.cpp')
-rw-r--r--src/Storage.cpp40
1 files changed, 34 insertions, 6 deletions
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<std::filesystem::directory_entry> 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) {