AttachmentHelper.php 5.2 KB

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