Bladeren bron

文件上传功能优化

jqh 5 jaren geleden
bovenliggende
commit
9a92ed69b9

+ 0 - 1
src/Form/Concerns/HasFiles.php

@@ -6,7 +6,6 @@ use Dcat\Admin\Admin;
 use Dcat\Admin\Contracts\UploadField as UploadFieldInterface;
 use Dcat\Admin\Form\Builder;
 use Dcat\Admin\Form\Field;
-use Illuminate\Http\JsonResponse;
 use Symfony\Component\HttpFoundation\File\UploadedFile;
 use Symfony\Component\HttpFoundation\Response;
 

+ 28 - 35
src/Form/Field/UploadField.php

@@ -2,7 +2,7 @@
 
 namespace Dcat\Admin\Form\Field;
 
-use Dcat\Admin\Admin;
+use Dcat\Admin\Traits\HasUploadedFile;
 use Illuminate\Support\Arr;
 use Illuminate\Support\Facades\Storage;
 use Illuminate\Support\Facades\URL;
@@ -12,6 +12,10 @@ use Symfony\Component\HttpFoundation\Response;
 
 trait UploadField
 {
+    use HasUploadedFile {
+        disk as _disk;
+    }
+
     /**
      * Upload directory.
      *
@@ -165,50 +169,39 @@ trait UploadField
      */
     public function upload(UploadedFile $file)
     {
-        try {
-            $request = request();
-
-            $id = $request->get('_id');
-
-            /* @var \Dcat\Admin\Support\WebUploader $webUploader */
-            $webUploader = Admin::context()->webUploader;
+        $request = request();
 
-            if (! $id) {
-                return $webUploader->responseErrorMessage(403, 'Missing id');
-            }
-
-            if ($errors = $this->getErrorMessages($file)) {
-                $webUploader->deleteTempFile();
-
-                return $webUploader->responseValidationMessage($errors);
-            }
+        $id = $request->get('_id');
 
-            $this->name = $this->getStoreName($file);
-
-            $this->renameIfExists($file);
+        if (! $id) {
+            return $this->responseErrorMessage(403, 'Missing id');
+        }
 
-            $this->prepareFile($file);
+        if ($errors = $this->getErrorMessages($file)) {
+            return $this->responseValidationMessage($errors);
+        }
 
-            if (! is_null($this->storagePermission)) {
-                $result = $this->getStorage()->putFileAs($this->getDirectory(), $file, $this->name, $this->storagePermission);
-            } else {
-                $result = $this->getStorage()->putFileAs($this->getDirectory(), $file, $this->name);
-            }
+        $this->name = $this->getStoreName($file);
 
-            $webUploader->deleteTempFile();
+        $this->renameIfExists($file);
 
-            if ($result) {
-                $path = $this->getUploadPath();
+        $this->prepareFile($file);
 
-                return $webUploader->responseUploaded($path, $this->objectUrl($path));
-            }
+        if (! is_null($this->storagePermission)) {
+            $result = $this->getStorage()->putFileAs($this->getDirectory(), $file, $this->name, $this->storagePermission);
+        } else {
+            $result = $this->getStorage()->putFileAs($this->getDirectory(), $file, $this->name);
+        }
 
-            return $webUploader->responseFailedMessage();
-        } catch (\Throwable $e) {
-            $webUploader->deleteTempFile();
+        if ($result) {
+            $path = $this->getUploadPath();
 
-            throw $e;
+            // 上传成功
+            return $this->responseUploaded($path, $this->objectUrl($path));
         }
+
+        // 上传失败
+        return $this->responseErrorMessage(trans('admin.upload.upload_failed'));
     }
 
     /**

+ 10 - 5
src/Middleware/WebUploader.php

@@ -5,7 +5,6 @@ namespace Dcat\Admin\Middleware;
 use Dcat\Admin\Admin;
 use Dcat\Admin\Support\WebUploader as Uploader;
 use Illuminate\Http\Request;
-use Symfony\Component\HttpFoundation\File\Exception\FileException;
 
 /**
  * 文件分块上传合并处理中间件.
@@ -26,14 +25,20 @@ class WebUploader
         try {
             if (! $file = $webUploader->getCompleteUploadedFile()) {
                 // 分块未上传完毕,返回已合并成功信息
-                return $webUploader->responseMerged();
+                return response()->json(['merge' => 1]);
             }
-        } catch (FileException $e) {
+
+            $response = $next($request);
+
+            // 移除临时文件
+            $webUploader->deleteTempFile();
+
+            return $response;
+        } catch (\Throwable $e) {
+            // 移除临时文件
             $webUploader->deleteTempFile();
 
             throw $e;
         }
-
-        return $next($request);
     }
 }

+ 0 - 87
src/Support/WebUploader.php

@@ -65,83 +65,6 @@ class WebUploader
         return true;
     }
 
-    /**
-     * 响应上传成功信息.
-     *
-     * @param string $path 文件完整路径
-     * @param string $url
-     * @return \Illuminate\Http\JsonResponse
-     */
-    public function responseUploaded(string $path, string $url)
-    {
-        return $this->response([
-            'status' => true,
-            'id'     => $path,
-            'name'   => basename($path),
-            'path'   => basename($path),
-            'url'    => $url,
-        ]);
-    }
-
-    /**
-     * 返回分块文件已合并信息.
-     *
-     * @return \Illuminate\Http\JsonResponse
-     */
-    public function responseMerged()
-    {
-        return $this->response(['merge' => 1]);
-    }
-
-    /**
-     * 响应失败信息.
-     *
-     * @param string $message
-     *
-     * @return \Illuminate\Http\JsonResponse
-     */
-    public function responseFailedMessage(string $message = null)
-    {
-        return $this->responseErrorMessage(107, $message ?: trans('admin.upload.upload_failed'));
-    }
-
-    /**
-     * @param FileException $e
-     *
-     * @return \Illuminate\Http\JsonResponse
-     */
-    public function responseFileException(FileException $e)
-    {
-        return $this->responseValidationMessage($e->getMessage());
-    }
-
-    /**
-     * 响应验证失败信息.
-     *
-     * @param mixed $message
-     *
-     * @return \Illuminate\Http\JsonResponse
-     */
-    public function responseValidationMessage($message)
-    {
-        return $this->responseErrorMessage(103, $message);
-    }
-
-    /**
-     * 响应失败信息.
-     *
-     * @param $code
-     * @param $error
-     *
-     * @return \Illuminate\Http\JsonResponse
-     */
-    public function responseErrorMessage($code, $error)
-    {
-        return $this->response([
-            'error' => ['code' => $code, 'message' => $error], 'status' => false,
-        ]);
-    }
-
     /**
      * 获取完整的上传文件.
      *
@@ -319,14 +242,4 @@ class WebUploader
 
         return trim($dir, '/');
     }
-
-    /**
-     * @param array $data
-     *
-     * @return \Illuminate\Http\JsonResponse
-     */
-    public function response($data)
-    {
-        return response()->json($data);
-    }
 }

+ 95 - 0
src/Traits/HasUploadedFile.php

@@ -0,0 +1,95 @@
+<?php
+
+namespace Dcat\Admin\Traits;
+
+use Dcat\Admin\Admin;
+use Dcat\Admin\Support\WebUploader;
+use Illuminate\Filesystem\FilesystemAdapter;
+use Illuminate\Support\Facades\Storage;
+use Symfony\Component\HttpFoundation\File\Exception\FileException;
+
+/**
+ * 文件上传辅助功能.
+ *
+ * Trait HasUploadedFile
+ * @package Dcat\Admin\Traits
+ */
+trait HasUploadedFile
+{
+    /**
+     * 获取文件上传管理.
+     *
+     * @return WebUploader
+     */
+    public function uploader()
+    {
+        return Admin::context()->webUploader;
+    }
+
+    /**
+     * 获取上传文件.
+     *
+     * @return \Symfony\Component\HttpFoundation\File\UploadedFile|void
+     */
+    public function file()
+    {
+        return $this->uploader()->getCompleteUploadedFile();
+    }
+
+    /**
+     * 获取文件管理仓库.
+     *
+     * @param string|null $disk
+     *
+     * @return \Illuminate\Contracts\Filesystem\Filesystem|FilesystemAdapter
+     */
+    public function disk(string $disk = null)
+    {
+        return Storage::disk($disk ?: config('admin.upload.disk'));
+    }
+
+    /**
+     * 响应上传成功信息.
+     *
+     * @param string $path 文件完整路径
+     * @param string $url
+     * @return \Illuminate\Http\JsonResponse
+     */
+    public function responseUploaded(string $path, string $url)
+    {
+        return response()->json([
+            'status' => true,
+            'id'     => $path,
+            'name'   => basename($path),
+            'path'   => basename($path),
+            'url'    => $url,
+        ]);
+    }
+
+    /**
+     * 响应验证失败信息.
+     *
+     * @param mixed $message
+     *
+     * @return \Illuminate\Http\JsonResponse
+     */
+    public function responseValidationMessage($message)
+    {
+        return $this->responseErrorMessage($message, 103);
+    }
+
+    /**
+     * 响应失败信息.
+     *
+     * @param $error
+     * @param $code
+     *
+     * @return \Illuminate\Http\JsonResponse
+     */
+    public function responseErrorMessage($error, $code = 107)
+    {
+        return response()->json([
+            'error' => ['code' => $code, 'message' => $error], 'status' => false,
+        ]);
+    }
+}