Browse Source

Merge pull request #1931 from visuddhinanda/laravel

添加头像支持
visuddhinanda 2 years ago
parent
commit
64ee2d3118

+ 11 - 5
app/Http/Api/StudioApi.php

@@ -2,6 +2,8 @@
 namespace App\Http\Api;
 
 use App\Models\UserInfo;
+use Illuminate\Support\Facades\Storage;
+use Illuminate\Support\Facades\App;
 
 class StudioApi{
     public static function getIdByName($name){
@@ -29,13 +31,17 @@ class StudioApi{
         if(!$userInfo){
             return false;
         }
-        return [
+        $data = [
             'id'=>$id,
-            'nickName'=>$userInfo['nickname'],
-            'realName'=>$userInfo['username'],
-            'studioName'=>$userInfo['username'],
-            'avatar'=>'',
+            'nickName'=>$userInfo->nickname,
+            'realName'=>$userInfo->username,
+            'studioName'=>$userInfo->username,
         ];
+        if($userInfo->avatar){
+            $img = str_replace('.jpg','_s.jpg',$userInfo->avatar);
+            $data['avatar'] = Storage::url($img);
+        }
+        return $data;
     }
 
     public static function getByIntId($id){

+ 9 - 2
app/Http/Api/UserApi.php

@@ -1,7 +1,10 @@
 <?php
 namespace App\Http\Api;
+
 use App\Models\UserInfo;
 use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Storage;
+use Illuminate\Support\Facades\App;
 
 class UserApi{
     public static function getIdByName($name){
@@ -39,13 +42,17 @@ class UserApi{
     public static function getByUuid($id){
         $user = UserInfo::where('userid',$id)->first();
         if($user){
-            return [
+            $data = [
                 'id'=>$id,
                 'nickName'=>$user['nickname'],
                 'userName'=>$user['username'],
                 'realName'=>$user['username'],
-                'avatar'=>'',
             ];
+            if($user->avatar){
+                $img = str_replace('.jpg','_s.jpg',$user->avatar);
+                $data['avatar'] = Storage::url($img);
+            }
+            return $data;
         }else{
             Log::error('$user=null;$id='.$id);
             return [

+ 65 - 8
app/Http/Controllers/AttachmentController.php

@@ -70,23 +70,46 @@ class AttachmentController extends Controller
         $request->validate([
             'file' => 'required',
         ]);
-        $file = $request->file('file');
 
-       //Move Uploaded File
+        $file = $request->file('file');
         $bucket = config('mint.attachments.bucket_name.permanent');
         $fileId = Str::uuid();
         $ext = $file->getClientOriginalExtension();
-        $name = $fileId.'.'.$ext;
-        $filename = $file->storeAs($bucket,$name);
+
+
+        if($request->get('type') === 'avatar'){
+            $resize = Image::make($file)->fit(512);
+            Storage::put($bucket.'/'.$fileId.'.jpg',$resize->stream());
+            $resize = Image::make($file)->fit(256);
+            Storage::put($bucket.'/'.$fileId.'_m.jpg',$resize->stream());
+            $resize = Image::make($file)->fit(128);
+            Storage::put($bucket.'/'.$fileId.'_s.jpg',$resize->stream());
+            $name = $fileId.'.jpg';
+        }else{
+            //Move Uploaded File
+            $name = $fileId.'.'.$ext;
+            $filename = $file->storeAs($bucket,$name);
+        }
+
         $attachment = new Attachment;
         $attachment->id = $fileId;
         $attachment->user_uid = $user['user_uid'];
         $attachment->bucket = $bucket;
         $attachment->name = $name;
-        $attachment->title = $file->getClientOriginalName();
+        $attachment->filename = $file->getClientOriginalName();
+        $path_parts = pathinfo($file->getClientOriginalName());
+        $attachment->title = $path_parts['filename'];
         $attachment->size = $file->getSize();
         $attachment->content_type = $file->getMimeType();
         $attachment->status = 'public';
+        if($request->has('studio')){
+            $owner_uid = StudioApi::getIdByName($request->get('studio'));
+        }else{
+            $owner_uid = $user['user_uid'];
+        }
+        if($owner_uid){
+            $attachment->owner_uid = $owner_uid;
+        }
         $attachment->save();
 
         $type = explode('/',$file->getMimeType());
@@ -112,13 +135,14 @@ class AttachmentController extends Controller
                 # code...
                 break;
         }
+        /*
         $json = array(
             'name' => $filename,
             'size' => $file->getSize(),
             'type' => $file->getMimeType(),
             'url' => Storage::url($bucket.'/'.$name),
             'uid' => $attachment->id,
-            );
+            );*/
         return $this->ok(new AttachmentResource($attachment));
 
     }
@@ -150,11 +174,44 @@ class AttachmentController extends Controller
     /**
      * Remove the specified resource from storage.
      *
-     * @param  \App\Models\Attachment  $attachment
+     * @param  string  $id
      * @return \Illuminate\Http\Response
      */
-    public function destroy(Attachment $attachment)
+    public function destroy(Request $request,string $id)
     {
         //
+        $user = AuthApi::current($request);
+        if(!$user){
+            return $this->error(__('auth.failed'),401,401);
+        }
+        if(Str::isUuid($id)){
+            $res = Attachment::where('id',$id)->first();
+        }else{
+            /**
+             * 从文件名获取bucket和name
+             */
+            $pos = mb_strrpos($request->get('name'),'/',0,"UTF-8");
+            if($pos === false){
+                return $this->error('无效的文件名',500,500);
+            }
+            $bucket = mb_substr($request->get('name'),0,$pos,'UTF-8');
+            $name = mb_substr($request->get('name'),$pos+1,NULL,'UTF-8');
+            $res = Attachment::where('bucket',$bucket)
+                            ->where('name',$name)
+                            ->first();
+        }
+        if(!$res){
+            return $this->error('no res');
+        }
+        if($user['user_uid']!==$res->user_uid){
+            return $this->error(__('auth.failed'),403,403);
+        }
+        $del = $res->delete();
+        //删除文件
+        $path_parts = pathinfo($request->get('name'));
+        Storage::delete($request->get('name'));
+        Storage::delete($path_parts['dirname'].'/'.$path_parts['filename'].'_m.jpg');
+        Storage::delete($path_parts['dirname'].'/'.$path_parts['filename'].'_s.jpg');
+        return $this->ok($del);
     }
 }

+ 6 - 0
app/Http/Controllers/AuthController.php

@@ -8,6 +8,8 @@ use Firebase\JWT\JWT;
 use Firebase\JWT\Key;
 use App\Http\Api\AuthApi;
 use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Storage;
+use Illuminate\Support\Facades\App;
 
 class AuthController extends Controller
 {
@@ -105,6 +107,10 @@ class AuthController extends Controller
                 "roles"=> [],
                 "token"=>\substr($request->header('Authorization'),7) ,
             ];
+            if($userInfo->avatar){
+                $img = str_replace('.jpg','_s.jpg',$userInfo->avatar);
+                $user['avatar'] = Storage::url($img);
+            }
             return $this->ok($user);
         }else{
             return $this->error('invalid token',[401],401);

+ 11 - 6
app/Http/Controllers/UserController.php

@@ -28,11 +28,8 @@ class UserController extends Controller
         $table = $table->skip($request->get("offset",0))
                        ->take($request->get('limit',20));
         $result = $table->get();
-        if($result){
-            return $this->ok(['rows'=>UserResource::collection($result),'count'=>$count]);
-        }else{
-            return $this->error();
-        }
+        return $this->ok(['rows'=>UserResource::collection($result),'count'=>$count]);
+
     }
 
     /**
@@ -55,18 +52,26 @@ class UserController extends Controller
     public function show($id)
     {
         //
+        $user = UserInfo::where('userid',$id)->first();
+        return $this->ok(new UserResource($user));
     }
 
     /**
      * Update the specified resource in storage.
      *
      * @param  \Illuminate\Http\Request  $request
-     * @param  int  $id
+     * @param  string  $id
      * @return \Illuminate\Http\Response
      */
     public function update(Request $request, $id)
     {
         //
+        $user = UserInfo::where('userid',$id)->first();
+        $user->nickname = $request->get('nickName');
+        $user->avatar = $request->get('avatar');
+        $user->email = $request->get('email');
+        $user->save();
+        return $this->ok(new UserResource($user));
     }
 
     /**

+ 13 - 4
app/Http/Resources/UserResource.php

@@ -3,6 +3,8 @@
 namespace App\Http\Resources;
 
 use Illuminate\Http\Resources\Json\JsonResource;
+use Illuminate\Support\Facades\Storage;
+use Illuminate\Support\Facades\App;
 
 class UserResource extends JsonResource
 {
@@ -14,10 +16,17 @@ class UserResource extends JsonResource
      */
     public function toArray($request)
     {
-        return [
-            "id"=>$this['userid'],
-            "userName"=> $this['username'],
-            "nickName"=> $this['nickname'],
+        $data = [
+            "id"=>$this->userid,
+            "userName"=> $this->username,
+            "nickName"=> $this->nickname,
+            "email"=> $this->email,
         ];
+        if($this->avatar){
+            $data['avatarName'] = $this->avatar;
+            $img = str_replace('.jpg','_s.jpg',$this->avatar);
+            $data['avatar'] = Storage::url($img);
+        }
+        return $data;
     }
 }

+ 34 - 0
database/migrations/2024_01_26_015425_add_avatar_in_user_infos.php

@@ -0,0 +1,34 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class AddAvatarInUserInfos extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('user_infos', function (Blueprint $table) {
+            //
+            $table->string('avatar',1024)->nullable();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('user_infos', function (Blueprint $table) {
+            //
+            $table->dropColumn('avatar');
+        });
+    }
+}

+ 36 - 0
database/migrations/2024_02_02_033216_add_filename_in_attachments.php

@@ -0,0 +1,36 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class AddFilenameInAttachments extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('attachments', function (Blueprint $table) {
+            //
+            $table->text('filename')->nullable()->index();
+
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('attachments', function (Blueprint $table) {
+            //
+            $table->dropColumn('filename');
+
+        });
+    }
+}

+ 36 - 0
database/migrations/2024_02_02_141216_add_owner_uid_in_attachments.php

@@ -0,0 +1,36 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class AddOwnerUidInAttachments extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('attachments', function (Blueprint $table) {
+            //
+            $table->uuid('owner_uid')->nullable()->index();
+
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('attachments', function (Blueprint $table) {
+            //
+            $table->dropColumn('owner_uid');
+
+        });
+    }
+}