Skip to content

Commit 60528e2

Browse files
author
Jovert Lota Palonpon
committedApr 2, 2019
wip
1 parent 9eb2419 commit 60528e2

File tree

9 files changed

+102
-58
lines changed

9 files changed

+102
-58
lines changed
 

Diff for: ‎app/Contracts/Uploader.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace App\Contracts;
4+
5+
interface Uploader
6+
{
7+
/**
8+
* Get the directory.
9+
*
10+
* @return string
11+
*/
12+
public function getDirectory() : string;
13+
}

Diff for: ‎app/Http/Controllers/Api/V1/UsersController.php

+13
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,19 @@ public function restore(Request $request, $id)
127127
return response()->json($this->paginatedQuery($request));
128128
}
129129

130+
/**
131+
* Store the user's avatar.
132+
*
133+
* @param Illuminate\Http\Request $request
134+
* @param App\User $user
135+
*
136+
* @return Illuminate\Http\JsonResponse
137+
*/
138+
public function storeAvatar(Request $request, User $user) : JsonResponse
139+
{
140+
return response()->json();
141+
}
142+
130143
/**
131144
* Get the paginated resource query.
132145
*

Diff for: ‎app/Traits/UploadsFiles.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Traits;
4+
5+
use Uploader;
6+
use Illuminate\Http\UploadedFile;
7+
8+
trait UploadsFiles
9+
{
10+
/**
11+
* Handle the upload for the uploader, and update it's attributes.
12+
*
13+
* @param Illuminate\Http\UploadedFile
14+
*
15+
* @return bool
16+
*/
17+
public function upload(UploadedFile $file)
18+
{
19+
$upload = Uploader::upload($this->getDirectory(), $file);
20+
21+
foreach ($upload as $attribute => $value) {
22+
$this->attributes[$attribute] = $value;
23+
}
24+
25+
return $this->update();
26+
}
27+
}

Diff for: ‎app/User.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
namespace App;
44

55
use App\Traits\HasJWT;
6+
use App\Contracts\Uploader;
7+
use App\Traits\UploadsFiles;
68
use Tymon\JWTAuth\Contracts\JWTSubject;
79
use Illuminate\Notifications\Notifiable;
810
use Illuminate\Database\Eloquent\SoftDeletes;
911
use Illuminate\Contracts\Auth\MustVerifyEmail;
1012
use Illuminate\Foundation\Auth\User as Authenticatable;
1113

12-
class User extends Authenticatable implements JWTSubject
14+
class User extends Authenticatable implements JWTSubject, Uploader
1315
{
14-
use Notifiable, SoftDeletes, HasJWT;
16+
use Notifiable, SoftDeletes, HasJWT, UploadsFiles;
1517

1618
/**
1719
* The attributes that are mass assignable.
@@ -28,4 +30,14 @@ class User extends Authenticatable implements JWTSubject
2830
protected $hidden = [
2931
'password', 'remember_token',
3032
];
33+
34+
/**
35+
* Get the directory for uploads.
36+
*
37+
* @return string
38+
*/
39+
public function getDirectory() : string
40+
{
41+
return 'users/'.$this->getKey();
42+
}
3143
}

Diff for: ‎app/Utils/Facades/Uploader.php

-18
This file was deleted.

Diff for: ‎app/Utils/Uploader.php

+14-36
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,6 @@
99

1010
class Uploader
1111
{
12-
/**
13-
* @var string The storage driver.
14-
*/
15-
protected $disk;
16-
17-
public function __construct()
18-
{
19-
$this->disk = config('filesystems.default');
20-
}
21-
22-
/**
23-
* Specify which storage driver will be used.
24-
*
25-
* @param string $name
26-
*
27-
* @return App\Utils\Uploader
28-
*/
29-
public function disk(string $name = 'public')
30-
{
31-
$this->disk = $name;
32-
33-
return $this;
34-
}
35-
3612
/**
3713
* Put the file into the storage.
3814
*
@@ -41,38 +17,40 @@ public function disk(string $name = 'public')
4117
*
4218
* @return array
4319
*/
44-
public function put(string $directory, UploadedFile $file)
20+
public static function upload(string $directory, UploadedFile $file)
4521
{
22+
$disk = config('filesystems.default');
4623
$filename = str_random(64).'.'.$file->getClientOriginalExtension();
4724
$original_filename = $file->getClientOriginalName();
4825

49-
$path = Storage::disk($this->disk)->putFileAs(
50-
$directory,
51-
$file,
52-
$filename
53-
);
26+
// Upload the file
27+
$path = Storage::putFileAs($directory, $file, $filename);
28+
$url = Storage::url($path);
29+
$thumbnail_url = null;
5430

31+
// Do image manipulations
5532
if (Str::startsWith($file->getClientMimeType(), 'image')) {
5633
$thumbnailDirectory = "{$directory}/thumbnails";
34+
$thumbnailPath = "{$thumbnailDirectory}/{$filename}";
5735

5836
if (! Storage::exists($thumbnailDirectory)) {
5937
Storage::makeDirectory($thumbnailDirectory);
6038
}
6139

62-
$fileSystemRoot = config("filesystems.disks.{$this->disk}.root");
40+
$fileSystemRoot = config("filesystems.disks.{$disk}.root");
6341
$fullPath = "{$fileSystemRoot}/{$path}";
64-
$thumbnailPath =
42+
$fullThumbnailPath =
6543
"{$fileSystemRoot}/{$thumbnailDirectory}/{$filename}";
6644

6745
Image::make($fullPath)
6846
->fit(240)
69-
->save($thumbnailPath, 95);
70-
}
47+
->save($fullThumbnailPath, 95);
7148

72-
$path = Storage::url($path);
49+
$thumbnail_url = Storage::url($thumbnailPath);
50+
}
7351

7452
return compact([
75-
'directory', 'filename', 'original_filename', 'path'
53+
'directory', 'filename', 'original_filename', 'url', 'thumbnail_url'
7654
]);
7755
}
7856
}

Diff for: ‎config/app.php

-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@
199199

200200
'aliases' => [
201201
'Image' => Intervention\Image\Facades\Image::class,
202-
'Uploader' => App\Utils\Facades\Uploader::class,
203202

204203
'App' => Illuminate\Support\Facades\App::class,
205204
'Artisan' => Illuminate\Support\Facades\Artisan::class,

Diff for: ‎routes/api.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@
3131

3232
Route::middleware('auth:api')->group(function () {
3333
Route::resource('users', 'UsersController', ['except' => ['edit', 'create']]);
34-
Route::patch('users/{user}/restore', 'UsersController@restore')->name('users.restore');
34+
Route::prefix('users')->name('users.')->group(function () {
35+
Route::patch('{user}/restore', 'UsersController@restore')->name('restore');
36+
37+
Route::prefix('{user}/avatar')->name('avatar.')->group(function () {
38+
Route::post('/', 'UsersController@storeAvatar')->name('store');
39+
Route::delete('/', 'UsersController@destroyAvatar')->name('destroy');
40+
});
41+
});
3542
});
3643
});
3744
});

Diff for: ‎tests/Feature/Api/V1/UsersTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,17 @@ public function a_user_can_restore_a_user()
109109
'total' => $incremented
110110
]);
111111
}
112+
113+
/** @test */
114+
public function a_user_can_store_a_user_avatar()
115+
{
116+
$user = User::first();
117+
118+
$payload = array_merge($this->getDefaultPayload(), [
119+
'user' => $user
120+
]);
121+
122+
$this->post(route('api.v1.users.avatar.store', $payload))
123+
->assertStatus(200);
124+
}
112125
}

0 commit comments

Comments
 (0)
Please sign in to comment.