FileController.php 1.5 KB
<?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);
    }
}