-
Notifications
You must be signed in to change notification settings - Fork 1
/
ViewController.php
131 lines (108 loc) · 4.61 KB
/
ViewController.php
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
namespace App\Http\Controllers;
use App\Client;
use App\File;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use Laravel\Lumen\Routing\Controller as BaseController;
use Symfony\Component\HttpFoundation\StreamedResponse;
class ViewController extends BaseController
{
/**
* Files view.
*
* @param Request $request
* @param string $client
* @param string $fileName
* @return Response
*/
public function show(Request $request, string $client, string $fileName): Response|StreamedResponse
{
$file = File::findOrFail($client, $fileName);
if ($file->visibility() !== 'public') {
Client::loginOrFail($client, $request->header('x-api-key'));
}
if ($request->hasAny(['w', 'h', 'format']) && $file->isSupported()) {
$ext = ($request->has('format') && $file->conversionSupported($request->input('format')))
? $request->input('format') : pathinfo($fileName, PATHINFO_EXTENSION);
$fileName = pathinfo($fileName, PATHINFO_FILENAME);
$size = ($request->hasAny(['w', 'h']) ? '_' : '') .
($request->has('w') ? 'w_' . $request->input('w') : '') .
($request->has('h') ? 'h_' . $request->input('h') : '');
$cacheName = 'cache' . DIRECTORY_SEPARATOR . $fileName . $size . '.' . $ext;
$cached = File::find($client, $cacheName);
if (!$cached) {
$image = Image::make($file->binary());
if ($request->hasAny(['w', 'h'])) {
$image = $image
->resize($request->input('w'), $request->input('h'), function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
}
$image = $image->encode($ext);
Storage::put($client . DIRECTORY_SEPARATOR . $cacheName, $image);
$cached = File::find($client, $cacheName);
}
$file = $cached;
}
if (preg_match('/^nginx\/(\d+\.)?(\d+\.)?(\*|\d+)$/', $request->server('SERVER_SOFTWARE'))) {
return response(null)
->header('X-Accel-Redirect', "/stream/{$file->path()}")
->header('Content-Type', $file->mimeType());
}
return $file->isStreamable() ? $this->stream($request, $file)
: response($file->binary())
->header('Content-Type', $file->mimeType())
->header('Cache-Control', 'public, max-age=' . env('CACHE_TIME', 15552000)); // default 6 months
}
private function stream(Request $request, File $file): Response|StreamedResponse
{
ob_get_clean();
$buffer = 131072; // 128 KiB = 32 * 4096 bytes (standard disk allocation size)
$streamPointer = $file->stream();
$start = 0;
$size = filesize($file->absolutePath());
$end = $size - 1;
$dynamicHeaders = [];
if ($request->server('HTTP_RANGE')) {
$request_end = $end;
[, $range] = explode('=', $request->server('HTTP_RANGE'), 2);
if ($range !== '-') {
$range = explode('-', $range);
$start = $range[0];
$request_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $request_end;
}
$request_end = ($request_end > $end) ? $end : $request_end;
$end = $request_end;
fseek($streamPointer, $start);
$dynamicHeaders['Content-Length'] = $end - $start + 1;
$dynamicHeaders['Content-Range'] = "bytes {$start}-{$end}/{$size}";
} else {
$dynamicHeaders['Content-Length'] = $size;
}
return response()->stream(
function () use ($streamPointer, $buffer, $end) {
set_time_limit(0);
while (!feof($streamPointer) && ftell($streamPointer) <= $end) {
$bytesToRead = $buffer;
if ((ftell($streamPointer) + $bytesToRead) > $end) {
$bytesToRead = $end - ftell($streamPointer) + 1;
}
echo fread($streamPointer, $bytesToRead);
flush();
}
},
Response::HTTP_PARTIAL_CONTENT,
array_merge(
$dynamicHeaders,
[
'Accept-Ranges' => "0-{$end}",
'Content-Type' => $file->mimeType(),
]
)
);
}
}