3 namespace Oxbow\Http\Controllers;
5 use Illuminate\Filesystem\Filesystem as File;
6 use Illuminate\Http\Request;
8 use Oxbow\Http\Requests;
11 class ImageController extends Controller
17 * ImageController constructor.
21 public function __construct(Image $image, File $file)
23 $this->image = $image;
28 * Returns an image from behind the public-facing application.
29 * @param Request $request
30 * @return \Illuminate\Http\Response
32 public function getImage(Request $request)
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);
42 'Last-Modified' => $headerLastModified,
43 'Cache-Control' => 'must-revalidate',
45 'Expires' => $headerExpires,
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);
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),
65 * Handles image uploads for use on pages.
66 * @param Request $request
67 * @return \Illuminate\Http\JsonResponse
69 public function upload(Request $request)
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;
80 $imageUpload->move($storagePath, $name);
81 // Create and save image object
82 $this->image->name = $name;
83 $this->image->url = $imagePath . $name;
85 return response()->json(['link' => $this->image->url]);