优化截图生成逻辑

This commit is contained in:
xiongziliang 2020-05-09 09:29:45 +08:00
parent 76bece0217
commit 5b4d4dab96

View File

@ -827,53 +827,52 @@ void installWebApi() {
api_regist2("/index/api/getSnap", [](API_ARGS2){ api_regist2("/index/api/getSnap", [](API_ARGS2){
CHECK_SECRET(); CHECK_SECRET();
CHECK_ARGS("url", "timeout_sec", "expire_sec"); CHECK_ARGS("url", "timeout_sec", "expire_sec");
auto file_prefix = MD5(allArgs["url"]).hexdigest() + "_";
string file_path;
int expire_sec = allArgs["expire_sec"]; int expire_sec = allArgs["expire_sec"];
File::scanDir(File::absolutePath(snap_root,""),[&](const string &path, bool isDir){ auto scan_path = File::absolutePath(MD5(allArgs["url"]).hexdigest(), snap_root) + "/";
if(!isDir){ string snap_path;
auto pos = path.find(file_prefix); File::scanDir(scan_path, [&](const string &path, bool isDir) {
if(pos != string::npos){ if (isDir) {
//忽略文件夹
return true;
}
//找到截图 //找到截图
auto tm = FindField(path.data() + pos + file_prefix.size(), nullptr, ".jpeg"); auto tm = FindField(path.data() + scan_path.size(), nullptr, ".jpeg");
if (atoll(tm.data()) + expire_sec < time(NULL)) { if (atoll(tm.data()) + expire_sec < time(NULL)) {
//截图已经过期,删除之,后面重新生成 //截图已经过期,删除之,后面重新生成
File::delete_file(path.data()); File::delete_file(path.data());
}else{
//截图未过期
file_path = path;
}
return false;
}
}
return true; return true;
}
//截图未过期,中断遍历,返回上次生成的截图
snap_path = path;
return false;
}); });
if(!file_path.empty()){ if(!snap_path.empty()){
//返回上次生成的截图
StrCaseMap headerOut; StrCaseMap headerOut;
headerOut["Content-Type"] = HttpFileManager::getContentType(".jpeg"); headerOut["Content-Type"] = HttpFileManager::getContentType(".jpeg");
invoker.responseFile(headerIn,headerOut,file_path); invoker.responseFile(headerIn,headerOut,snap_path);
return ; return ;
} }
//无截图或者截图已经过期 //无截图或者截图已经过期
file_path = File::absolutePath(StrPrinter << file_prefix << time(NULL) << ".jpeg" ,snap_root); snap_path = StrPrinter << scan_path << time(NULL) << ".jpeg";
#if !defined(_WIN32) #if !defined(_WIN32)
//创建文件夹 //创建文件夹
File::create_path(file_path.c_str(), S_IRWXO | S_IRWXG | S_IRWXU); File::create_path(snap_path.c_str(), S_IRWXO | S_IRWXG | S_IRWXU);
#else #else
File::create_path(file_path.c_str(),0); File::create_path(snap_path.c_str(),0);
#endif #endif
FFmpegSnap::makeSnap(allArgs["url"],file_path,allArgs["timeout_sec"],[invoker,headerIn,file_path](bool success){ FFmpegSnap::makeSnap(allArgs["url"],snap_path,allArgs["timeout_sec"],[invoker,headerIn,snap_path](bool success){
if(!success){ if(!success){
//生成截图失败,可能残留空文件 //生成截图失败,可能残留空文件
File::delete_file(file_path.data()); File::delete_file(snap_path.data());
} }
StrCaseMap headerOut; StrCaseMap headerOut;
headerOut["Content-Type"] = HttpFileManager::getContentType(".jpeg"); headerOut["Content-Type"] = HttpFileManager::getContentType(".jpeg");
invoker.responseFile(headerIn, headerOut, file_path); invoker.responseFile(headerIn, headerOut, snap_path);
}); });
}); });