Skip to content
This repository has been archived by the owner on May 5, 2019. It is now read-only.

Commit

Permalink
check image existence before storing
Browse files Browse the repository at this point in the history
  • Loading branch information
forehalo committed Jan 12, 2017
1 parent 2705cc7 commit e8722d8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
30 changes: 27 additions & 3 deletions app/Http/Controllers/Api/AssetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,42 @@

class AssetController extends ApiController
{
protected $disk;

public function __construct()
{
// no root permissions.
// $link = public_path('storage');
// if (@linkinfo($link) === -1) {
// symlink(config('filesystems.disks.images.root'), $link);
// }
$this->disk = Storage::disk('images');
}

public function upload(Request $request)
{
$this->validate($request, [
'image' => 'required|image|max:20000'
]);

$image = $request->file('image');

if ($image->isValid()) {
$name = $image->getClientOriginalName();
$image->storeAs('public/images', $name);
return response()->json(['link' => url(Storage::url('images/'.$name))], REST_CREATE_SUCCESS);
// check image existence
if ($this->disk->exists($name)) {
return response()->json([
'message' => trans('app.upload_image_fail'),
'errors' => ['image' => trans('app.existent_image')]
], REST_BAD_REQUEST);
}

$image->storeAs('', $name, 'images');
return response()->json(['link' => url($this->disk->url($name))], REST_CREATE_SUCCESS);
}
return response()->json(['message' => trans('app.upload_image_fail'), 'errors' => ['image' => trans('app.invalid_image')]], REST_BAD_REQUEST);
return response()->json([
'message' => trans('app.upload_image_fail'),
'errors' => ['image' => trans('app.invalid_image')]
], REST_BAD_REQUEST);
}
}
7 changes: 7 additions & 0 deletions config/filesystems.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@
'visibility' => 'public',
],

'images' => [
'driver' => 'local',
'root' => storage_path('app/public/images'),
'visibility' => 'public',
'url' => 'storage/images',
],

's3' => [
'driver' => 's3',
'key' => 'your-key',
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
'unsubscribe_fail' => 'Fail to unsubscribe.',
'upload_image_fail' => 'Fail to upload image.',
'invalid_image' => 'The image uploaded is invalid.',
'existent_image' => 'This image already exists in current storage.',

'declaration' => 'Reserve the right,if reproduced please indicate the source.',
'add_comment' => 'Leave a comment',
Expand Down
1 change: 1 addition & 0 deletions resources/lang/zh-CN/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
'unsubscribe_fail' => '取消提醒失败。',
'upload_image_fail' => '上传图片失败。',
'invalid_image' => '图片无效。',
'existent_image' => '改图片已存在。',

'index' => '首页',
'author' => '作者',
Expand Down

0 comments on commit e8722d8

Please sign in to comment.