AttachmentHelper.php 4.8 KB

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