<?php
namespace app\common\util;
class Upload
{
    public function file(): array
    {
        $file = request()->file('file');
        // 上传到本地服务器
        $rename = \think\facade\Filesystem::disk('public')->putFile('topic', $file);
        $rename = str_replace('\\', '/', $rename);
        $url = request()->domain() . getVirRootDir() . '/storage/' . $rename;

        $file_path = app()->getRootPath() . 'public/storage/' . $rename;
        $file_info = mime_content_type($file_path);

        if (str_contains($file_info, 'image')) {
            $attr = getimagesize($file_path);
            $size = filesize($file_path);
            if ($size / 1000 >= 700 || $attr[0] >= 900) {
                $img_compress = new \app\common\util\ImgCompress($file_path, 0.5);
                $img_compress->compressImg($file_path);
            }
        }

        return [
            "file" => $rename,
            "url" => $url,
            "size"=>filesize($file_path)
        ];
    }
}