我有一个使用Symfony 5制作的应用程序,我有一个脚本,可以将服务器上的视频上传到登录的用户频道。
这基本上是我的控制器的代码:
/**
* Upload a video to YouTube.
*
* @Route("/upload_youtube/{id}", name="api_admin_video_upload_youtube", methods={"POST"}, requirements={"id" = "\d+"})
*/
public function upload_youtube(int $id, Request $request, VideoRepository $repository, \Google_Client $googleClient): JsonResponse
{
$video = $repository->find($id);
if (!$video) {
return $this->json([], Response::HTTP_NOT_FOUND);
}
$data = json_decode(
$request->getContent(),
true
);
$googleClient->setRedirectUri($_SERVER['CLIENT_URL'] . '/admin/videos/youtube');
$googleClient->fetchAccessTokenWithAuthCode($data['code']);
$videoPath = $this->getParameter('videos_directory') . '/' . $video->getFilename();
$service = new \Google_Service_YouTube($googleClient);
$ytVideo = new \Google_Service_YouTube_Video();
$ytVideoSnippet = new \Google_Service_YouTube_VideoSnippet();
$ytVideoSnippet->setTitle($video->getTitle());
$ytVideo->setSnippet($ytVideoSnippet);
$ytVideoStatus = new \Google_Service_YouTube_VideoStatus();
$ytVideoStatus->setPrivacyStatus('private');
$ytVideo->setStatus($ytVideoStatus);
$chunkSizeBytes = 1 * 1024 * 1024;
$googleClient->setDefer(true);
$insertRequest = $service->videos->insert(
'snippet,status',
$ytVideo
);
这基本上是有效的,但问题是视频可能非常大(10G +),所以它需要很长时间,基本上Nginx在结束之前终止,并在上传完成之前返回“504网关超时”。
无论如何,我不希望用户在上传页面时必须等待页面加载。
因此,我正在寻找一种方法,而不仅仅是立即运行该脚本,而是在某种后台线程中或以异步方式执行该脚本。
控制器向用户返回a,我可以告诉他正在上传,稍后再回来检查进度。200
如何做到这一点?
慕侠2389804