Skip to content

Commit c4d19fa

Browse files
committed
Add picture to the question/answer: added support s3
1 parent f7b5366 commit c4d19fa

File tree

8 files changed

+326
-48
lines changed

8 files changed

+326
-48
lines changed

app/Http/Controllers/API/ImageController.php

+22-16
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Illuminate\Support\Facades\Response;
1111
use App\Services\Questions\Contracts\QuestionServiceInterface;
1212
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
13-
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
1413

1514
class ImageController extends Controller
1615
{
@@ -29,31 +28,38 @@ public function __construct(QuestionServiceInterface $questionService)
2928
public function store(Request $request, ImageServiceInterface $imageService)
3029
{
3130
$response_type = $request->get('responseType');
31+
// For CKEditor API
32+
// (http://docs.ckeditor.com/#!/guide/dev_file_browser_api)
33+
$funcNum = $request->get('CKEditorFuncNum');
34+
3235
if (!$request->hasFile('upload')) {
3336
if ($response_type === 'json') {
3437
return Response::json([
3538
'description' => 'File not exists',
3639
], 422);
3740
}
38-
throw new UnprocessableEntityHttpException();
39-
}
41+
$url = '';
42+
$message = 'File invalid. Please choose another file.';
43+
} else {
44+
try {
45+
$fileName = $imageService->save($request->file('upload'));
46+
$url = $imageService->url($fileName);
4047

41-
$fileName = $imageService->save($request->file('upload'));
42-
$url = $imageService->url($fileName);
48+
if ($response_type === 'json') {
49+
return Response::json([
50+
'fileName' => $fileName,
51+
'uploaded' => 1,
52+
'url' => $url
53+
], 200, [], JSON_NUMERIC_CHECK);
54+
}
4355

44-
if ($response_type === 'json') {
45-
return Response::json([
46-
'fileName' => $fileName,
47-
'uploaded' => 1,
48-
'url' => $url
49-
], 200, [], JSON_NUMERIC_CHECK);
56+
$message = 'Image was loading successfully';
57+
} catch (\Exception $e) {
58+
$url = '';
59+
$message = 'File invalid. Please choose another file.';
60+
}
5061
}
5162

52-
// For CKEditor API
53-
// (http://docs.ckeditor.com/#!/guide/dev_file_browser_api)
54-
$funcNum = $request->get('CKEditorFuncNum');
55-
$message = 'Image was loading successfully';
56-
5763
return "<script type='text/javascript'>
5864
window.parent.CKEDITOR.tools.callFunction(
5965
$funcNum,

app/Services/Image/Contracts/ImageServiceInterface.php

+2
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ public function url($filename);
1515
public function generateFilename($extension, $extraName = '');
1616

1717
public function resize($file, $new_name, $width);
18+
19+
public function delete($filename, $isLocal = false);
1820
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace App\Services\Image\Exceptions;
4+
5+
class ImageNotWritableException extends ImageException
6+
{
7+
}

app/Services/Image/ImageService.php

+39-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace App\Services\Image;
44

55
use App\Services\Image\Contracts\ImageServiceInterface;
6+
use App\Services\Image\Exceptions\ImageNotWritableException;
7+
use Illuminate\Support\Facades\Response;
8+
use Illuminate\Support\Facades\Storage;
69
use Symfony\Component\HttpFoundation\File\UploadedFile;
710
use Intervention\Image\Facades\Image;
811
use Illuminate\Support\Facades\File;
@@ -41,14 +44,18 @@ public function url($filename)
4144

4245
public function get($filename)
4346
{
44-
$path = storage_path('app') . config('images.localPath') . $filename;
45-
$exists = File::exists($path);
47+
$path = config('images.localPath') . $filename;
4648

47-
if (!$exists) {
49+
if (!Storage::exists($path)) {
4850
throw new ImageNotFoundHttpException();
4951
}
5052

51-
return Image::make($path)->response(pathinfo($path, PATHINFO_EXTENSION));
53+
$data = Storage::get($path);
54+
$mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $data);
55+
56+
return Response::make($data)
57+
->header('Content-Type', $mime)
58+
->header('Content-Length', strlen($data));
5259
}
5360

5461
public function generateFilename($extension, $extraName = '')
@@ -57,19 +64,42 @@ public function generateFilename($extension, $extraName = '')
5764
return [
5865
'filename' => $fileName . '.' . $extension,
5966
'path' => config('images.localPath') . $fileName . '.' . $extension,
60-
'full_path' => storage_path('app') . config('images.localPath') .
67+
'local_path' => storage_path('app') . config('images.localPath') .
6168
$fileName . '.' . $extension,
6269
'url' => config('images.url') . $fileName . '/' . $extension
6370
];
6471
}
6572

66-
public function resize($file, $new_name, $width)
73+
public function resize($file, $newName, $width)
6774
{
68-
Image::make($file)
75+
$image = Image::make($file)
6976
->resize($width, null, function ($constraint) {
7077
$constraint->upsize();
7178
$constraint->aspectRatio();
72-
})
73-
->save(storage_path('app') . config('images.localPath') . $new_name);
79+
});
80+
81+
$data = $image->encode(pathinfo($newName, PATHINFO_EXTENSION));
82+
83+
$saved = Storage::put(
84+
config('images.localPath') . $newName,
85+
$data->getEncoded()
86+
);
87+
88+
if ($saved === false) {
89+
throw new ImageNotWritableException(
90+
'Can\'t write image data to path ({$path})'
91+
);
92+
}
93+
}
94+
95+
public function delete($filename, $isLocal = false)
96+
{
97+
if ($isLocal) {
98+
File::delete(
99+
storage_path('app') . config('images.localPath') . $filename
100+
);
101+
} else {
102+
Storage::delete(config('images.localPath') . $filename);
103+
}
74104
}
75105
}

composer.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
"tymon/jwt-auth": "^0.5.4",
1818
"smart-crowd/laravel-rbac": "dev-master",
1919
"doctrine/dbal": "^2.5",
20-
"react/zmq": "^0.3.0"
20+
"react/zmq": "^0.3.0",
21+
"league/flysystem-aws-s3-v3": "^1.0",
22+
"aws/aws-sdk-php-laravel": "~3.0"
2123
},
2224
"repositories": [
2325
{

0 commit comments

Comments
 (0)