mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-26 12:37:09 +08:00
解决截图失败的问题
This commit is contained in:
parent
4f42f508d5
commit
2cf66594e8
@ -940,50 +940,60 @@ void installWebApi() {
|
|||||||
CHECK_ARGS("url", "timeout_sec", "expire_sec");
|
CHECK_ARGS("url", "timeout_sec", "expire_sec");
|
||||||
GET_CONFIG(string, snap_root, API::kSnapRoot);
|
GET_CONFIG(string, snap_root, API::kSnapRoot);
|
||||||
|
|
||||||
|
bool have_old_snap = false, res_old_snap = false;
|
||||||
int expire_sec = allArgs["expire_sec"];
|
int expire_sec = allArgs["expire_sec"];
|
||||||
auto scan_path = File::absolutePath(MD5(allArgs["url"]).hexdigest(), snap_root) + "/";
|
auto scan_path = File::absolutePath(MD5(allArgs["url"]).hexdigest(), snap_root) + "/";
|
||||||
string snap_path;
|
string new_snap = StrPrinter << scan_path << time(NULL) << ".jpeg";
|
||||||
|
|
||||||
File::scanDir(scan_path, [&](const string &path, bool isDir) {
|
File::scanDir(scan_path, [&](const string &path, bool isDir) {
|
||||||
if (isDir) {
|
if (isDir || !end_with(path, ".jpeg")) {
|
||||||
//忽略文件夹
|
//忽略文件夹或其他类型的文件
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//找到截图
|
//找到截图
|
||||||
auto tm = FindField(path.data() + scan_path.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());
|
rename(path.data(), new_snap.data());
|
||||||
|
have_old_snap = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//截图未过期,中断遍历,返回上次生成的截图
|
//截图存在,且未过期,那么返回之
|
||||||
snap_path = path;
|
res_old_snap = true;
|
||||||
|
responseSnap(path, headerIn, invoker);
|
||||||
|
//中断遍历
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
|
||||||
if(!snap_path.empty()){
|
if (res_old_snap) {
|
||||||
responseSnap(snap_path, headerIn, invoker);
|
//已经回复了旧的截图
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//无截图或者截图已经过期
|
//无截图或者截图已经过期
|
||||||
snap_path = StrPrinter << scan_path << time(NULL) << ".jpeg";
|
if (!have_old_snap) {
|
||||||
|
//无过期截图,生成一个空文件,目的是顺便创建文件夹路径
|
||||||
//生成一个空文件,目的是顺便创建文件夹路径,
|
//同时防止在FFmpeg生成截图途中不停的尝试调用该api多次启动FFmpeg进程
|
||||||
//同时防止在FFmpeg生成截图途中不停的尝试调用该api启动FFmpeg生成相同的截图
|
auto file = File::create_file(new_snap.data(), "wb");
|
||||||
auto file = File::create_file(snap_path.data(), "wb");
|
|
||||||
if (file) {
|
if (file) {
|
||||||
fclose(file);
|
fclose(file);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//启动FFmpeg进程,开始截图
|
//启动FFmpeg进程,开始截图,生成临时文件,截图成功后替换为正式文件
|
||||||
FFmpegSnap::makeSnap(allArgs["url"],snap_path,allArgs["timeout_sec"],[invoker,headerIn,snap_path](bool success){
|
auto new_snap_tmp = new_snap + ".tmp";
|
||||||
|
FFmpegSnap::makeSnap(allArgs["url"], new_snap_tmp, allArgs["timeout_sec"], [invoker, headerIn, new_snap, new_snap_tmp](bool success) {
|
||||||
if (!success) {
|
if (!success) {
|
||||||
//生成截图失败,可能残留空文件
|
//生成截图失败,可能残留空文件
|
||||||
File::delete_file(snap_path.data());
|
File::delete_file(new_snap_tmp.data());
|
||||||
|
} else {
|
||||||
|
//临时文件改成正式文件
|
||||||
|
File::delete_file(new_snap.data());
|
||||||
|
rename(new_snap_tmp.data(), new_snap.data());
|
||||||
}
|
}
|
||||||
responseSnap(snap_path, headerIn, invoker);
|
responseSnap(new_snap, headerIn, invoker);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user