CheckFileController.class.php
3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* Created by PhpStorm.
* User: liyifei2012it
* Date: 17/5/22
* Time: 18:07
*/
namespace Frontend\Controller\Callback;
use Think\Log;
use VcySDK\Service;
use VcySDK\FileConvert;
use Common\Common\Constant;
use Common\Service\TaskService;
use Common\Service\FileService;
class CheckFileController extends AbstractController
{
/**
* 检查文件转码计划任务回调接口
* @author liyifei
*/
public function Index()
{
Log::record(sprintf('---%s %s CheckFile START---', QY_DOMAIN, APP_IDENTIFIER), Log::INFO);
$fileId = I('get.file_id', 0, 'intval');
if (empty($fileId)) {
$this->_exit();
}
// 未找到任务,退出
$taskServ = new TaskService();
$task = $taskServ->get_by_conds(['file_id' => $fileId]);
if (empty($task)) {
Log::record("not found task, file_id: {$fileId}", Log::INFO);
$this->_exit();
}
// 未找到文件,删除计划任务
$fileServ = new FileService();
$file = $fileServ->get($fileId);
if (empty($file)) {
Log::record("not found file_id: {$fileId}", Log::INFO);
$this->_delTask($fileId, $task['cron_id']);
}
// 查询转码结果
$at_convert_url = '';
$convert_status = Constant::FILE_STATUS_CONVERT;
$convertServ = new FileConvert(Service::instance());
$result = $convertServ->get($file['at_id']);
if (is_array($result) && isset($result['caConvertStatus'])) {
// 转码成功
if (!empty($result['caAttachment']) && $result['caConvertStatus'] == FileConvert::CONVERT_STATUS_SUCCESS) {
$convert_status = Constant::FILE_STATUS_NORMAL;
$at_convert_url = $result['caAttachment'];
}
// 转码失败(状态不是:待处理、转码中、成功,即为失败)
$normalStatus = [
FileConvert::CONVERT_STATUS_WATTING,
FileConvert::CONVERT_STATUS_ING,
FileConvert::CONVERT_STATUS_SUCCESS,
];
if (!in_array($result['caConvertStatus'], $normalStatus)) {
$convert_status = Constant::FILE_STATUS_FAIL;
}
}
// 更新转码状态,结束计划任务
if ($convert_status != Constant::FILE_STATUS_CONVERT) {
$fileServ->update($fileId, [
'file_status' => $convert_status,
'at_convert_url' => $at_convert_url,
]);
Log::record("convert end, file_id: {$fileId}, at_convert_url: {$at_convert_url}", Log::INFO);
// 结束计划任务
$this->_delTask($fileId, $task['cron_id']);
}
$this->_exit();
}
/**
* 退出
* @author zhonglei
* @return void
*/
private function _exit()
{
// 日志结束
Log::record(sprintf('---%s %s CheckFile END---', QY_DOMAIN, APP_IDENTIFIER), Log::INFO);
exit('SUCCESS');
}
/**
* 删除计划任务并退出
* @author liyifei
* @param int $file_id 文件ID
* @param string $cron_id UC计划任务ID
* @return void
*/
private function _delTask($file_id, $cron_id)
{
$taskServ = new TaskService();
$taskServ->delTask($file_id, $cron_id);
Log::record("delete task: {$cron_id}", Log::INFO);
$this->_exit();
}
}