<?php

namespace app\admin\controller;


use support\Storage;

class UploadFileController
{
    /**
     * @Desc 上传文件
     * @Author Gorden
     * @Date 2024/2/26 13:22
     *
     * @return \support\Response
     */
    public function upload($config)
    {
        try {
            $res = Storage::uploadFile($config);
            $data = [
                'url' => $res[0]['url'],
                'path' => $config['uri'] . date('Ymd') . '/' . $res[0]['save_name'],
                'size' => $res[0]['size'],
                'mime_type' => $res[0]['mime_type'],
            ];
        } catch (\Exception $e) {
            return json_fail('上传失败:' . $e->getMessage());
        }

        return json_success('上传成功', $data);
    }

    /**
     * @Desc 上传图片
     * @Author Gorden
     * @Date 2024/2/26 17:16
     *
     * @return \support\Response
     */
    public function image()
    {
        $config = [
            'single_limit' => 1024 * 1024 * 5,
            'nums' => 1,
            'include' => ['jpg', 'jpeg', 'png'],
            'uri' => '/storage/images/',
            'root' => public_path() . '/storage/images/',
        ];

        return $this->upload($config);
    }

    /**
     * @Desc 上传视频
     * @Author Gorden
     * @Date 2024/2/26 17:16
     *
     * @return \support\Response
     */
    public function video()
    {
        $config = [
            'single_limit' => 1024 * 1024 * 50,
            'nums' => 1,
            'include' => ['mp4'],
            'uri' => '/storage/videos/',
            'root' => public_path() . '/storage/videos/',
        ];

        return $this->upload($config);
    }
}