Improve:Files Manager module.

This commit is contained in:
Fancy code 2024-04-29 11:08:06 +08:00
parent d71ea78114
commit 9802e42e67
8 changed files with 150 additions and 4 deletions

View File

@ -17,6 +17,7 @@ include_directories(
#)
aux_source_directory(./src SRC_FILES)
aux_source_directory(./src/sqlite3 SRC_FILES)
set(TARGET_NAME FilesManager)
add_library(${TARGET_NAME} STATIC ${SRC_FILES})

View File

@ -0,0 +1,27 @@
/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FILES_DATABASE_H
#define FILES_DATABASE_H
#include "StatusCode.h"
#include "FilesHandle.h"
#include "IFilesManager.h"
class FilesDatabase : public FilesHandle
{
public:
FilesDatabase() = default;
virtual ~FilesDatabase() = default;
StatusCode DatabaseSaveFile(const SaveFileInfo &fileInfo);
};
#endif

View File

@ -0,0 +1,59 @@
/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "FilesHandle.h"
#include "ILog.h"
#include <algorithm>
#include <cctype>
#include <chrono>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <sys/stat.h>
#include <sys/types.h>
std::string FilesHandle::CreateFilePathName(const std::string &sourceFile)
{
const std::string ERROR_SUFFIX = "";
const char *dot = strrchr(sourceFile.c_str(), '.');
if (!dot || dot == sourceFile.c_str()) {
LogError("File suffix error.\n");
return ERROR_SUFFIX;
}
std::string fileSuffix = dot + 1;
if (fileSuffix != "jpg" && fileSuffix != "mp4" && fileSuffix != "JPG" && fileSuffix != "MP4") {
LogError("File suffix error.\n");
return ERROR_SUFFIX;
}
std::string fileType = (fileSuffix == "jpg" || fileSuffix == "JPG") ? "picture" : "video";
auto now = std::chrono::system_clock::now();
time_t t_now = std::chrono::system_clock::to_time_t(now);
struct tm tm_now = *std::localtime(&t_now);
int year = tm_now.tm_year + 1900;
int month = tm_now.tm_mon + 1;
int day = tm_now.tm_mday;
int hour = tm_now.tm_hour;
int minute = tm_now.tm_min;
int second = tm_now.tm_sec;
std::ostringstream pathStream;
pathStream << "/DCIM/" << fileType << "/" << std::setw(4) << std::setfill('0') << year << "/" << std::setw(2)
<< std::setfill('0') << month << "/" << std::setw(2) << std::setfill('0') << day << "/" << std::setw(2)
<< std::setfill('0') << hour << std::setw(2) << std::setfill('0') << minute << std::setw(2)
<< std::setfill('0') << second << "." << fileSuffix;
return pathStream.str();
}

View File

@ -0,0 +1,25 @@
/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FILES_HANDLE_H
#define FILES_HANDLE_H
#include <iostream>
class FilesHandle
{
public:
FilesHandle() = default;
virtual ~FilesHandle() = default;
std::string CreateFilePathName(const std::string &sourceFile);
};
#endif

View File

@ -24,6 +24,5 @@ StatusCode FilesManagerImpl::UnInit(void)
}
StatusCode FilesManagerImpl::SaveFile(const SaveFileInfo &fileInfo)
{
IStorageManager::GetInstance()->SaveFile(fileInfo.mFileName, "/DCIM/picture/2024/04/28/test.jpg");
return CreateStatusCode(STATUS_CODE_OK);
return FilesDatabase::DatabaseSaveFile(fileInfo);
}

View File

@ -14,13 +14,16 @@
*/
#ifndef FILES_MANAGER_IMPL_H
#define FILES_MANAGER_IMPL_H
#include "FilesDatabase.h"
#include "IFilesManager.h"
#include <map>
class FilesManagerImpl : public IFilesManager
class FilesManagerImpl : public IFilesManager, public FilesDatabase
{
public:
FilesManagerImpl() = default;
virtual ~FilesManagerImpl() = default;
protected:
StatusCode Init(void) override;
StatusCode UnInit(void) override;
StatusCode SaveFile(const SaveFileInfo &fileInfo) override;

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "FilesDatabase.h"
#include "ILog.h"
#include "IStorageManager.h"
StatusCode FilesDatabase::DatabaseSaveFile(const SaveFileInfo &fileInfo)
{
std::string saveFile = FilesHandle::CreateFilePathName(fileInfo.mFileName);
LogInfo("Save file:%s\n", saveFile.c_str());
if (0 == saveFile.length()) {
LogError("File suffix error, save file failed.\n");
return CreateStatusCode(STATUS_CODE_NOT_OK);
}
StatusCode code = IStorageManager::GetInstance()->SaveFile(fileInfo.mFileName, saveFile);
if (IsCodeOK(code)) {
// TODO: handle database here
return CreateStatusCode(STATUS_CODE_OK);
}
return CreateStatusCode(STATUS_CODE_NOT_OK);
}

View File

@ -34,7 +34,7 @@ TEST_F(HunttingCameraTest, INTEGRATION_HunttingCamera_EXAMPLE_MediaReprot)
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockReportCameraEvent("test name", "test path", CameraEvent::PICTIRUE, CameraType::MAIN_CAMERA);
MockReportCameraEvent("/tmp/test.MP4", "test path", CameraEvent::PICTIRUE, CameraType::MAIN_CAMERA);
// MockAppPlayback();
MainThread::GetInstance()->Runing();
}