]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/ImageController.php
Got image uploads working
[bookstack] / app / Http / Controllers / ImageController.php
1 <?php
2
3 namespace Oxbow\Http\Controllers;
4
5 use Illuminate\Filesystem\Filesystem as File;
6 use Illuminate\Http\Request;
7
8 use Oxbow\Http\Requests;
9 use Oxbow\Image;
10
11 class ImageController extends Controller
12 {
13     protected $image;
14     protected $file;
15
16     /**
17      * ImageController constructor.
18      * @param Image $image
19      * @param File $file
20      */
21     public function __construct(Image $image, File $file)
22     {
23         $this->image = $image;
24         $this->file = $file;
25     }
26
27     /**
28      * Returns an image from behind the public-facing application.
29      * @param Request $request
30      * @return \Illuminate\Http\Response
31      */
32     public function getImage(Request $request)
33     {
34         $cacheTime = 60*60*24;
35         $path = storage_path() . '/' . $request->path();
36         $modifiedTime = $this->file->lastModified($path);
37         $eTag = md5($modifiedTime . $path);
38         $headerLastModified = gmdate('r', $modifiedTime);
39         $headerExpires = gmdate('r', $modifiedTime + $cacheTime);
40
41         $headers = [
42             'Last-Modified' => $headerLastModified,
43             'Cache-Control' => 'must-revalidate',
44             'Pragma' => 'public',
45             'Expires' => $headerExpires,
46             'Etag' => $eTag
47         ];
48
49         $browserModifiedSince = $request->header('If-Modified-Since');
50         $browserNoneMatch = $request->header('If-None-Match');
51         if($browserModifiedSince !== null && file_exists($path) && ($browserModifiedSince == $headerLastModified || $browserNoneMatch == $eTag)) {
52             return response()->make('', 304, $headers);
53         }
54
55         if(file_exists($path)) {
56             return response()->make(file_get_contents($path), 200, array_merge($headers, [
57                 'Content-Type' => $this->file->mimeType($path),
58                 'Content-Length' => filesize($path),
59             ]));
60         }
61         abort(404);
62     }
63
64     /**
65      * Handles image uploads for use on pages.
66      * @param Request $request
67      * @return \Illuminate\Http\JsonResponse
68      */
69     public function upload(Request $request)
70     {
71         $imageUpload = $request->file('file');
72         $name = $imageUpload->getClientOriginalName();
73         $imagePath = '/images/' . Date('Y-m-M') . '/';
74         $storagePath = storage_path(). $imagePath;
75         $fullPath = $storagePath . $name;
76         while(file_exists($fullPath)) {
77             $name = substr(sha1(rand()), 0, 3) . $name;
78             $fullPath = $storagePath . $name;
79         }
80         $imageUpload->move($storagePath, $name);
81         // Create and save image object
82         $this->image->name = $name;
83         $this->image->url = $imagePath . $name;
84         $this->image->save();
85         return response()->json(['link' => $this->image->url]);
86     }
87
88
89 }