FilesController.php
2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers;
use URL;
use Storage;
use Response;
use File;
Use PDF;
use Illuminate\Support\Facades\Input;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Facade;
use League\Flysystem\Filesystem;
class FilesController extends Controller
{
/**
* Previews an image if possible.
*
* @param File $file
* @return mixed
* @internal param $id
*/
public function preview(File $file)
{
$path = storage_path('app/') . $file->path . $file->name_thumbnail;
$handler = new File($path);
$lifetime = 31556926; // One year in seconds
/**
* Prepare some header variables
*/
$file_time = $handler->getMTime(); // Get the last modified time for the file (Unix timestamp)
$header_content_type = $handler->getMimeType();
$header_content_length = $handler->getSize();
$header_etag = md5($file_time . $path);
$header_last_modified = gmdate('r', $file_time);
$header_expires = gmdate('r', $file_time + $lifetime);
$headers = array(
'Content-Disposition' => 'inline; filename="' . $file->name_thumbnail . '"',
'Last-Modified' => $header_last_modified,
'Cache-Control' => 'must-revalidate',
'Expires' => $header_expires,
'Pragma' => 'public',
'Etag' => $header_etag
);
/**
* Is the resource cached?
*/
$h1 = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $_SERVER['HTTP_IF_MODIFIED_SINCE'] == $header_last_modified;
$h2 = isset($_SERVER['HTTP_IF_NONE_MATCH']) && str_replace('"', '', stripslashes($_SERVER['HTTP_IF_NONE_MATCH'])) == $header_etag;
if ($h1 || $h2) {
return Response::make('', 304, $headers); // File (image) is cached by the browser, so we don't have to send it again
}
$headers = array_merge($headers, array(
'Content-Type' => $header_content_type,
'Content-Length' => $header_content_length
));
return Response::make(file_get_contents($path), 200, $headers);
}
}