AttachmentHelper.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 = 2 * 1024 * 1024, array $mimeType = []) {
  21. \Log::info("key:".$request->hasFile($key));
  22. if ($request->hasFile($key)) {
  23. \Log::info("key:".$key);
  24. $rel_path = '/upload/' . $tag . '/' . date('Ymd');
  25. $path = public_path() . $rel_path;
  26. if (!file_exists($path)) {
  27. if (!@mkdir($path, 0755, true)) {
  28. return ErrorCode::ATTACHMENT_MKDIR_FAILED;
  29. }
  30. }
  31. $files = $request->file($key);
  32. if ($files === null) {
  33. return ErrorCode::ATTACHMENT_UPLOAD_INVALID;
  34. }
  35. if ($files instanceof UploadedFile) {
  36. $files = [$files];
  37. }
  38. $result = [];
  39. foreach ($files as $idx => $file) {
  40. if (!$file->isValid()) {
  41. $result[$idx] = ErrorCode::ATTACHMENT_UPLOAD_INVALID;
  42. continue;
  43. }
  44. $fileSize = $file->getSize();
  45. if ($fileSize > $size) {
  46. $result[$idx] = ErrorCode::ATTACHMENT_SIZE_EXCEEDED;
  47. continue;
  48. }
  49. $fileMimeType = $file->getMimeType();
  50. if (!empty($mimeType) && !in_array($fileMimeType, $mimeType)) {
  51. $result[$idx] = ErrorCode::ATTACHMENT_MIME_NOT_ALLOWED;
  52. continue;
  53. }
  54. $clientName = $file->getClientOriginalName();
  55. $md5 = md5($clientName . time());
  56. $md5_filename = $md5 . '.' . $file->getClientOriginalExtension();
  57. try {
  58. $file->move($path, $md5_filename);
  59. $real_path = $path . '/' . $md5_filename;
  60. $url_path = $rel_path . '/' . $md5_filename;
  61. $attachment = new BaseAttachmentModel();
  62. $attachment->name = $clientName;
  63. $attachment->md5 = $md5;
  64. $attachment->path = $real_path;
  65. $attachment->url = $url_path;
  66. $attachment->size = $fileSize;
  67. $attachment->file_type = $fileMimeType;
  68. if ($attachment->save()) {
  69. $result[$idx] = route('attachment.download', $md5);
  70. } else {
  71. @unlink($real_path);
  72. $result[$idx] = ErrorCode::ATTACHMENT_SAVE_FAILED;
  73. }
  74. } catch (FileException $e) {
  75. $result[$idx] = ErrorCode::ATTACHMENT_MOVE_FAILED;
  76. }
  77. }
  78. if (count($result) == 1) {
  79. return array_shift($result);
  80. }
  81. return $result;
  82. } else {
  83. return ErrorCode::ATTACHMENT_UPLOAD_INVALID;
  84. }
  85. }
  86. /**
  87. * 删除附件
  88. *
  89. * @param $md5 string 删除文件的md5码
  90. * @return int 错误码or 0(成功)
  91. */
  92. public function deleteAttachment($md5) {
  93. $attachment = Attachment::where(['md5' => $md5])->first();
  94. if (!$attachment) {
  95. return ErrorCode::ATTACHMENT_NOT_EXIST;
  96. }
  97. if (file_exists($attachment->path)) {
  98. if (@unlink($attachment->path)) {
  99. if ($attachment->delete()) {
  100. return 0;
  101. } else {
  102. return ErrorCode::ATTACHMENT_RECORD_DELETE_FAILED;
  103. }
  104. } else {
  105. return ErrorCode::ATTACHMENT_DELETE_FAILED;
  106. }
  107. } else {
  108. return ErrorCode::ATTACHMENT_NOT_EXIST;
  109. }
  110. }
  111. }