forked from summerblue/larabbs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b40334
commit 25b65ca
Showing
8 changed files
with
189 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api; | ||
|
||
use App\Models\Image; | ||
use Illuminate\Http\Request; | ||
use App\Handlers\ImageUploadHandler; | ||
use App\Transformers\ImageTransformer; | ||
use App\Http\Requests\Api\ImageRequest; | ||
|
||
class ImagesController extends Controller | ||
{ | ||
public function store(ImageRequest $request, ImageUploadHandler $uploader, Image $image) | ||
{ | ||
$user = $this->user(); | ||
|
||
$size = $request->type == 'avatar' ? 362 : 1024; | ||
$result = $uploader->save($request->image, str_plural($request->type), $user->id, $size); | ||
|
||
$image->path = $result['path']; | ||
$image->type = $request->type; | ||
$image->user_id = $user->id; | ||
$image->save(); | ||
|
||
return $this->response->item($image, new ImageTransformer())->setStatusCode(201); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\Api; | ||
|
||
class ImageRequest extends FormRequest | ||
{ | ||
public function rules() | ||
{ | ||
|
||
$rules = [ | ||
'type' => 'required|string|in:avatar,topic', | ||
]; | ||
|
||
if ($this->type == 'avatar') { | ||
$rules['image'] = 'required|mimes:jpeg,bmp,png,gif|dimensions:min_width=200,min_height=200'; | ||
} else { | ||
$rules['image'] = 'required|mimes:jpeg,bmp,png,gif'; | ||
} | ||
|
||
return $rules; | ||
} | ||
|
||
public function messages() | ||
{ | ||
return [ | ||
'image.dimensions' => '图片的清晰度不够,宽和高需要 200px 以上', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Image extends Model | ||
{ | ||
protected $fillable = ['type', 'path']; | ||
|
||
public function user() | ||
{ | ||
return $this->belongsTo(User::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace App\Transformers; | ||
|
||
use App\Models\Image; | ||
use League\Fractal\TransformerAbstract; | ||
|
||
class ImageTransformer extends TransformerAbstract | ||
{ | ||
public function transform(Image $image) | ||
{ | ||
return [ | ||
'id' => $image->id, | ||
'user_id' => $image->user_id, | ||
'type' => $image->type, | ||
'path' => $image->path, | ||
'created_at' => (string) $image->created_at, | ||
'updated_at' => (string) $image->updated_at, | ||
]; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
database/migrations/2019_05_11_220518_create_images_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class CreateImagesTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('images', function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
$table->integer('user_id')->index(); | ||
$table->string('type')->index(); | ||
$table->string('path'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('images'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters