Browse Source

安装overtrue/laravel-filesystem-cos包

langshiyeye 1 year ago
parent
commit
1cea052212

+ 6 - 0
.env.example

@@ -57,3 +57,9 @@ VITE_PUSHER_HOST="${PUSHER_HOST}"
 VITE_PUSHER_PORT="${PUSHER_PORT}"
 VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
 VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
+
+COS_APP_ID="1322828622"
+COS_SECRET_ID=""
+COS_SECRET_KEY=""
+COS_REGION="ap-guangzhou"
+COS_BUCKET="dev"

+ 32 - 0
app/Http/Controllers/API/FileController.php

@@ -0,0 +1,32 @@
+<?php
+
+namespace App\Http\Controllers\API;
+
+use App\Http\Controllers\Controller;
+use App\Http\Requests\API\File\DownloadRequest;
+use App\Http\Requests\API\File\UploadRequest;
+use Illuminate\Support\Facades\Storage;
+
+class FileController extends Controller
+{
+
+    public function upload(UploadRequest $request)
+    {
+        // 获取上传的文件
+        $file = $request->file('file');
+        // 存储文件到腾讯云COS
+        $path='uploads/' .date('Ym/d/'). $file->getClientOriginalName();//文件相对路径
+        Storage::put($path, file_get_contents($file->getRealPath()));
+        // 返回响应
+        return response()->json([
+            'message' => 'File uploaded successfully.',
+            'path' => $path,
+        ]);
+    }
+
+    public function download(DownloadRequest $request)
+    {
+        $url=$request->url;
+        return Storage::download($url);
+    }
+}

+ 39 - 0
app/Http/Requests/API/File/DownloadRequest.php

@@ -0,0 +1,39 @@
+<?php
+
+namespace App\Http\Requests\API\File;
+
+use App\Http\Requests\RuleHelper;
+use Illuminate\Foundation\Http\FormRequest;
+use Illuminate\Support\Facades\Storage;
+
+class DownloadRequest extends FormRequest
+{
+    use RuleHelper;
+    /**
+     * Determine if the user is authorized to make this request.
+     */
+    public function authorize(): bool
+    {
+        return true;
+    }
+
+    /**
+     * Get the validation rules that apply to the request.
+     *
+     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
+     */
+    public function rules(): array
+    {
+        return [
+            'url' => [
+                'required',
+                function ($attribute, $value, $fail) {
+                    // 检查文件是否存在默认存储对象系统中
+                    if (!Storage::exists($value)) {
+                        $fail('url file does not exist');
+                    }
+                },
+            ],
+        ];
+    }
+}

+ 40 - 0
app/Http/Requests/API/File/UploadRequest.php

@@ -0,0 +1,40 @@
+<?php
+
+namespace App\Http\Requests\API\File;
+
+use App\Http\Requests\RuleHelper;
+use Illuminate\Foundation\Http\FormRequest;
+use Illuminate\Support\Facades\Storage;
+
+class UploadRequest extends FormRequest
+{
+    use RuleHelper;
+    /**
+     * Determine if the user is authorized to make this request.
+     */
+    public function authorize(): bool
+    {
+        return true;
+    }
+
+    /**
+     * Get the validation rules that apply to the request.
+     *
+     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
+     */
+    public function rules(): array
+    {
+        return [
+            'file' => [
+                'required',
+                function ($attribute, $value, $fail) {
+                    // 检查文件是否存在默认存储对象系统中
+                    if (Storage::exists('uploads/' .date('Ym/d/'). $value->getClientOriginalName())) {
+                        $fail('File already exists');
+                    }
+                },
+            ],
+
+        ];
+    }
+}

+ 1 - 0
composer.json

@@ -11,6 +11,7 @@
         "laravel/framework": "^10.10",
         "laravel/sanctum": "^3.3",
         "laravel/tinker": "^2.8",
+        "overtrue/laravel-filesystem-cos": "^3.4",
         "overtrue/laravel-query-logger": "^3.1",
         "spatie/laravel-permission": "^6.3",
         "tucker-eric/eloquentfilter": "^3.2"

+ 202 - 2
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "d0446807358361b59ce0bd0aa8a0c545",
+    "content-hash": "9e76908a58f1c11692d8918c4ac2bb5c",
     "packages": [
         {
             "name": "brick/math",
@@ -2124,6 +2124,108 @@
             ],
             "time": "2023-02-08T01:06:31+00:00"
         },
+        {
+            "name": "overtrue/flysystem-cos",
+            "version": "5.1.8",
+            "dist": {
+                "type": "zip",
+                "url": "https://mirrors.tencent.com/repository/composer/overtrue/flysystem-cos/5.1.8/overtrue-flysystem-cos-5.1.8.zip",
+                "reference": "9a8f2eba21d7c869b7f53798a5329f55e0a7e42f",
+                "shasum": ""
+            },
+            "require": {
+                "league/flysystem": "^3.0",
+                "overtrue/qcloud-cos-client": "^2.1.4",
+                "php": ">=8.0.2"
+            },
+            "require-dev": {
+                "brainmaestro/composer-git-hooks": "dev-master",
+                "laravel/pint": "^1.6",
+                "league/flysystem-adapter-test-utilities": "^3.0",
+                "mockery/mockery": "^1.0",
+                "phpunit/phpunit": "^9.5"
+            },
+            "type": "library",
+            "extra": {
+                "hooks": {
+                    "pre-commit": [
+                        "composer fix-style",
+                        "composer test"
+                    ],
+                    "pre-push": [
+                        "composer test",
+                        "composer check-style"
+                    ]
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Overtrue\\Flysystem\\Cos\\": "src"
+                }
+            },
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "overtrue",
+                    "email": "i@overtrue.me"
+                }
+            ],
+            "description": "Flysystem adapter for the QCloud COS storage.",
+            "time": "2023-12-18T06:08:54+00:00"
+        },
+        {
+            "name": "overtrue/laravel-filesystem-cos",
+            "version": "3.4.0",
+            "dist": {
+                "type": "zip",
+                "url": "https://mirrors.tencent.com/repository/composer/overtrue/laravel-filesystem-cos/3.4.0/overtrue-laravel-filesystem-cos-3.4.0.zip",
+                "reference": "73a46723a320d04714e9db3f2c28c88f772e2ece",
+                "shasum": ""
+            },
+            "require": {
+                "laravel/framework": "^9.0|^10.0",
+                "overtrue/flysystem-cos": "^5.1"
+            },
+            "require-dev": {
+                "brainmaestro/composer-git-hooks": "dev-master",
+                "laravel/pint": "^1.5",
+                "mockery/mockery": "^1.0"
+            },
+            "type": "library",
+            "extra": {
+                "laravel": {
+                    "providers": [
+                        "Overtrue\\LaravelFilesystem\\Cos\\CosStorageServiceProvider"
+                    ]
+                },
+                "hooks": {
+                    "pre-commit": [
+                        "composer check-style"
+                    ],
+                    "pre-push": [
+                        "composer check-style"
+                    ]
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Overtrue\\LaravelFilesystem\\Cos\\": "src"
+                }
+            },
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "overtrue",
+                    "email": "i@overtrue.me"
+                }
+            ],
+            "description": "A Cos storage filesystem for Laravel.",
+            "time": "2023-02-15T08:27:47+00:00"
+        },
         {
             "name": "overtrue/laravel-query-logger",
             "version": "3.1.0",
@@ -2173,6 +2275,63 @@
             "description": "A dev tool to log all queries for laravel application.",
             "time": "2023-02-15T08:34:54+00:00"
         },
+        {
+            "name": "overtrue/qcloud-cos-client",
+            "version": "2.1.4",
+            "dist": {
+                "type": "zip",
+                "url": "https://mirrors.tencent.com/repository/composer/overtrue/qcloud-cos-client/2.1.4/overtrue-qcloud-cos-client-2.1.4.zip",
+                "reference": "7d4fe598cb81e588b7735a32762c4fa2b47f9492",
+                "shasum": ""
+            },
+            "require": {
+                "ext-dom": "*",
+                "ext-json": "*",
+                "ext-libxml": "*",
+                "ext-simplexml": "*",
+                "guzzlehttp/guzzle": "^7.4",
+                "php": ">=8.0.2",
+                "psr/http-message": "^1.0|^2.0",
+                "thenorthmemory/xml": "^1.0"
+            },
+            "require-dev": {
+                "brainmaestro/composer-git-hooks": "^2.8",
+                "friendsofphp/php-cs-fixer": "^3.5",
+                "laravel/pint": "^1.2",
+                "mockery/mockery": "^1.0",
+                "monolog/monolog": "^2.5",
+                "phpunit/phpunit": "^9.5"
+            },
+            "type": "library",
+            "extra": {
+                "hooks": {
+                    "pre-commit": [
+                        "composer test",
+                        "composer check-style"
+                    ],
+                    "pre-push": [
+                        "composer test",
+                        "composer check-style"
+                    ]
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Overtrue\\CosClient\\": "src"
+                }
+            },
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "overtrue",
+                    "email": "anzhengchao@gmail.com"
+                }
+            ],
+            "description": "Client of QCloud.com COS",
+            "time": "2023-12-18T06:07:39+00:00"
+        },
         {
             "name": "phpoption/phpoption",
             "version": "1.9.2",
@@ -4551,6 +4710,47 @@
             ],
             "time": "2023-12-28T19:16:56+00:00"
         },
+        {
+            "name": "thenorthmemory/xml",
+            "version": "1.1.1",
+            "dist": {
+                "type": "zip",
+                "url": "https://mirrors.cloud.tencent.com/repository/composer/thenorthmemory/xml/1.1.1/thenorthmemory-xml-1.1.1.zip",
+                "reference": "6f50c63450a0b098772423f8bdc3c4ad2c4c30bb",
+                "shasum": ""
+            },
+            "require": {
+                "ext-libxml": "*",
+                "ext-simplexml": "*",
+                "php": ">=7.1.2"
+            },
+            "require-dev": {
+                "phpstan/phpstan": "^0.12.89 || ^1.0",
+                "phpunit/phpunit": "^7.5 || ^8.5.16 || ^9.3.5"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "TheNorthMemory\\Xml\\": "src/"
+                }
+            },
+            "license": [
+                "Apache-2.0"
+            ],
+            "authors": [
+                {
+                    "name": "James ZHANG",
+                    "homepage": "https://github.com/TheNorthMemory"
+                }
+            ],
+            "description": "A wrapper of the XML parser and builder",
+            "homepage": "https://github.com/TheNorthMemory/xml",
+            "keywords": [
+                "xml-builder",
+                "xml-parser"
+            ],
+            "time": "2023-01-15T06:01:13+00:00"
+        },
         {
             "name": "tijsverkoyen/css-to-inline-styles",
             "version": "v2.2.7",
@@ -7678,5 +7878,5 @@
         "php": "^8.1"
     },
     "platform-dev": [],
-    "plugin-api-version": "2.3.0"
+    "plugin-api-version": "2.6.0"
 }

+ 17 - 0
config/filesystems.php

@@ -55,6 +55,23 @@ return [
             'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
             'throw' => false,
         ],
+        'cos' => [
+            'driver' => 'cos',
+            'app_id'     => env('COS_APP_ID'),
+            'secret_id'  => env('COS_SECRET_ID'),
+            'secret_key' => env('COS_SECRET_KEY'),
+            'region'     => env('COS_REGION', 'ap-guangzhou'),
+            'bucket'     => env('COS_BUCKET'),  // 不带数字 app_id 后缀
+            'signed_url' => true,   // 可选,如果 bucket 为私有访问请打开此项
+            'use_https' => true,    // 可选,是否使用 https,默认 false
+            //'domain' => 'https://test-1314247667.cos.ap-guangzhou.myqcloud.com',  // 可选,自定义域名
+            'cdn' => env('COS_CDN'),    // 可选,使用 CDN 域名时指定生成的 URL host
+            'prefix' => env('COS_PATH_PREFIX'), // 全局路径前缀
+            'guzzle' => [
+                'timeout' => env('COS_TIMEOUT', 60),
+                'connect_timeout' => env('COS_CONNECT_TIMEOUT', 60),
+            ],
+        ],
 
     ],
 

+ 3 - 0
routes/api.php

@@ -89,5 +89,8 @@ Route::middleware(['auth:sanctum'])->group(function () {
         Route::post("config-setting", [API\ConfigController::class, "setting"])->name("config.setting");
 
         Route::get("plan-by-assets", [API\PlanController::class, "byAssets"])->name("plan.by-assets");
+
+        Route::post("upload", [API\FileController::class, "upload"])->name("fileUpload");
+        Route::post("download", [API\FileController::class, "download"])->name("fileDownload");
     });
 });