visuddhinanda 2 anni fa
parent
commit
e12a9de055

+ 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);