AttachmentHelper.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. \Log::info("fileMimeType:".$fileMimeType);
  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. $attachment = new BaseAttachmentModel();
  61. $attachment->name = $clientName;
  62. $attachment->md5 = $md5;
  63. $attachment->path = $real_path;
  64. $attachment->url = $url_path;
  65. $attachment->size = $fileSize;
  66. $attachment->file_type = $fileMimeType;
  67. if ($attachment->save()) {
  68. $result[$idx] = $md5;
  69. } else {
  70. @unlink($real_path);
  71. $result[$idx] = ErrorCode::ATTACHMENT_SAVE_FAILED;
  72. }
  73. } catch (FileException $e) {
  74. $result[$idx] = ErrorCode::ATTACHMENT_MOVE_FAILED;
  75. }
  76. }
  77. if (count($result) == 1) {
  78. return array_shift($result);
  79. }
  80. return $result;
  81. } else {
  82. return ErrorCode::ATTACHMENT_UPLOAD_INVALID;
  83. }
  84. }
  85. /**
  86. * 删除附件
  87. *
  88. * @param $md5 string 删除文件的md5码
  89. * @return int 错误码or 0(成功)
  90. */
  91. public function deleteAttachment($md5) {
  92. $attachment = Attachment::where(['md5' => $md5])->first();
  93. if (!$attachment) {
  94. return ErrorCode::ATTACHMENT_NOT_EXIST;
  95. }
  96. if (file_exists($attachment->path)) {
  97. if (@unlink($attachment->path)) {
  98. if ($attachment->delete()) {
  99. return 0;
  100. } else {
  101. return ErrorCode::ATTACHMENT_RECORD_DELETE_FAILED;
  102. }
  103. } else {
  104. return ErrorCode::ATTACHMENT_DELETE_FAILED;
  105. }
  106. } else {
  107. return ErrorCode::ATTACHMENT_NOT_EXIST;
  108. }
  109. }
  110. }