quanwei
2025-11-28 3ea53e61cc23fdb3ddf8b38a199ca60a6da8c407
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
 
namespace app\common\library\storage;
 
use think\Exception;
 
/**
 * 存储模块驱动
 */
class Driver
{
    private $config;    // upload 配置
    private $engine;    // 当前存储引擎类
    protected $error;
 
    /**
     * 构造方法
     */
    public function __construct($config, $storage = null)
    {
        $this->config = $config;
        // 实例化当前存储引擎
        $this->engine = $this->getEngineClass($storage);
    }
 
    public function validate($name, $fileInfo, $sence = 'image'){
        if($sence == 'image'){
            // 文件校验
            try{
                validate([$name=>[
                    'fileSize' => $this->config['max_image'] * 1024 * 1024, // 默认2M
                    'fileExt' => 'jpg,jpeg,png,gif,bmp',
                    'fileMime' => 'image/jpeg,image/png,image/gif,image/bmp',
                ]],
                    [
                        $name.'.fileSize' => '最大可上传'. $this->config['max_image'] .'M图片',
                        $name.'.fileExt' => '只能上传jpg,jpeg,png,gif,bmp格式图片',
                        $name.'.fileMime' => '只能上传jpg,jpeg,png,gif,bmp格式图片'
                    ]
                )->check([$name => $fileInfo]);
                return true;
            }catch (\Exception $e){
                $this->engine->error = $e->getMessage();
                return false;
            }
        }
        if($sence == 'video'){
            // 文件校验
            try{
                validate([$name=>[
                    'fileSize' => $this->config['max_video'] * 1024 * 1024, // 默认10M
                    'fileExt' => 'mp4',
                    'fileMime' => 'video/mp4',
                ]],
                    [
                        $name.'.fileSize' => '最大可上传'. $this->config['max_video'] .'M视频',
                        $name.'.fileExt' => '只能上传mp4格式视频',
                        $name.'.fileMime' => '只能上传mp4格式视频'
                    ]
                )->check([$name => $fileInfo]);
                return true;
            }catch (\Exception $e){
                $this->engine->error = $e->getMessage();
                return false;
            }
        }
        return false;
    }
    /**
     * 设置上传的文件信息
     */
    public function setUploadFile($name = 'iFile')
    {
        return $this->engine->setUploadFile($name);
    }
 
    /**
     * 设置上传的文件信息
     */
    public function setUploadFileByReal($filePath)
    {
        return $this->engine->setUploadFileByReal($filePath);
    }
 
    /**
     * 执行文件上传
     */
    public function upload()
    {
        return $this->engine->upload();
    }
 
    /**
     * 执行生成缩略图
     */
    public function createThumb($saveName, $is_original = false)
    {
        return $this->engine->createThumb($saveName, $is_original);
    }
 
    /**
     * 执行文件删除
     */
    public function delete($fileName)
    {
        return $this->engine->delete($fileName);
    }
 
    /**
     * 获取错误信息
     */
    public function getError()
    {
        return $this->engine->getError();
    }
 
    /**
     * 获取文件路径
     */
    public function getFileName()
    {
        return $this->engine->getFileName();
    }
 
    /**
     * 返回文件信息
     */
    public function getFileInfo()
    {
        return $this->engine->getFileInfo();
    }
 
    /**
     * 获取当前的存储引擎
     */
    private function getEngineClass($storage = null)
    {
        $engineName = is_null($storage) ? $this->config['default'] : $storage;
        $classSpace = __NAMESPACE__ . '\\engine\\' . ucfirst($engineName);
        if (!class_exists($classSpace)) {
            throw new Exception('未找到存储引擎类: ' . $engineName);
        }
        return new $classSpace($this->config['engine'][$engineName]);
    }
 
}