AttachmentHelper.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Helper;
  3. use FFMpeg;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Http\UploadedFile;
  6. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  7. use App\Services\Base\ErrorCode;
  8. use App\Models\BaseAttachmentModel;
  9. trait AttachmentHelper
  10. {
  11. /**
  12. * 上传附件
  13. *
  14. * @param Request $request laravel's http request
  15. * @param string|array $key 文件key
  16. * @param string $tag 文件tag
  17. * @param int $size 文件size限制,默认2M
  18. * @param array $mimeType 文件mime类型限制,默认不限
  19. * @return array|string|int 返回:md5字串|ErrorCode或[md5字串|ErrorCode]
  20. */
  21. public function uploadAttachment(Request $request, $key, $tag = 'files', $size = 10 * 1024 * 1024, array $mimeType = []) {
  22. if ($request->hasFile($key)) {
  23. $rel_path = '/upload/' . $tag . '/' . date('Ymd');
  24. $path = public_path() . $rel_path;
  25. if (!file_exists($path)) {
  26. if (!@mkdir($path, 0755, true)) {
  27. return ErrorCode::ATTACHMENT_MKDIR_FAILED;
  28. }
  29. }
  30. $files = $request->file($key);
  31. if ($files === null) {
  32. return ErrorCode::ATTACHMENT_UPLOAD_INVALID;
  33. }
  34. if ($files instanceof UploadedFile) {
  35. $files = [$files];
  36. }
  37. $result = [];
  38. foreach ($files as $idx => $file) {
  39. if (!$file->isValid()) {
  40. $result[$idx] = ErrorCode::ATTACHMENT_UPLOAD_INVALID;
  41. continue;
  42. }
  43. $fileSize = $file->getSize();
  44. if ($fileSize > $size) {
  45. $result[$idx] = ErrorCode::ATTACHMENT_SIZE_EXCEEDED;
  46. continue;
  47. }
  48. $fileMimeType = $file->getMimeType();
  49. if (!empty($mimeType) && !in_array($fileMimeType, $mimeType)) {
  50. $result[$idx] = ErrorCode::ATTACHMENT_MIME_NOT_ALLOWED;
  51. continue;
  52. }
  53. $clientName = $file->getClientOriginalName();
  54. $md5 = md5($clientName . time());
  55. $md5_filename = $md5 . '.' . $file->getClientOriginalExtension();
  56. try {
  57. $file->move($path, $md5_filename);
  58. $real_path = $path . '/' . $md5_filename;
  59. $url_path = $rel_path . '/' . $md5_filename;
  60. if ($fileMimeType == "video/mp4" || $fileMimeType == "video/quicktime") {
  61. // $ffmpeg = FFMpeg\FFMpeg::create(array(
  62. // 'ffmpeg.binaries' => '/usr/bin/ffmpeg',
  63. // 'ffprobe.binaries' => '/usr/bin/ffprobe'
  64. // ));
  65. // \Log::info($real_path);
  66. // $video = $ffmpeg->open($real_path);
  67. // if (!file_exists($vpath)) {
  68. // if (!@mkdir($vpath, 0755, true)) {
  69. // return ErrorCode::ATTACHMENT_MKDIR_FAILED;
  70. // }
  71. // }
  72. // $pic = $vpath.$md5.'.jpg';
  73. //正常缩略图
  74. $ffmpeg = FFMpeg\FFMpeg::create(array(
  75. 'ffmpeg.binaries' => '/usr/bin/ffmpeg',
  76. 'ffprobe.binaries' => '/usr/bin/ffprobe'
  77. ));
  78. $video = $ffmpeg->open($real_path);
  79. $vpath = $path;
  80. if (!file_exists($vpath)) {
  81. if (!@mkdir($vpath, 0755, true)) {
  82. return ErrorCode::ATTACHMENT_MKDIR_FAILED;
  83. }
  84. }
  85. $pic = $real_path.'.jpg';
  86. \Log::info($pic);
  87. $video
  88. ->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(1))
  89. ->save( $pic );
  90. }
  91. $attachment = new BaseAttachmentModel();
  92. $attachment->name = $clientName;
  93. $attachment->md5 = $md5;
  94. $attachment->path = $real_path;
  95. $attachment->url = $url_path;
  96. $attachment->size = $fileSize;
  97. $attachment->file_type = $fileMimeType;
  98. if ($attachment->save()) {
  99. $result[$idx] = $md5;
  100. } else {
  101. @unlink($real_path);
  102. $result[$idx] = ErrorCode::ATTACHMENT_SAVE_FAILED;
  103. }
  104. } catch (FileException $e) {
  105. $result[$idx] = ErrorCode::ATTACHMENT_MOVE_FAILED;
  106. }
  107. }
  108. if (count($result) == 1) {
  109. return array_shift($result);
  110. }
  111. return $result;
  112. } else {
  113. return ErrorCode::ATTACHMENT_UPLOAD_INVALID;
  114. }
  115. }
  116. /**
  117. * 删除附件
  118. *
  119. * @param $md5 string 删除文件的md5码
  120. * @return int 错误码or 0(成功)
  121. */
  122. public function deleteAttachment($md5) {
  123. $attachment = BaseAttachmentModel::where(['md5' => $md5])->first();
  124. if (!$attachment) {
  125. return ErrorCode::ATTACHMENT_NOT_EXIST;
  126. }
  127. if (file_exists($attachment->path)) {
  128. if (@unlink($attachment->path)) {
  129. if ($attachment->delete()) {
  130. return 0;
  131. } else {
  132. return ErrorCode::ATTACHMENT_RECORD_DELETE_FAILED;
  133. }
  134. } else {
  135. return ErrorCode::ATTACHMENT_DELETE_FAILED;
  136. }
  137. } else {
  138. return ErrorCode::ATTACHMENT_NOT_EXIST;
  139. }
  140. }
  141. }