AttachmentHelper.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. public function aliUpload(Request $request, $key, $tag = 'files', $size = 10 * 1024 * 1024, array $mimeType = []){
  86. if ($request->hasFile($key)) {
  87. $rel_path = '/upload/' . $tag . '/' . date('Ymd');
  88. $path = public_path() . $rel_path;
  89. if (!file_exists($path)) {
  90. if (!@mkdir($path, 0755, true)) {
  91. return ErrorCode::ATTACHMENT_MKDIR_FAILED;
  92. }
  93. }
  94. $files = $request->file($key);
  95. if ($files === null) {
  96. return ErrorCode::ATTACHMENT_UPLOAD_INVALID;
  97. }
  98. if ($files instanceof UploadedFile) {
  99. $files = [$files];
  100. }
  101. $result = [];
  102. foreach ($files as $idx => $file) {
  103. if (!$file->isValid()) {
  104. $result[$idx] = ErrorCode::ATTACHMENT_UPLOAD_INVALID;
  105. continue;
  106. }
  107. $fileSize = $file->getSize();
  108. if ($fileSize > $size) {
  109. $result[$idx] = ErrorCode::ATTACHMENT_SIZE_EXCEEDED;
  110. continue;
  111. }
  112. $fileMimeType = $file->getMimeType();
  113. \Log::info("fileMimeType:".$fileMimeType);
  114. if (!empty($mimeType) && !in_array($fileMimeType, $mimeType)) {
  115. $result[$idx] = ErrorCode::ATTACHMENT_MIME_NOT_ALLOWED;
  116. continue;
  117. }
  118. $clientName = $file->getClientOriginalName();
  119. $md5 = md5($clientName . time());
  120. $md5_filename = $md5 . '.' . $file->getClientOriginalExtension();
  121. try {
  122. $file_Path = $file->getRealPath();
  123. OSS::publicUpload(config('alioss.BucketName'),$md5_filename, $file_Path);
  124. $attachment = new BaseAttachmentModel();
  125. $attachment->name = $clientName;
  126. $attachment->md5 = $md5;
  127. $attachment->path = config('alioss.FileUrl').$md5_filename;
  128. $attachment->url = config('alioss.FileUrl').$md5_filename;
  129. $attachment->size = $fileSize;
  130. $attachment->file_type = $fileMimeType;
  131. if ($attachment->save()) {
  132. $result[$idx] = $md5;
  133. } else {
  134. OSS::publicDeleteObject(config('alioss.BucketName'),$md5_filename);
  135. $result[$idx] = ErrorCode::ATTACHMENT_SAVE_FAILED;
  136. }
  137. } catch (FileException $e) {
  138. $result[$idx] = ErrorCode::ATTACHMENT_MOVE_FAILED;
  139. }
  140. }
  141. if (count($result) == 1) {
  142. return array_shift($result);
  143. }
  144. return $result;
  145. } else {
  146. return ErrorCode::ATTACHMENT_UPLOAD_INVALID;
  147. }
  148. }
  149. /**
  150. * 删除附件
  151. *
  152. * @param $md5 string 删除文件的md5码
  153. * @return int 错误码or 0(成功)
  154. */
  155. public function deleteAttachment($md5) {
  156. $attachment = Attachment::where(['md5' => $md5])->first();
  157. if (!$attachment) {
  158. return ErrorCode::ATTACHMENT_NOT_EXIST;
  159. }
  160. if (file_exists($attachment->path)) {
  161. if($attachment->path == $attachment->url){
  162. $key = explode('/',$attachment->path);
  163. OSS::publicDeleteObject(config('alioss.BucketName'),end($key));
  164. return 0;
  165. }else{
  166. if (@unlink($attachment->path)) {
  167. if ($attachment->delete()) {
  168. return 0;
  169. } else {
  170. return ErrorCode::ATTACHMENT_RECORD_DELETE_FAILED;
  171. }
  172. } else {
  173. return ErrorCode::ATTACHMENT_DELETE_FAILED;
  174. }
  175. }
  176. } else {
  177. return ErrorCode::ATTACHMENT_NOT_EXIST;
  178. }
  179. }
  180. }