hunting/middleware/FilesManager/src/FilesHandle.cpp
2024-06-17 16:10:32 +08:00

56 lines
2.2 KiB
C++

/*
* 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 <cctype>
#include <chrono>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
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();
}