No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MediaFile.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海口快推科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 小虎哥 <1105415366@qq.com>
  11. * Date: 2018-4-3
  12. */
  13. namespace app\user\model;
  14. use think\Db;
  15. use think\Model;
  16. /**
  17. * 视频文件
  18. */
  19. class MediaFile extends Model
  20. {
  21. //初始化
  22. protected function initialize()
  23. {
  24. // 需要调用`Model`的`initialize`方法
  25. parent::initialize();
  26. }
  27. /**
  28. * 删除单条视频文章的所有视频
  29. * @author 小虎哥 by 2018-4-3
  30. */
  31. public function delVideoFile($aid = array())
  32. {
  33. if (!is_array($aid)) {
  34. $aid = array($aid);
  35. }
  36. $file_url_list = Db::name('media_file')->where(['aid' => ['IN', $aid]])->column('file_url');
  37. $result = Db::name('media_file')->where(['aid' => ['IN', $aid]])->delete();
  38. if ($result !== false) {
  39. Db::name('media_log')->where(['aid' => ['IN', $aid]])->delete();
  40. foreach ($file_url_list as $key => $val) {
  41. $file_url_tmp = preg_replace('#^(/[/\w\-]+)?(/uploads/media/)#i', '.$2', $val);
  42. if (!is_http_url($val) && file_exists($file_url_tmp)) {
  43. @unlink($file_url_tmp);
  44. }
  45. }
  46. }
  47. \think\Cache::clear('media_file');
  48. return $result;
  49. }
  50. /**
  51. * 保存视频文章的视频
  52. * @author 小虎哥 by 2018-4-3
  53. */
  54. public function savefile($aid, $post = [], $opt = 'add')
  55. {
  56. $video_files = [];
  57. if (!empty($post['video'])) {
  58. $post['video'] = htmlspecialchars_decode($post['video']);
  59. $post['video'] = json_decode($post['video'], true);
  60. $addonFieldExt['total_video'] = count($post['video']);
  61. $addonFieldExt['total_duration'] = 0;
  62. foreach ($post['video'] as $k => $v) {
  63. $v['file_url'] = trim($v['file_url']);
  64. if (empty($v['file_url'])) {
  65. $addonFieldExt['total_video'] -= 1;
  66. continue;
  67. }
  68. $addonFieldExt['total_duration'] += $v['file_time'];
  69. $file_size = !empty($v['file_size']) ? $v['file_size'] : 0;
  70. $is_remote = 0;
  71. $file_ext = explode('.', $v['file_url']);
  72. $file_ext = preg_replace('/^(.*)\?(.*)$/i', '${1}', end($file_ext));
  73. $uhash = md5($v['file_url'] . $file_size);
  74. if (is_http_url($v['file_url'])) {
  75. $is_remote = 1;
  76. $md5file = '';
  77. } else {
  78. if (preg_match('#^(/[\w]+)?(/uploads/media/)#i', $v['file_url'])) {
  79. $file_path_tmp = preg_replace('#^(/[\w]+)?(/uploads/media/)#i', '$2', $v['file_url']);
  80. } else {
  81. $file_path_tmp = preg_replace('#^(' . $this->root_dir . ')?(/)#i', '$2', $v['file_url']);
  82. }
  83. $md5file = md5_file('.' . $file_path_tmp);
  84. }
  85. $arr = [
  86. 'aid' => $aid,
  87. 'title' => $post['title'],
  88. 'file_url' => !empty($v['file_url']) ? $v['file_url'] : '',
  89. 'file_time' => !empty($v['file_time']) ? $v['file_time'] : 0,
  90. 'file_title' => !empty($v['file_title']) ? $v['file_title'] : '',
  91. 'file_ext' => $file_ext,
  92. 'file_size' => $file_size,
  93. 'file_mime' => !empty($v['file_mime']) ? $v['file_mime'] : '',
  94. 'uhash' => $uhash,
  95. 'md5file' => $md5file,
  96. 'is_remote' => $is_remote,
  97. 'gratis' => !empty($v['gratis']) ? $v['gratis'] : 0,
  98. 'add_time' => getTime(),
  99. 'update_time' => getTime(),
  100. ];
  101. if (!empty($v['file_id'])){
  102. $arr['file_id'] = $v['file_id'];
  103. }
  104. $video_files[] = $arr;
  105. }
  106. }
  107. if (!empty($video_files)) {
  108. if ('add' == $opt) {
  109. Db::name('media_file')->insertAll($video_files);
  110. } else if ('edit' == $opt) {
  111. $file_ids = [];
  112. $insert = [];
  113. foreach ($video_files as $k => $v) {
  114. if (!empty($v['file_id'])) {
  115. $file_ids[] = $v['file_id'];
  116. } else {
  117. unset($v['file_id']);
  118. $insert[] = $v;
  119. unset($video_files[$k]);
  120. }
  121. }
  122. $file_url_list = Db::name('media_file')->where('aid', $aid)->column('file_url');
  123. Db::name('media_file')->where('aid', $aid)->where('file_id', 'not in', $file_ids)->delete();
  124. //更新
  125. $update = self::saveAll($video_files);
  126. //插入
  127. $insert = Db::name('media_file')->insertAll($insert);
  128. if (!empty($update) || !empty($insert)) {
  129. \think\Cache::clear('media_file');
  130. foreach ($video_files as $k => $v) {
  131. $index_key = array_search($v['file_url'], $file_url_list);
  132. if (false !== $index_key && 0 <= $index_key) {
  133. unset($file_url_list[$index_key]);
  134. }
  135. }
  136. try {
  137. foreach ($file_url_list as $key => $val) {
  138. $file_url_tmp = preg_replace('#^(/[/\w\-]+)?(/uploads/media/)#i', '.$2', $val);
  139. if (!is_http_url($val) && file_exists($file_url_tmp)) {
  140. @unlink($file_url_tmp);
  141. }
  142. }
  143. } catch (\Exception $e) {
  144. }
  145. }
  146. }
  147. $addonFieldExt['update_time'] = getTime();
  148. Db::name('media_content')->where('aid', $aid)->update($addonFieldExt);
  149. } else {
  150. if ('edit' == $opt) {
  151. Db::name('media_file')->where('aid', $aid)->delete();
  152. Db::name('media_content')->where('aid', $aid)->update(['total_duration' => 0, 'total_video' => 0, 'update_time' => getTime()]);
  153. }
  154. }
  155. }
  156. }