MediaCenterRepository.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace App\Repositories\Utils;
  3. use App\Repositories\ConfigRepoistory;
  4. use Hyperf\Guzzle\ClientFactory;
  5. use Psr\SimpleCache\CacheInterface;
  6. class MediaCenterRepository
  7. {
  8. /**
  9. * 缓存仓库
  10. * @var null|CacheInterface
  11. */
  12. private $cacheRepo = null;
  13. /**
  14. * http客户端仓库
  15. * @var null|\GuzzleHttp\Client
  16. */
  17. private $httpClient = null;
  18. private $configRepo = null;
  19. private $config = [];
  20. /**
  21. * 获取hash资源信息.
  22. * @param string $fileHash
  23. * @param false $isForce
  24. * @return mixed
  25. * @throws \GuzzleHttp\Exception\GuzzleException
  26. * @throws \Psr\SimpleCache\InvalidArgumentException
  27. */
  28. public function getHashInfo(string $fileHash, $isForce = false)
  29. {
  30. if (empty($fileHash)) {
  31. return [];
  32. }
  33. $cacheKey = 'video:media_center:hash:'. $fileHash;
  34. $cacheTime = 7 * 24 * 60 * 60;
  35. $data = $this->getCacheRepo()->get($cacheKey, []);
  36. if (empty($data) || $isForce === true) {
  37. $arr = $this->isHashExistsWithoutCache($fileHash);
  38. if (isset($arr['data']['is_exists']) && $arr['data']['is_exists'] === true) {
  39. $data = $arr['data'];
  40. $this->getCacheRepo()->set($cacheKey, $data, $cacheTime);
  41. }
  42. }
  43. return $data;
  44. }
  45. /**
  46. * 是否存在相同hash, 存在返回数据..
  47. * @param string $fileHash
  48. * @return array
  49. * @throws \GuzzleHttp\Exception\GuzzleException
  50. */
  51. public function isHashExistsWithoutCache(string $fileHash)
  52. {
  53. $result = [];
  54. $config = $this->getConfig();
  55. $uri = '/api/ppvod/is/hash/exists';
  56. $postData = [
  57. 'file_hash' => $fileHash,
  58. ];
  59. $options = [
  60. 'query' => [
  61. 'token' => $config['token'],
  62. ],
  63. 'json' => $postData,
  64. ];
  65. $ret = $this->getHttpClient()->post($uri, $options);
  66. $httpStatus = $ret->getStatusCode();
  67. $bodyRaw = $ret->getBody()->getContents();
  68. if ($httpStatus == 200) {
  69. $arrResp = json_decode($bodyRaw, true);
  70. $result['code'] = $arrResp['code'];
  71. $result['message'] = $arrResp['message'];
  72. $result['data'] = $arrResp['data'];
  73. } else {
  74. $result['code'] = 500;
  75. $result['message'] = 'http_status:'. strval($httpStatus);
  76. $result['data'] = [];
  77. logger('MediaCenterRepository-isHashExistsWithoutCache', ['http_status' => $httpStatus, 'body_raw' => $bodyRaw]);
  78. }
  79. return $result;
  80. }
  81. /**
  82. * 同步视频到媒资库.
  83. * @param $postData
  84. * @return array
  85. * @throws \GuzzleHttp\Exception\GuzzleException
  86. * @throws \Psr\SimpleCache\InvalidArgumentException
  87. */
  88. public function syncAppVideoToMediaCenter($postData)
  89. {
  90. $result = [];
  91. $config = $this->getConfig();
  92. $uri = '/api/app/sync/video';
  93. $options = [
  94. 'query' => [
  95. 'token' => $config['token'],
  96. ],
  97. 'json' => $postData,
  98. ];
  99. $ret = $this->getHttpClient()->post($uri, $options);
  100. $httpStatus = $ret->getStatusCode();
  101. $bodyRaw = $ret->getBody()->getContents();
  102. if ($httpStatus == 200) {
  103. $arrResp = json_decode($bodyRaw, true);
  104. $result['code'] = $arrResp['code'];
  105. $result['message'] = $arrResp['message'];
  106. $result['data'] = $arrResp['data'];
  107. } else {
  108. $result['code'] = 500;
  109. $result['message'] = 'http_status:'. strval($httpStatus);
  110. $result['data'] = [];
  111. logger('MediaCenterRepository-syncVideo', ['http_status' => $httpStatus, 'body_raw' => $bodyRaw]);
  112. }
  113. return $result;
  114. }
  115. /**
  116. * 获取配置.
  117. * @return array
  118. * @throws \Psr\SimpleCache\InvalidArgumentException
  119. */
  120. public function getConfig()
  121. {
  122. if (empty($this->config)) {
  123. $baseUrl = $this->getConfigRepo()->get('media_center_base_url', 'sys', '');
  124. $token = $this->getConfigRepo()->get('media_center_token', 'sys', '');
  125. $baseUrl = rtrim($baseUrl, '/'); // 干掉最后一个斜杆
  126. $this->config = [
  127. 'base_url' => $baseUrl,
  128. 'token' => $token,
  129. ];
  130. }
  131. return $this->config;
  132. }
  133. /**
  134. * @return \GuzzleHttp\Client|ClientFactory|null
  135. */
  136. private function getHttpClient()
  137. {
  138. if (!$this->httpClient instanceof ClientFactory) {
  139. $config = $this->getConfig();
  140. $baseUrl = rtrim($config['base_url'], '/');
  141. $token = $config['token'];
  142. $options = array(
  143. 'base_uri' => $baseUrl,
  144. 'timeout' => 3, // 超时,单位秒.
  145. 'verify' => false, // 是否校验SSL证书.
  146. 'http_errors' => false, // 如果跟随http异常头部是否中断.
  147. 'debug' => false, // 是否启用调试模式.
  148. 'allow_redirects' => false, // 不跟随301/302跳转.
  149. 'headers' => [
  150. 'X-TOKEN' => $token,
  151. ],
  152. );
  153. $this->httpClient = make(ClientFactory::class)->create($options);
  154. }
  155. return $this->httpClient;
  156. }
  157. /**
  158. * @return ConfigRepoistory|mixed
  159. */
  160. private function getConfigRepo()
  161. {
  162. if (!$this->configRepo instanceof ConfigRepoistory) {
  163. $this->configRepo = make(ConfigRepoistory::class);
  164. }
  165. return $this->configRepo;
  166. }
  167. /**
  168. * @return mixed|CacheInterface|null
  169. */
  170. private function getCacheRepo()
  171. {
  172. if (!$this->cacheRepo instanceof CacheInterface) {
  173. $this->cacheRepo = make(CacheInterface::class);
  174. }
  175. return $this->cacheRepo;
  176. }
  177. }