| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\jobs;
- use yii\base\BaseObject;
- use yii\queue\JobInterface;
- use app\models\VideoGoodsList;
- use app\utils\CurlHelper;
- class DownloadVideoJob extends BaseObject implements JobInterface
- {
- public $id;
- public $baseUrl;
- public function execute($queue)
- {
- static::download($this->id, $this->baseUrl);
- }
- public static function download($id, $baseUrl)
- {
- $item = VideoGoodsList::findOne($id);
- if (!$item) {
- return false;
- }
- if (empty($item->video_url)) {
- return false;
- }
- $saveFile = \Yii::$app->basePath . '/runtime/' . md5($item->video_url) . '.mp4';
- try {
- CurlHelper::download($item->video_url, $saveFile);
- // 使用http_post函数上传视频到服务器
- $url = $baseUrl . '/index.php?r=client/v1/upload/upload-video';
- $res = http_post($url, [
- 'multipart' => [
- [
- 'name' => 'video',
- 'contents' => fopen($saveFile, 'r'),
- 'filename' => basename($saveFile),
- ]
- ],
- ]);
- if ($res->getStatusCode() != 200) {
- return false;
- }
- $data = json_decode((string)$res->getBody(), true);
- if ($data && $data['code'] == 0) {
- $item->video_url = $data['url'];
- $item->save();
- }
- if (\file_exists($saveFile)) {
- \unlink($saveFile);
- }
- } catch (\Throwable $e) {
- \debug_log($e->getMessage(), 'video.log');
- if (\file_exists($saveFile)) {
- \unlink($saveFile);
- }
- }
- }
- }
|