Skip to content

Commit

Permalink
Merge pull request #387 from AdelakunShola/feat/update-user-profile
Browse files Browse the repository at this point in the history
feat: update user profile image
  • Loading branch information
Dev-Tonia authored Aug 6, 2024
2 parents 6be653d + 2851fb2 commit 2acc58f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 58 deletions.
94 changes: 45 additions & 49 deletions app/Http/Controllers/Api/V1/User/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use CloudinaryLabs\CloudinaryLaravel\Facades\Cloudinary;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\File;


class ProfileController extends Controller
{
Expand Down Expand Up @@ -99,63 +101,57 @@ public function update(Request $request)

public function uploadImage(Request $request)
{

$validator = Validator::make($request->all(), [
'file' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048'
'file' => 'required|image|mimes:jpeg,png,jpg,gif'
]);

if ($validator->fails()) {
return response()
->json([
'Status' => 400,
return response()->json([
'Status' => 400,
'Message' => $validator->errors()
], 400);
}



$file = $request->file('file');
$imagePath = $file->getRealPath();

try {
$response = Http::post('https://api.cloudinary.com/v1_1/' . env('CLOUDINARY_CLOUD_NAME') . '/image/upload', [
'file' => base64_encode(file_get_contents($imagePath)),
'upload_preset' => env('CLOUDINARY_UPLOAD_PRESET'),
]);

$data = $response->json();

if ($response->successful()) {
$uploadedFileUrl = $data['secure_url'];

$user = Auth::user();
$profile = $user->profile;

if (!$profile) {
return response()
->json([
'Status' => 404,
'Message' => 'Profile not found'
], 404);
}

$profile->update(['avatar_url' => $uploadedFileUrl]);

return response()
->json([
'Status' => 200,
'Message' => 'Image uploaded and profile updated successfully',
'Data' => ['avatar_url' => $uploadedFileUrl]
], 200);
} else {
return response()->json(['Status' => 500, 'Message' => 'Failed to upload image'], 500);
}
} catch (\Exception $e) {
return response()
->json([
'Status' => 500,
'Message' => 'Failed to upload image'
], 500);
}

$fileName = time() . '.' . $file->getClientOriginalExtension();
$filePath = public_path('uploads');


if (!File::exists($filePath)) {
File::makeDirectory($filePath, 0755, true);
}


$file->move($filePath, $fileName);


$imageUrl = url('uploads/' . $fileName);


$user = Auth::user();
$profile = $user->profile;

if (!$profile) {
return response()->json([
'Status' => 404,
'Message' => 'Profile not found'
], 404);
}

$profile->update(['avatar_url' => $imageUrl]);

return response()->json([
'Status' => 200,
'Message' => 'Image uploaded and profile updated successfully',
'Data' => ['avatar_url' => $imageUrl]
], 200);
}






/**
Expand Down
24 changes: 15 additions & 9 deletions tests/Feature/ProfileUpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,27 @@ public function it_can_update_profile()
/** @test */
public function it_can_upload_image()
{
// Mocking Cloudinary API response
Http::fake([
'https://api.cloudinary.com/v1_1/*' => Http::response([
'secure_url' => 'https://example.com/new-avatar.png'
], 200)
]);


$file = UploadedFile::fake()->image('avatar.jpg');

$response = $this->post('/api/v1/profile/upload-image', [
'file' => $file
]);

$response->assertStatus(200);
$response->assertJsonStructure(['Status', 'Message', 'Data' => ['avatar_url']]);

$response->assertJsonStructure([
'Status',
'Message',
'Data' => ['avatar_url']
]);

$uploadedFileUrl = $response->json('Data.avatar_url');

$this->assertFileExists(public_path('uploads/' . basename($uploadedFileUrl)));

$this->assertStringStartsWith(url('uploads/'), $uploadedFileUrl);
}


}

0 comments on commit 2acc58f

Please sign in to comment.