| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- namespace App\Repositories\Utils;
- use App\Repositories\ConfigRepoistory;
- use Hyperf\Guzzle\ClientFactory;
- use Psr\SimpleCache\CacheInterface;
- class MediaCenterRepository
- {
- /**
- * 缓存仓库
- * @var null|CacheInterface
- */
- private $cacheRepo = null;
- /**
- * http客户端仓库
- * @var null|\GuzzleHttp\Client
- */
- private $httpClient = null;
- private $configRepo = null;
- private $config = [];
- /**
- * 获取hash资源信息.
- * @param string $fileHash
- * @param false $isForce
- * @return mixed
- * @throws \GuzzleHttp\Exception\GuzzleException
- * @throws \Psr\SimpleCache\InvalidArgumentException
- */
- public function getHashInfo(string $fileHash, $isForce = false)
- {
- if (empty($fileHash)) {
- return [];
- }
- $cacheKey = 'video:media_center:hash:'. $fileHash;
- $cacheTime = 7 * 24 * 60 * 60;
- $data = $this->getCacheRepo()->get($cacheKey, []);
- if (empty($data) || $isForce === true) {
- $arr = $this->isHashExistsWithoutCache($fileHash);
- if (isset($arr['data']['is_exists']) && $arr['data']['is_exists'] === true) {
- $data = $arr['data'];
- $this->getCacheRepo()->set($cacheKey, $data, $cacheTime);
- }
- }
- return $data;
- }
- /**
- * 是否存在相同hash, 存在返回数据..
- * @param string $fileHash
- * @return array
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function isHashExistsWithoutCache(string $fileHash)
- {
- $result = [];
- $config = $this->getConfig();
- $uri = '/api/ppvod/is/hash/exists';
- $postData = [
- 'file_hash' => $fileHash,
- ];
- $options = [
- 'query' => [
- 'token' => $config['token'],
- ],
- 'json' => $postData,
- ];
- $ret = $this->getHttpClient()->post($uri, $options);
- $httpStatus = $ret->getStatusCode();
- $bodyRaw = $ret->getBody()->getContents();
- if ($httpStatus == 200) {
- $arrResp = json_decode($bodyRaw, true);
- $result['code'] = $arrResp['code'];
- $result['message'] = $arrResp['message'];
- $result['data'] = $arrResp['data'];
- } else {
- $result['code'] = 500;
- $result['message'] = 'http_status:'. strval($httpStatus);
- $result['data'] = [];
- logger('MediaCenterRepository-isHashExistsWithoutCache', ['http_status' => $httpStatus, 'body_raw' => $bodyRaw]);
- }
- return $result;
- }
- /**
- * 同步视频到媒资库.
- * @param $postData
- * @return array
- * @throws \GuzzleHttp\Exception\GuzzleException
- * @throws \Psr\SimpleCache\InvalidArgumentException
- */
- public function syncAppVideoToMediaCenter($postData)
- {
- $result = [];
- $config = $this->getConfig();
- $uri = '/api/app/sync/video';
- $options = [
- 'query' => [
- 'token' => $config['token'],
- ],
- 'json' => $postData,
- ];
- $ret = $this->getHttpClient()->post($uri, $options);
- $httpStatus = $ret->getStatusCode();
- $bodyRaw = $ret->getBody()->getContents();
- if ($httpStatus == 200) {
- $arrResp = json_decode($bodyRaw, true);
- $result['code'] = $arrResp['code'];
- $result['message'] = $arrResp['message'];
- $result['data'] = $arrResp['data'];
- } else {
- $result['code'] = 500;
- $result['message'] = 'http_status:'. strval($httpStatus);
- $result['data'] = [];
- logger('MediaCenterRepository-syncVideo', ['http_status' => $httpStatus, 'body_raw' => $bodyRaw]);
- }
- return $result;
- }
-
- /**
- * 获取配置.
- * @return array
- * @throws \Psr\SimpleCache\InvalidArgumentException
- */
- public function getConfig()
- {
- if (empty($this->config)) {
- $baseUrl = $this->getConfigRepo()->get('media_center_base_url', 'sys', '');
- $token = $this->getConfigRepo()->get('media_center_token', 'sys', '');
- $baseUrl = rtrim($baseUrl, '/'); // 干掉最后一个斜杆
- $this->config = [
- 'base_url' => $baseUrl,
- 'token' => $token,
- ];
- }
- return $this->config;
- }
- /**
- * @return \GuzzleHttp\Client|ClientFactory|null
- */
- private function getHttpClient()
- {
- if (!$this->httpClient instanceof ClientFactory) {
- $config = $this->getConfig();
- $baseUrl = rtrim($config['base_url'], '/');
- $token = $config['token'];
- $options = array(
- 'base_uri' => $baseUrl,
- 'timeout' => 3, // 超时,单位秒.
- 'verify' => false, // 是否校验SSL证书.
- 'http_errors' => false, // 如果跟随http异常头部是否中断.
- 'debug' => false, // 是否启用调试模式.
- 'allow_redirects' => false, // 不跟随301/302跳转.
- 'headers' => [
- 'X-TOKEN' => $token,
- ],
- );
- $this->httpClient = make(ClientFactory::class)->create($options);
- }
- return $this->httpClient;
- }
- /**
- * @return ConfigRepoistory|mixed
- */
- private function getConfigRepo()
- {
- if (!$this->configRepo instanceof ConfigRepoistory) {
- $this->configRepo = make(ConfigRepoistory::class);
- }
- return $this->configRepo;
- }
- /**
- * @return mixed|CacheInterface|null
- */
- private function getCacheRepo()
- {
- if (!$this->cacheRepo instanceof CacheInterface) {
- $this->cacheRepo = make(CacheInterface::class);
- }
- return $this->cacheRepo;
- }
- }
|