From c26e48e43c6c77ab7876c1bfd9c36f0612a68715 Mon Sep 17 00:00:00 2001 From: JDierkse Date: Thu, 16 Mar 2023 13:14:50 +0100 Subject: [PATCH] Add Folder check and Folder creation --- Recorder/FoscamRecorder.cpp | 83 ++++++++++++++++++++++++++++++++--- Recorder/FoscamRecorder.h | 1 + Recorder/RTSPRecorder.cpp | 83 ++++++++++++++++++++++++++++++++--- Recorder/RTSPRecorder.h | 1 + Recorder/Util.cpp | 25 ----------- Recorder/Util.h | 15 ------- Recorder/VStarCamRecorder.cpp | 81 +++++++++++++++++++++++++++++++--- Recorder/VStarCamRecorder.h | 1 + Recorder/WatchBotRecorder.cpp | 81 +++++++++++++++++++++++++++++++--- Recorder/WatchBotRecorder.h | 1 + Util/Makefile | 1 + Util/Util.cpp | 46 +++++++++++++++++++ Util/Util.h | 18 ++++++++ 13 files changed, 376 insertions(+), 61 deletions(-) delete mode 100644 Recorder/Util.cpp delete mode 100644 Recorder/Util.h create mode 120000 Util/Makefile create mode 100644 Util/Util.cpp create mode 100644 Util/Util.h diff --git a/Recorder/FoscamRecorder.cpp b/Recorder/FoscamRecorder.cpp index 5c419a2..13061e6 100644 --- a/Recorder/FoscamRecorder.cpp +++ b/Recorder/FoscamRecorder.cpp @@ -1,10 +1,11 @@ #include "FoscamRecorder.h" -#include "Util.h" +#include "Util/Util.h" #include #include #include #include #include +#include #include #include @@ -26,7 +27,31 @@ std::string FoscamRecorder::Snapshot(ctpl::thread_pool* pThreadPool, const std:: { Logging::Log(Logging::Severity::Debug, "FoscamRecorder Snapshot"); - std::string dateTimeString = GetDateTimeString(); + std::string folderName = GetFolderName(m_settings); + if (!Util::FolderExists(folderName)) + { + try + { + Util::CreateFolder(folderName); + } + catch (const std::exception& e) + { + std::cerr << "Exception caught" << std::endl; + + std::stringstream ss; + ss << "Type : " << typeid(e).name() << std::endl; + ss << "ERROR: " << e.what() << std::endl; + + Logging::Log(Logging::Severity::Error, ss.str()); + + return std::string(); + } + } + + //if (m_pRecorderMutex->IsRecording(m_settings.IpAddress)) + // return std::string(); + + std::string dateTimeString = Util::GetDateTimeString(); std::string fileName = GetFileName(m_settings, "jpg", dateTimeString); Http::HttpClient* pHttpClient = m_pHttpClient; @@ -40,7 +65,27 @@ std::string FoscamRecorder::MultiSnapshot(ctpl::thread_pool* pThreadPool, const { Logging::Log(Logging::Severity::Debug, "FoscamRecorder MultiSnapshot"); - std::string dateTimeString = GetDateTimeString(); + std::string folderName = GetFolderName(m_settings); + if (!Util::FolderExists(folderName)) + { + try + { + Util::CreateFolder(folderName); + } + catch (const std::exception& e) + { + std::cerr << "Exception caught" << std::endl; + + std::stringstream ss; + ss << "Type : " << typeid(e).name() << std::endl; + ss << "ERROR: " << e.what() << std::endl; + + Logging::Log(Logging::Severity::Error, ss.str()); + + return std::string(); + } + } + std::string dateTimeString = Util::GetDateTimeString(); std::string fileName = GetFileName(m_settings, "jpg", dateTimeString); Http::HttpClient* pHttpClient = m_pHttpClient; @@ -58,7 +103,28 @@ std::string FoscamRecorder::Video(ctpl::thread_pool* pThreadPool, const std::str std::stringstream url; url << "rtsp://" << m_settings.Username << ":" << m_settings.Password << "@" << m_settings.IpAddress << ":" << port << "/videoSub"; - std::string dateTimeString = GetDateTimeString(); + std::string folderName = GetFolderName(m_settings); + if (!Util::FolderExists(folderName)) + { + try + { + Util::CreateFolder(folderName); + } + catch (const std::exception& e) + { + std::cerr << "Exception caught" << std::endl; + + std::stringstream ss; + ss << "Type : " << typeid(e).name() << std::endl; + ss << "ERROR: " << e.what() << std::endl; + + Logging::Log(Logging::Severity::Error, ss.str()); + + return std::string(); + } + } + + std::string dateTimeString = Util::GetDateTimeString(); std::string fileName = GetFileName(m_settings, "mp4", dateTimeString); std::stringstream command; @@ -71,11 +137,18 @@ std::string FoscamRecorder::Video(ctpl::thread_pool* pThreadPool, const std::str return fileName; } -std::string FoscamRecorder::GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString) +std::string FoscamRecorder::GetFolderName(const Settings& settings) { filesystem::path fileLocation(settings.Path); fileLocation = fileLocation / filesystem::path(settings.IpAddress); + return fileLocation.str(); +} + +std::string FoscamRecorder::GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString) +{ + filesystem::path fileLocation(GetFolderName(settings)); + std::string fileName = std::string(dateTimeString).append(".").append(extension); filesystem::path fullFileName = fileLocation / filesystem::path(fileName); diff --git a/Recorder/FoscamRecorder.h b/Recorder/FoscamRecorder.h index 023a83b..7f91da7 100644 --- a/Recorder/FoscamRecorder.h +++ b/Recorder/FoscamRecorder.h @@ -26,6 +26,7 @@ public: virtual std::string Video(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg) override; private: + static std::string GetFolderName(const Settings& settings); static std::string GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString); static void Snapshots(Http::HttpClient* pHttpClient, const Settings& settings, std::string dateTimeString, int numberOfImages); static Image::JpegImage Snapshot(Http::HttpClient* pHttpClient, const Settings& settings); diff --git a/Recorder/RTSPRecorder.cpp b/Recorder/RTSPRecorder.cpp index 8ed7802..ed15fb9 100644 --- a/Recorder/RTSPRecorder.cpp +++ b/Recorder/RTSPRecorder.cpp @@ -1,7 +1,8 @@ #include "RTSPRecorder.h" -#include "Util.h" +#include "Util/Util.h" #include #include +#include namespace CameraRecorder { @@ -21,7 +22,28 @@ std::string RTSPRecorder::Snapshot(ctpl::thread_pool* pThreadPool, const std::st { Logging::Log(Logging::Severity::Debug, "RTSPRecorder Snapshot"); - std::string dateTimeString = GetDateTimeString(); + std::string folderName = GetFolderName(m_settings); + if (!Util::FolderExists(folderName)) + { + try + { + Util::CreateFolder(folderName); + } + catch (const std::exception& e) + { + std::cerr << "Exception caught" << std::endl; + + std::stringstream ss; + ss << "Type : " << typeid(e).name() << std::endl; + ss << "ERROR: " << e.what() << std::endl; + + Logging::Log(Logging::Severity::Error, ss.str()); + + return std::string(); + } + } + + std::string dateTimeString = Util::GetDateTimeString(); std::string fileName = GetFileName(m_settings, "jpg", dateTimeString, 0); Http::HttpClient* pHttpClient = m_pHttpClient; @@ -35,7 +57,28 @@ std::string RTSPRecorder::MultiSnapshot(ctpl::thread_pool* pThreadPool, const st { Logging::Log(Logging::Severity::Debug, "RTSPRecorder MultiSnapshot"); - std::string dateTimeString = GetDateTimeString(); + std::string folderName = GetFolderName(m_settings); + if (!Util::FolderExists(folderName)) + { + try + { + Util::CreateFolder(folderName); + } + catch (const std::exception& e) + { + std::cerr << "Exception caught" << std::endl; + + std::stringstream ss; + ss << "Type : " << typeid(e).name() << std::endl; + ss << "ERROR: " << e.what() << std::endl; + + Logging::Log(Logging::Severity::Error, ss.str()); + + return std::string(); + } + } + + std::string dateTimeString = Util::GetDateTimeString(); std::string fileName = GetFileName(m_settings, "jpg", dateTimeString, 0); Http::HttpClient* pHttpClient = m_pHttpClient; @@ -52,7 +95,28 @@ std::string RTSPRecorder::Video(ctpl::thread_pool* pThreadPool, const std::strin std::stringstream url; url << "rtsp://" << m_settings.Username << ":" << m_settings.Password << "@" << m_settings.IpAddress << ":" << m_settings.Port << m_settings.URL; - std::string dateTimeString = GetDateTimeString(); + std::string folderName = GetFolderName(m_settings); + if (!Util::FolderExists(folderName)) + { + try + { + Util::CreateFolder(folderName); + } + catch (const std::exception& e) + { + std::cerr << "Exception caught" << std::endl; + + std::stringstream ss; + ss << "Type : " << typeid(e).name() << std::endl; + ss << "ERROR: " << e.what() << std::endl; + + Logging::Log(Logging::Severity::Error, ss.str()); + + return std::string(); + } + } + + std::string dateTimeString = Util::GetDateTimeString(); std::string fileName = GetFileName(m_settings, "mp4", dateTimeString, 0); std::stringstream command; @@ -65,12 +129,19 @@ std::string RTSPRecorder::Video(ctpl::thread_pool* pThreadPool, const std::strin return fileName; } -std::string RTSPRecorder::GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString, int imageIndex) +std::string RTSPRecorder::GetFolderName(const Settings& settings) { filesystem::path fileLocation(settings.Path); fileLocation = fileLocation / filesystem::path(settings.IpAddress); - std::string fileName = std::string(dateTimeString).append(".").append(extension); + return fileLocation.str(); +} + +std::string RTSPRecorder::GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString, int imageIndex) +{ + filesystem::path fileLocation(GetFolderName(settings)); + + std::string fileName = std::string(dateTimeString).append("_").append(std::to_string(imageIndex)).append(".").append(extension); filesystem::path fullFileName = fileLocation / filesystem::path(fileName); return fullFileName.str(); diff --git a/Recorder/RTSPRecorder.h b/Recorder/RTSPRecorder.h index 52e4d2b..f836080 100644 --- a/Recorder/RTSPRecorder.h +++ b/Recorder/RTSPRecorder.h @@ -26,6 +26,7 @@ public: virtual std::string Video(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg) override; private: + static std::string GetFolderName(const Settings& settings); static std::string GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString, int imageIndex); static void Snapshots(const std::string& ffmpeg, const Settings& settings, std::string dateTimeString, int numberOfImages); static void Snapshot(const std::string& ffmpeg, const Settings& settings, const filesystem::path& fileName); diff --git a/Recorder/Util.cpp b/Recorder/Util.cpp deleted file mode 100644 index 806e62e..0000000 --- a/Recorder/Util.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include "Util.h" -#include -#include - - -namespace CameraRecorder { -namespace Recorder { - -std::string GetDateTimeString(bool omitSeparator) -{ - std::array buffer; - buffer.fill(0); - std::time_t t = std::time(nullptr); - std::tm* tm = std::localtime(&t); - - if (omitSeparator) - strftime(buffer.data(), sizeof(buffer), "%Y%m%d%H%M%S", tm); - else - strftime(buffer.data(), sizeof(buffer), "%Y%m%d-%H%M%S", tm); - - return buffer.data(); -} - -} // namespace Recorder -} // namespace CameraRecorder diff --git a/Recorder/Util.h b/Recorder/Util.h deleted file mode 100644 index 8830c22..0000000 --- a/Recorder/Util.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef RECORDER_UTIL_H -#define RECORDER_UTIL_H - -#include - - -namespace CameraRecorder { -namespace Recorder { - -std::string GetDateTimeString(bool omitSeparator = false); - -} // namespace Recorder -} // namespace CameraRecorder - -#endif // RECORDER_UTIL_H diff --git a/Recorder/VStarCamRecorder.cpp b/Recorder/VStarCamRecorder.cpp index ca4ca26..2570528 100644 --- a/Recorder/VStarCamRecorder.cpp +++ b/Recorder/VStarCamRecorder.cpp @@ -1,8 +1,9 @@ #include "VStarCamRecorder.h" -#include "Util.h" +#include "Util/Util.h" #include #include #include +#include #include @@ -23,7 +24,28 @@ std::string VStarCamRecorder::Snapshot(ctpl::thread_pool* pThreadPool, const std { Logging::Log(Logging::Severity::Debug, "VStarCamRecorder Snapshot"); - std::string dateTimeString = GetDateTimeString(true); + std::string folderName = GetFolderName(m_settings); + if (!Util::FolderExists(folderName)) + { + try + { + Util::CreateFolder(folderName); + } + catch (const std::exception& e) + { + std::cerr << "Exception caught" << std::endl; + + std::stringstream ss; + ss << "Type : " << typeid(e).name() << std::endl; + ss << "ERROR: " << e.what() << std::endl; + + Logging::Log(Logging::Severity::Error, ss.str()); + + return std::string(); + } + } + + std::string dateTimeString = Util::GetDateTimeString(true); std::string fileName = GetFileName(m_settings, "jpg", dateTimeString, 0); Http::HttpClient* pHttpClient = m_pHttpClient; @@ -37,7 +59,28 @@ std::string VStarCamRecorder::MultiSnapshot(ctpl::thread_pool* pThreadPool, cons { Logging::Log(Logging::Severity::Debug, "VStarCamRecorder MultiSnapshot"); - std::string dateTimeString = GetDateTimeString(true); + std::string folderName = GetFolderName(m_settings); + if (!Util::FolderExists(folderName)) + { + try + { + Util::CreateFolder(folderName); + } + catch (const std::exception& e) + { + std::cerr << "Exception caught" << std::endl; + + std::stringstream ss; + ss << "Type : " << typeid(e).name() << std::endl; + ss << "ERROR: " << e.what() << std::endl; + + Logging::Log(Logging::Severity::Error, ss.str()); + + return std::string(); + } + } + + std::string dateTimeString = Util::GetDateTimeString(true); std::string fileName = GetFileName(m_settings, "jpg", dateTimeString, 0); Http::HttpClient* pHttpClient = m_pHttpClient; @@ -55,7 +98,28 @@ std::string VStarCamRecorder::Video(ctpl::thread_pool* pThreadPool, const std::s std::stringstream url; url << "rtsp://" << m_settings.Username << ":" << m_settings.Password << "@" << m_settings.IpAddress << ":" << port << "/udp/av0_0"; - std::string dateTimeString = GetDateTimeString(true); + std::string folderName = GetFolderName(m_settings); + if (!Util::FolderExists(folderName)) + { + try + { + Util::CreateFolder(folderName); + } + catch (const std::exception& e) + { + std::cerr << "Exception caught" << std::endl; + + std::stringstream ss; + ss << "Type : " << typeid(e).name() << std::endl; + ss << "ERROR: " << e.what() << std::endl; + + Logging::Log(Logging::Severity::Error, ss.str()); + + return std::string(); + } + } + + std::string dateTimeString = Util::GetDateTimeString(true); std::string fileName = GetFileName(m_settings, "mp4", dateTimeString, 0); std::stringstream command; @@ -68,11 +132,18 @@ std::string VStarCamRecorder::Video(ctpl::thread_pool* pThreadPool, const std::s return fileName; } -std::string VStarCamRecorder::GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString, int imageIndex) +std::string VStarCamRecorder::GetFolderName(const Settings& settings) { filesystem::path fileLocation(settings.Path); fileLocation = fileLocation / filesystem::path(settings.IpAddress); + return fileLocation.str(); +} + +std::string VStarCamRecorder::GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString, int imageIndex) +{ + filesystem::path fileLocation(GetFolderName(settings)); + std::string fileName = std::string("VSTA000000XXXXX_0_").append(dateTimeString).append("_").append(std::to_string(imageIndex)).append(".").append(extension); filesystem::path fullFileName = fileLocation / filesystem::path(fileName); diff --git a/Recorder/VStarCamRecorder.h b/Recorder/VStarCamRecorder.h index e5f9084..5932186 100644 --- a/Recorder/VStarCamRecorder.h +++ b/Recorder/VStarCamRecorder.h @@ -26,6 +26,7 @@ public: virtual std::string Video(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg) override; private: + static std::string GetFolderName(const Settings& settings); static std::string GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString, int imageIndex); static void Snapshots(Http::HttpClient* pHttpClient, const Settings& settings, std::string dateTimeString, int numberOfImages); static void Snapshot(Http::HttpClient* pHttpClient, const Settings& settings, const filesystem::path& fileName); diff --git a/Recorder/WatchBotRecorder.cpp b/Recorder/WatchBotRecorder.cpp index f582338..959494e 100644 --- a/Recorder/WatchBotRecorder.cpp +++ b/Recorder/WatchBotRecorder.cpp @@ -1,10 +1,11 @@ #include "WatchBotRecorder.h" -#include "Util.h" +#include "Util/Util.h" #include #include #include #include #include +#include #include #include @@ -26,7 +27,28 @@ std::string WatchBotRecorder::Snapshot(ctpl::thread_pool* pThreadPool, const std { Logging::Log(Logging::Severity::Debug, "WatchBotRecorder Snapshot"); - std::string dateTimeString = GetDateTimeString(); + std::string folderName = GetFolderName(m_settings); + if (!Util::FolderExists(folderName)) + { + try + { + Util::CreateFolder(folderName); + } + catch (const std::exception& e) + { + std::cerr << "Exception caught" << std::endl; + + std::stringstream ss; + ss << "Type : " << typeid(e).name() << std::endl; + ss << "ERROR: " << e.what() << std::endl; + + Logging::Log(Logging::Severity::Error, ss.str()); + + return std::string(); + } + } + + std::string dateTimeString = Util::GetDateTimeString(); std::string fileName = GetFileName(m_settings, "jpg", dateTimeString); Http::HttpClient* pHttpClient = m_pHttpClient; @@ -40,7 +62,28 @@ std::string WatchBotRecorder::MultiSnapshot(ctpl::thread_pool* pThreadPool, cons { Logging::Log(Logging::Severity::Debug, "WatchBotRecorder MultiSnapshot"); - std::string dateTimeString = GetDateTimeString(); + std::string folderName = GetFolderName(m_settings); + if (!Util::FolderExists(folderName)) + { + try + { + Util::CreateFolder(folderName); + } + catch (const std::exception& e) + { + std::cerr << "Exception caught" << std::endl; + + std::stringstream ss; + ss << "Type : " << typeid(e).name() << std::endl; + ss << "ERROR: " << e.what() << std::endl; + + Logging::Log(Logging::Severity::Error, ss.str()); + + return std::string(); + } + } + + std::string dateTimeString = Util::GetDateTimeString(); std::string fileName = GetFileName(m_settings, "jpg", dateTimeString); Http::HttpClient* pHttpClient = m_pHttpClient; @@ -57,7 +100,28 @@ std::string WatchBotRecorder::Video(ctpl::thread_pool* pThreadPool, const std::s std::stringstream url; url << "http://" << m_settings.IpAddress << ":" << m_settings.Port << "/videostream.asf?user=" << m_settings.Username << "&pwd=" << m_settings.Password; - std::string dateTimeString = GetDateTimeString(); + std::string folderName = GetFolderName(m_settings); + if (!Util::FolderExists(folderName)) + { + try + { + Util::CreateFolder(folderName); + } + catch (const std::exception& e) + { + std::cerr << "Exception caught" << std::endl; + + std::stringstream ss; + ss << "Type : " << typeid(e).name() << std::endl; + ss << "ERROR: " << e.what() << std::endl; + + Logging::Log(Logging::Severity::Error, ss.str()); + + return std::string(); + } + } + + std::string dateTimeString = Util::GetDateTimeString(); std::string fileName = GetFileName(m_settings, "mp4", dateTimeString); std::stringstream command; @@ -70,11 +134,18 @@ std::string WatchBotRecorder::Video(ctpl::thread_pool* pThreadPool, const std::s return fileName; } -std::string WatchBotRecorder::GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString) +std::string WatchBotRecorder::GetFolderName(const Settings& settings) { filesystem::path fileLocation(settings.Path); fileLocation = fileLocation / filesystem::path(settings.IpAddress); + return fileLocation.str(); +} + +std::string WatchBotRecorder::GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString) +{ + filesystem::path fileLocation(GetFolderName(settings)); + std::string fileName = std::string(dateTimeString).append(".").append(extension); filesystem::path fullFileName = fileLocation / filesystem::path(fileName); diff --git a/Recorder/WatchBotRecorder.h b/Recorder/WatchBotRecorder.h index 6d73813..9b709a2 100644 --- a/Recorder/WatchBotRecorder.h +++ b/Recorder/WatchBotRecorder.h @@ -26,6 +26,7 @@ public: virtual std::string Video(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg) override; private: + static std::string GetFolderName(const Settings& settings); static std::string GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString); static void Snapshots(Http::HttpClient* pHttpClient, const Settings& settings, std::string dateTimeString, int numberOfImages); static Image::JpegImage Snapshot(Http::HttpClient* pHttpClient, const Settings& settings); diff --git a/Util/Makefile b/Util/Makefile new file mode 120000 index 0000000..d0b0e8e --- /dev/null +++ b/Util/Makefile @@ -0,0 +1 @@ +../Makefile \ No newline at end of file diff --git a/Util/Util.cpp b/Util/Util.cpp new file mode 100644 index 0000000..aeab9af --- /dev/null +++ b/Util/Util.cpp @@ -0,0 +1,46 @@ +#include "Util.h" +#include +#include +#include +#include +#include + + +namespace CameraRecorder { +namespace Util { + +std::string GetDateTimeString(bool omitSeparator) +{ + std::array buffer; + buffer.fill(0); + std::time_t t = std::time(nullptr); + std::tm* tm = std::localtime(&t); + + if (omitSeparator) + strftime(buffer.data(), sizeof(buffer), "%Y%m%d%H%M%S", tm); + else + strftime(buffer.data(), sizeof(buffer), "%Y%m%d-%H%M%S", tm); + + return buffer.data(); +} + +bool FolderExists(const std::string& path) +{ + struct stat info; + if (stat(path.c_str(), &info) != 0) + return false; + else if (info.st_mode & S_IFDIR) + return true; + else + return false; +} + +void CreateFolder(const std::string& path) +{ + int status = mkdir(path.c_str(), 0777); + if ((status < 0) && (errno != EEXIST)) + throw std::runtime_error("Can't create folder."); +} + +} // namespace Util +} // namespace CameraRecorder diff --git a/Util/Util.h b/Util/Util.h new file mode 100644 index 0000000..f9e7aa0 --- /dev/null +++ b/Util/Util.h @@ -0,0 +1,18 @@ +#ifndef UTIL_UTIL_H +#define UTIL_UTIL_H + +#include + + +namespace CameraRecorder { +namespace Util { + +std::string GetDateTimeString(bool omitSeparator = false); + +bool FolderExists(const std::string& path); +void CreateFolder(const std::string& path); + +} // namespace Util +} // namespace CameraRecorder + +#endif // UTIL_UTIL_H