| Conditions | 5 |
| Paths | 4 |
| Total Lines | 41 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 21 | public function updateAvatar(Request $request) |
||
| 22 | { |
||
| 23 | $isValid = $request->file('avatar')->isValid(); |
||
| 24 | if($isValid){ |
||
| 25 | $extension = $request->file('avatar')->extension(); |
||
| 26 | }else{ |
||
| 27 | $output=[ |
||
| 28 | 'ret' => '400', |
||
| 29 | 'desc' => 'Invalid file', |
||
| 30 | 'data' => null |
||
| 31 | ]; |
||
| 32 | return response()->json($output); |
||
|
|
|||
| 33 | } |
||
| 34 | $allow_extension = ['jpg','png','jpeg']; |
||
| 35 | if($isValid && in_array($extension,$allow_extension)){ |
||
| 36 | $path = $request->file('avatar')->store('/static/img/avatar','NOJPublic'); |
||
| 37 | |||
| 38 | $user = Auth::user(); |
||
| 39 | $old_path = $user->avatar; |
||
| 40 | if($old_path != '/static/img/avatar/default.png'){ |
||
| 41 | Storage::disk('NOJPublic')->delete($old_path); |
||
| 42 | } |
||
| 43 | |||
| 44 | $user->avatar = '/'.$path; |
||
| 45 | $user->save(); |
||
| 46 | |||
| 47 | $output=[ |
||
| 48 | 'ret' => '200', |
||
| 49 | 'desc' => 'success', |
||
| 50 | 'data' => [ |
||
| 51 | 'url' => '/'.$path |
||
| 52 | ] |
||
| 53 | ]; |
||
| 54 | return response()->json($output); |
||
| 55 | }else{ |
||
| 56 | $output=[ |
||
| 57 | 'ret' => '400', |
||
| 58 | 'desc' => 'Invalid file', |
||
| 59 | 'data' => null |
||
| 60 | ]; |
||
| 61 | return response()->json($output); |
||
| 62 | } |
||
| 66 |