wesley.chen 7 年之前
父节点
当前提交
0649d617e9

+ 5 - 2
app/Helper/AttachmentHelper.php

xqd xqd xqd
@@ -1,6 +1,7 @@
 <?php
 namespace App\Helper;
 
+use App\Services\OSS;
 use Illuminate\Http\Request;
 use Illuminate\Http\UploadedFile;
 use Symfony\Component\HttpFoundation\File\Exception\FileException;
@@ -75,7 +76,8 @@ trait AttachmentHelper
                     $attachment->size = $fileSize;
                     $attachment->file_type = $fileMimeType;
                     if ($attachment->save()) {
-                        $result[$idx] = $md5;
+                        $result['md5'] = $md5;
+                        $result['url'] = env('APP_URL').$url_path;
                     } else {
                         @unlink($real_path);
                         $result[$idx] = ErrorCode::ATTACHMENT_SAVE_FAILED;
@@ -150,7 +152,8 @@ trait AttachmentHelper
                     $attachment->file_type = $fileMimeType;
 
                     if ($attachment->save()) {
-                        $result[$idx] = $md5;
+                        $result['md5'] = $md5;
+                        $result['url'] = config('alioss.FileUrl').$md5_filename;
                     } else {
                         OSS::publicDeleteObject(config('alioss.BucketName'),$md5_filename);
                         $result[$idx] = ErrorCode::ATTACHMENT_SAVE_FAILED;

+ 12 - 10
app/Http/Controllers/Api/V1/AttachmentController.php

xqd xqd xqd xqd xqd
@@ -36,7 +36,7 @@ class AttachmentController extends Controller
         }
 
         return Response::download($attachment->path, $attachment->name, [
-            'Content-type'  => $attachment->file_type,
+            'Content-type' => $attachment->file_type,
             'Accept-Ranges' => 'bytes',
             'Accept-Length' => $attachment->size,
         ]);
@@ -57,8 +57,8 @@ class AttachmentController extends Controller
      *     "code": 0,
      *     "message": "",
      *     "data": [
-     *         "file": "f72e7dad80f597ed6621a009e82243ad",
-     *          //文件访问url http://localhost/attachment/f72e7dad80f597ed6621a009e82243ad
+     *         "md5": "57c0fe723e1d7f7b0e45fd455aee19f2",
+     *         "url": "http://dev.xxg.com/upload/upload/20180727/57c0fe723e1d7f7b0e45fd455aee19f2.jpg"
      *     ]
      * }
      * @apiErrorExample {json} Error-Response:
@@ -85,15 +85,16 @@ class AttachmentController extends Controller
      *    1202    ATTACHMENT_MIME_NOT_ALLOWED     附件类型不允许
      *    1203    ATTACHMENT_NOT_EXIST            附件不存在
      */
-    public function upload(Request $request) {
+    public function upload(Request $request)
+    {
         \Log::info($request->all());
         $validator = Validator::make($request->all(),
             [
-                'tag'         => 'required|alpha_dash',
+                'tag' => 'required|alpha_dash',
             ],
             [
-                'tag.required'      => 'tag必填',
-                'tag.alpha_dash'    => 'tag只能为字母数字中/下划线',
+                'tag.required' => 'tag必填',
+                'tag.alpha_dash' => 'tag只能为字母数字中/下划线',
             ]
         );
 
@@ -147,7 +148,8 @@ class AttachmentController extends Controller
      *    206     ATTACHMENT_RECORD_DELETE_FAILED 删除附件记录失败
      *    1203    ATTACHMENT_NOT_EXIST            附件不存在
      */
-    public function delete($md5) {
+    public function delete($md5)
+    {
         $result = $this->deleteAttachment($md5);
         if ($result === 0) {
             return $this->api(['result' => true]);
@@ -156,11 +158,11 @@ class AttachmentController extends Controller
         }
     }
 
-    public function index(){
+    public function index()
+    {
         $result = BaseAttachmentModel::all()->sortByDesc('id');
         return $this->api($result);
     }
 
 
-
 }

+ 187 - 0
app/Services/OSS.php

xqd
@@ -0,0 +1,187 @@
+<?php
+
+namespace App\Services;
+
+use JohnLui\AliyunOSS;
+
+use Exception;
+use DateTime;
+
+class OSS {
+
+    /* 城市名称:
+     *
+     *  经典网络下可选:杭州、上海、青岛、北京、张家口、深圳、香港、硅谷、弗吉尼亚、新加坡、悉尼、日本、法兰克福、迪拜
+     *  VPC 网络下可选:杭州、上海、青岛、北京、张家口、深圳、硅谷、弗吉尼亚、新加坡、悉尼、日本、法兰克福、迪拜
+     */
+    private $city = '北京';
+
+    // 经典网络 or VPC
+    private $networkType = '经典网络';
+
+    private $AccessKeyId;
+    private $AccessKeySecret;
+
+
+    private $ossClient;
+
+    /**
+     * 私有初始化 API,非 API,不用关注
+     * @param boolean 是否使用内网
+     */
+    public function __construct($isInternal = false)
+    {
+        if ($this->networkType == 'VPC' && !$isInternal) {
+            throw new Exception("VPC 网络下不提供外网上传、下载等功能");
+        }
+        $this->ossClient = AliyunOSS::boot(
+            $this->city,
+            $this->networkType,
+            $isInternal,
+            $this->AccessKeyId = config('alioss.AccessKeyId'),
+            $this->AccessKeySecret = config('alioss.AccessKeySecret')
+        );
+    }
+
+
+    /**
+     * 使用外网上传文件
+     * @param  string bucket名称
+     * @param  string 上传之后的 OSS object 名称
+     * @param  string 上传文件路径
+     * @return boolean 上传是否成功
+     */
+    public static function publicUpload($bucketName, $ossKey, $filePath, $options = [])
+    {
+        $oss = new OSS();
+        $oss->ossClient->setBucket($bucketName);
+        return $oss->ossClient->uploadFile($ossKey, $filePath, $options);
+    }
+
+    /**
+     * 使用阿里云内网上传文件
+     * @param  string bucket名称
+     * @param  string 上传之后的 OSS object 名称
+     * @param  string 上传文件路径
+     * @return boolean 上传是否成功
+     */
+    public static function privateUpload($bucketName, $ossKey, $filePath, $options = [])
+    {
+        $oss = new OSS(true);
+        $oss->ossClient->setBucket($bucketName);
+        return $oss->ossClient->uploadFile($ossKey, $filePath, $options);
+    }
+
+
+    /**
+     * 使用外网直接上传变量内容
+     * @param  string bucket名称
+     * @param  string 上传之后的 OSS object 名称
+     * @param  string 上传的变量
+     * @return boolean 上传是否成功
+     */
+    public static function publicUploadContent($bucketName, $ossKey, $content, $options = [])
+    {
+        $oss = new OSS();
+        $oss->ossClient->setBucket($bucketName);
+        return $oss->ossClient->uploadContent($ossKey, $content, $options);
+    }
+
+    /**
+     * 使用阿里云内网直接上传变量内容
+     * @param  string bucket名称
+     * @param  string 上传之后的 OSS object 名称
+     * @param  string 上传的变量
+     * @return boolean 上传是否成功
+     */
+    public static function privateUploadContent($bucketName, $ossKey, $content, $options = [])
+    {
+        $oss = new OSS(true);
+        $oss->ossClient->setBucket($bucketName);
+        return $oss->ossClient->uploadContent($ossKey, $content, $options);
+    }
+
+
+    /**
+     * 使用外网删除文件
+     * @param  string bucket名称
+     * @param  string 目标 OSS object 名称
+     * @return boolean 删除是否成功
+     */
+    public static function publicDeleteObject($bucketName, $ossKey)
+    {
+        $oss = new OSS();
+        $oss->ossClient->setBucket($bucketName);
+        return $oss->ossClient->deleteObject($bucketName, $ossKey);
+    }
+
+    /**
+     * 使用阿里云内网删除文件
+     * @param  string bucket名称
+     * @param  string 目标 OSS object 名称
+     * @return boolean 删除是否成功
+     */
+    public static function privateDeleteObject($bucketName, $ossKey)
+    {
+        $oss = new OSS(true);
+        $oss->ossClient->setBucket($bucketName);
+        return $oss->ossClient->deleteObject($bucketName, $ossKey);
+    }
+
+
+    /**
+     * -------------------------------------------------
+     *
+     *
+     *  下面不再分公网内网出 API,也不注释了,大家自行体会吧。。。
+     *
+     *
+     * -------------------------------------------------
+     */
+
+    public function copyObject($sourceBuckt, $sourceKey, $destBucket, $destKey)
+    {
+        $oss = new OSS();
+        return $oss->ossClient->copyObject($sourceBuckt, $sourceKey, $destBucket, $destKey);
+    }
+
+    public function moveObject($sourceBuckt, $sourceKey, $destBucket, $destKey)
+    {
+        $oss = new OSS();
+        return $oss->ossClient->moveObject($sourceBuckt, $sourceKey, $destBucket, $destKey);
+    }
+
+    // 获取公开文件的 URL
+    public static function getPublicObjectURL($bucketName, $ossKey)
+    {
+        $oss = new OSS();
+        $oss->ossClient->setBucket($bucketName);
+        return $oss->ossClient->getPublicUrl($ossKey);
+    }
+    // 获取私有文件的URL,并设定过期时间,如 \DateTime('+1 day')
+    public static function getPrivateObjectURLWithExpireTime($bucketName, $ossKey, DateTime $expire_time)
+    {
+        $oss = new OSS();
+        $oss->ossClient->setBucket($bucketName);
+        return $oss->ossClient->getUrl($ossKey, $expire_time);
+    }
+
+    public static function createBucket($bucketName)
+    {
+        $oss = new OSS();
+        return $oss->ossClient->createBucket($bucketName);
+    }
+
+    public static function getAllObjectKey($bucketName)
+    {
+        $oss = new OSS();
+        return $oss->ossClient->getAllObjectKey($bucketName);
+    }
+
+    public static function getObjectMeta($bucketName, $ossKey)
+    {
+        $oss = new OSS();
+        return $oss->ossClient->getObjectMeta($bucketName, $ossKey);
+    }
+
+}

+ 1 - 0
composer.json

xqd
@@ -10,6 +10,7 @@
         "fideloper/proxy": "~3.3",
         "germey/geetest": "^3.0",
         "intervention/image": "^2.4",
+        "johnlui/aliyun-oss": "^2.0.",
         "laravel/framework": "5.5.*",
         "laravel/passport": "^4.0",
         "laravel/tinker": "~1.0",

+ 10 - 0
config/alioss.php

xqd
@@ -0,0 +1,10 @@
+<?php
+
+return [
+    'ossServer' => env('ALIOSS_SERVER', 'http://oss-cn-beijing.aliyuncs.com'),                      // 外网
+    'ossServerInternal' => env('ALIOSS_SERVERINTERNAL', 'http://oss-cn-beijing-internal.aliyuncs.com'),      // 内网
+    'AccessKeyId' => env('ALIOSS_KEYID', 'LTAILL5VPO7heulm'),                     // key
+    'AccessKeySecret' => env('ALIOSS_KEYSECRET', 'w4UgiS2d6uj7jiRPkbDIaFkcJwUez5'),             // secret
+    'BucketName' => env('ALIOSS_BUCKETNAME', 'swdz-xcx') ,                 // bucket
+    'FileUrl' => env('ALIOSS_FileUrl', 'http://ossxcx.9026.com/')
+];

+ 1 - 1
public/apidoc/api_data.js

xqd
@@ -129,7 +129,7 @@ define({ "api": [
       "examples": [
         {
           "title": "Success-Response:",
-          "content": "HTTP/1.1 200 OK\n{\n    \"state\": true,\n    \"code\": 0,\n    \"message\": \"\",\n    \"data\": [\n        \"file\": \"f72e7dad80f597ed6621a009e82243ad\",\n         //文件访问url http://localhost/attachment/f72e7dad80f597ed6621a009e82243ad\n    ]\n}",
+          "content": "HTTP/1.1 200 OK\n{\n    \"state\": true,\n    \"code\": 0,\n    \"message\": \"\",\n    \"data\": [\n        \"md5\": \"57c0fe723e1d7f7b0e45fd455aee19f2\",\n        \"url\": \"http://dev.xxg.com/upload/upload/20180727/57c0fe723e1d7f7b0e45fd455aee19f2.jpg\"\n    ]\n}",
           "type": "json"
         }
       ]

+ 1 - 1
public/apidoc/api_data.json

xqd
@@ -129,7 +129,7 @@
       "examples": [
         {
           "title": "Success-Response:",
-          "content": "HTTP/1.1 200 OK\n{\n    \"state\": true,\n    \"code\": 0,\n    \"message\": \"\",\n    \"data\": [\n        \"file\": \"f72e7dad80f597ed6621a009e82243ad\",\n         //文件访问url http://localhost/attachment/f72e7dad80f597ed6621a009e82243ad\n    ]\n}",
+          "content": "HTTP/1.1 200 OK\n{\n    \"state\": true,\n    \"code\": 0,\n    \"message\": \"\",\n    \"data\": [\n        \"md5\": \"57c0fe723e1d7f7b0e45fd455aee19f2\",\n        \"url\": \"http://dev.xxg.com/upload/upload/20180727/57c0fe723e1d7f7b0e45fd455aee19f2.jpg\"\n    ]\n}",
           "type": "json"
         }
       ]

+ 1 - 1
public/apidoc/api_project.js

xqd
@@ -7,7 +7,7 @@ define({
   "apidoc": "0.3.0",
   "generator": {
     "name": "apidoc",
-    "time": "2018-07-25T09:17:33.346Z",
+    "time": "2018-07-27T08:05:27.543Z",
     "url": "http://apidocjs.com",
     "version": "0.17.6"
   }

+ 1 - 1
public/apidoc/api_project.json

xqd
@@ -7,7 +7,7 @@
   "apidoc": "0.3.0",
   "generator": {
     "name": "apidoc",
-    "time": "2018-07-25T09:17:33.346Z",
+    "time": "2018-07-27T08:05:27.543Z",
     "url": "http://apidocjs.com",
     "version": "0.17.6"
   }

+ 4 - 0
routes/api.php

xqd
@@ -24,6 +24,10 @@ $api = app('Dingo\Api\Routing\Router');
 
 $api->version('v1', ['namespace' => 'App\Http\Controllers\Api\V1'], function ($api) {
 
+    $api->post('attachment/upload', [
+        'as' => 'attachment.upload',
+        'uses' => 'AttachmentController@upload',
+    ]);
     $api->post('home/login', [
         'as' => 'home.login',
         'uses' => 'HomeController@login',