FileController.php
1.5 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
<?php
namespace App\Http\Controllers\file;
use App\Enums\Common\Code;
use App\Models\File\File;
class FileController
{
public $upload_img = [
//设置静态缓存参数(304)
'header' => [
'Cache-Control' => 'max-age=2592000',
'Pragma' => 'cache',
'Expires' => "%Expires%", // cache 1 month
'etag' => "%etag%",
'Last-Modified' => "%Last-Modified%",
'Content-Description' => 'File Transfer',
],
];
public $path = '';
public $config = '';
public $thr_path = '';
public $request = '';
public function __construct()
{
$this->request = request();
$this->config = config('filesystems.disks.upload');
$this->path = $this->config['root'];
}
public function index($hash = '', $w = 0 ,$h = 0){
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) || isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
header("HTTP/1.1 304 Not Modified");
exit;
}
$file_model = new File();
$info = $file_model->read(['hash'=>$hash]);
if ($info === false) {
$this->response('指定文件不存在!', Code::USER_ERROR);
}
$path = $info['path'];
if (!is_file($path)) {
$this->response('指定文件已被系统删除!', Code::USER_ERROR);
}
$content = file_get_contents($path);
$header['Content-Length'] = $info['size'];
return response($content, 200, $header);
}
}