Added image filename implementation
This commit is contained in:
81
main.cpp
81
main.cpp
@@ -1,3 +1,6 @@
|
|||||||
|
// Start using chrono when C++20 becomes available
|
||||||
|
// #include <chrono>
|
||||||
|
#include <filesystem>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <map>
|
#include <map>
|
||||||
@@ -8,7 +11,13 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
// Stop using these when C++20 becomes available
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#include "httplib.h"
|
#include "httplib.h"
|
||||||
#include "json.hpp"
|
#include "json.hpp"
|
||||||
@@ -19,6 +28,7 @@
|
|||||||
using nlohmann::json;
|
using nlohmann::json;
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
constexpr int API_KEY_LENGHT = 16;
|
constexpr int API_KEY_LENGHT = 16;
|
||||||
constexpr char DEFAULT_IMAGE_URL[] = "https://trmnl.com/images/setup/setup-logo.bmp";
|
constexpr char DEFAULT_IMAGE_URL[] = "https://trmnl.com/images/setup/setup-logo.bmp";
|
||||||
@@ -92,6 +102,54 @@ bool reload_container(TRMNLContainer& container, const string& filename)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string get_timestamp_from_filename(const string& filepath)
|
||||||
|
{
|
||||||
|
char result[128] = "";
|
||||||
|
|
||||||
|
struct stat status;
|
||||||
|
stat(filepath.c_str(), &status);
|
||||||
|
|
||||||
|
// Time of last modification
|
||||||
|
time_t file_time = status.st_mtim.tv_sec;
|
||||||
|
|
||||||
|
tm* file_tm;
|
||||||
|
file_tm = gmtime(&file_time);
|
||||||
|
|
||||||
|
snprintf(result, sizeof(result) - 1, "%04d-%02d-%02dT%02d:%02d:%02d",
|
||||||
|
file_tm->tm_year + 1900,
|
||||||
|
file_tm->tm_mon + 1,
|
||||||
|
file_tm->tm_mday,
|
||||||
|
file_tm->tm_hour,
|
||||||
|
file_tm->tm_min,
|
||||||
|
file_tm->tm_sec
|
||||||
|
);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use this when C++20 becomes available
|
||||||
|
#if 0
|
||||||
|
string get_timestamp_from_file_time(const fs::file_time_type& ftime)
|
||||||
|
{
|
||||||
|
char result[128] = "";
|
||||||
|
time_t file_time = std::chrono::system_clock::to_time_t(std::chrono::file_clock::to_sys(ftime));
|
||||||
|
|
||||||
|
tm* file_tm;
|
||||||
|
file_tm = gmtime(&file_time);
|
||||||
|
|
||||||
|
snprintf(result, sizeof(result) - 1, "%04d-%02d-%02dT%02d:%02d:%02d",
|
||||||
|
file_tm->tm_year + 1900,
|
||||||
|
file_tm->tm_mon + 1,
|
||||||
|
file_tm->tm_mday,
|
||||||
|
file_tm->tm_hour,
|
||||||
|
file_tm->tm_min,
|
||||||
|
file_tm->tm_sec
|
||||||
|
);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
// First string is image filename for URL
|
// First string is image filename for URL
|
||||||
// Second string is timestamp for TRMNL filename
|
// Second string is timestamp for TRMNL filename
|
||||||
pair<string, string> find_image_for_friendly(const string& folder_images, const string& friendly_id)
|
pair<string, string> find_image_for_friendly(const string& folder_images, const string& friendly_id)
|
||||||
@@ -99,7 +157,22 @@ pair<string, string> find_image_for_friendly(const string& folder_images, const
|
|||||||
pair<string, string> result = {"", ""};
|
pair<string, string> result = {"", ""};
|
||||||
set<string> permitted_extensions = { ".bmp", ".png" };
|
set<string> permitted_extensions = { ".bmp", ".png" };
|
||||||
|
|
||||||
// TODO: Implement
|
if (!fs::is_directory(folder_images))
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const fs::directory_entry& entry : fs::directory_iterator(folder_images))
|
||||||
|
{
|
||||||
|
if ((entry.path().stem() == friendly_id) &&
|
||||||
|
(0 != permitted_extensions.count(entry.path().extension())))
|
||||||
|
{
|
||||||
|
result.first = entry.path().filename();
|
||||||
|
result.second = get_timestamp_from_filename(entry.path());
|
||||||
|
// Change to this when C++20 becomes available
|
||||||
|
// result.second = get_timestamp_from_file_time(entry.last_write_time());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -170,6 +243,12 @@ int main(int argc, char **argv)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!fs::is_directory(folder_images))
|
||||||
|
{
|
||||||
|
cout << "filepath for images is not a folder" << endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
if (!cert_file.empty() && !key_file.empty())
|
if (!cert_file.empty() && !key_file.empty())
|
||||||
{
|
{
|
||||||
// TODO: Implement SSL Server Properly
|
// TODO: Implement SSL Server Properly
|
||||||
|
|||||||
Reference in New Issue
Block a user