<?php

namespace app\controller;

use app\model\File;
use app\validate\Upload as VUpload;
use think\facade\Config;
use think\facade\Filesystem;
use think\facade\Lang;
use think\response\Json;

/**
 * 文件上传
 *
 * Class Upload
 * @package app\controller\api\file
 */
class Upload extends Base
{
    protected $noNeedLogin = [];

    // 图片上传是否进行压缩[max-width:1920px]
    private bool $isCompress = false;
    private $validate = null;
    // 文件上传对外默认保存目录(相对路径)
    private string $uploadPath = '';
    private $videoUploadPath;
    // 文件上传对外默认保存目录是否有写权限
    private bool $uploadPathIsWritable = false;
    private $videoUploadPathIsWritable = 0;
    protected bool $saveToOos = false;

    public function initialize()
    {
        parent::initialize();

//        $system = System::getSystem();
//        if (!empty($system)) {
//            $this->isCompress = $system['compress'] ?? true;
//        }
        $this->validate        = new VUpload();
        $this->uploadPath      = Config::get('filesystem.disks.local.url');
        $this->videoUploadPath = Config::get('filesystem.disks.video.url');

        if (!is_writable(app()->getRootPath().'public'.$this->uploadPath)) {
            mkdir(app()->getRootPath().'public'.$this->uploadPath, 0777, true);
        }
        $this->uploadPathIsWritable = 1;

        if (!is_writable(app()->getRootPath().'public'.$this->videoUploadPath)) {
            mkdir(app()->getRootPath().'public'.$this->videoUploadPath, 0777, true);
        }

        $this->videoUploadPathIsWritable = 1;
    }


    /**
     * 通用文件上传
     * @return Json
     */
    public function file()
    {
        $file = request()->file('file');
        if (empty($file)) {
            return $this->json(4001, '请上传的文件');
        }

        if ($this->validate->checkFile($file)) {
            try {
                if (!$this->uploadPathIsWritable) {
                    throw new \Exception('上传文件夹需要写入权限');
                }
                $src            = Filesystem::putFile('files/'.date('Ym'), $file, 'uniqid');
                $src            = $this->uploadPath.'/'.$src;
                $return['src']  = $src;
                $return['name'] = $file->getOriginalName();

                //加入上传文件表
                File::add($file, $src, $file->md5());
            } catch (\Exception $e) {
                return $this->json(4003, $e->getMessage());
            }

            return $this->json(0, 'success', $return);
        } else {

            $errorMsg = Lang::get($this->validate->getError());
            return $this->json(4002, $errorMsg);
        }
    }

    /**
     * 通用图片上传
     * @return Json
     */
    public function image()
    {
        $image = request()->file('image');
        if (empty($image)) {
            return $this->json(4001, '请上传图片文件');
        }

        if (!in_array($image->getMime(), ['image/png', 'image/jpeg', 'image/bmp'])) {
            return $this->json(4001, '请上传png|jpeg|jpg|bmp格式图片');
        }

        if ($image->getSize() > 1.5 * 1024 * 1024) {
            return $this->json(4001, '图片超出尺寸要求');
        }

        try {
            if (!$this->uploadPathIsWritable) {
                throw new \Exception('上传文件夹需要写入权限');
            }
            $src                = Filesystem::putFile('images/'.date('Ym'), $image, 'uniqid');
            $src                = $this->uploadPath.'/'.$src;
            $return['src']      = $src;
        } catch (\Exception $e) {
            return $this->json(4003, $e->getMessage());
        }

        return $this->json(0, 'success', $return);
    }

    /**
     * 通用视频上传
     * @return Json
     */
    public function video()
    {
        try {
            $file = request()->file('video');
            $md5  = $file->md5();//文件md5
            if ($this->validate->checkVideo($file)) {
                if (!$this->videoUploadPathIsWritable) {
                    throw new \Exception('上传文件夹需要写入权限');
                }

                $src                = Filesystem::putFile('videos/'.date('Ym'), $file, 'uniqid');
                $src                = $this->uploadPath.'/'.$src;
                $return['src']      = $src;
                $return['full_src'] = resourceJoin($src);

                //加入上传文件表
                File::add($file, $src, $md5, 'video');

                return $this->json(0, 'success', $return);
            } else {
                $errorMsg = Lang::get($this->validate->getError());
                return $this->json(4002, $errorMsg);
            }
        } catch (\Exception $e) {
            return $this->json(4000, $e->getMessage());
        }

    }
}