AttachmentHelper.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. $pic = public_path() . '/upload/vpic/'.$md5.'.jpg';
  68. \Log::info($pic);
  69. $video
  70. ->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(1))
  71. ->save( $pic );
  72. }
  73. $attachment = new BaseAttachmentModel();
  74. $attachment->name = $clientName;
  75. $attachment->md5 = $md5;
  76. $attachment->path = $real_path;
  77. $attachment->url = $url_path;
  78. $attachment->size = $fileSize;
  79. $attachment->file_type = $fileMimeType;
  80. if ($attachment->save()) {
  81. $result[$idx] = $md5;
  82. } else {
  83. @unlink($real_path);
  84. $result[$idx] = ErrorCode::ATTACHMENT_SAVE_FAILED;
  85. }
  86. } catch (FileException $e) {
  87. $result[$idx] = ErrorCode::ATTACHMENT_MOVE_FAILED;
  88. }
  89. }
  90. if (count($result) == 1) {
  91. return array_shift($result);
  92. }
  93. return $result;
  94. } else {
  95. return ErrorCode::ATTACHMENT_UPLOAD_INVALID;
  96. }
  97. }
  98. /**
  99. * 删除附件
  100. *
  101. * @param $md5 string 删除文件的md5码
  102. * @return int 错误码or 0(成功)
  103. */
  104. public function deleteAttachment($md5) {
  105. $attachment = BaseAttachmentModel::where(['md5' => $md5])->first();
  106. if (!$attachment) {
  107. return ErrorCode::ATTACHMENT_NOT_EXIST;
  108. }
  109. if (file_exists($attachment->path)) {
  110. if (@unlink($attachment->path)) {
  111. if ($attachment->delete()) {
  112. return 0;
  113. } else {
  114. return ErrorCode::ATTACHMENT_RECORD_DELETE_FAILED;
  115. }
  116. } else {
  117. return ErrorCode::ATTACHMENT_DELETE_FAILED;
  118. }
  119. } else {
  120. return ErrorCode::ATTACHMENT_NOT_EXIST;
  121. }
  122. }
  123. }