Storage.php
3.9 KB
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
147
148
149
150
151
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +---------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace cmf\lib;
class Storage
{
private $driver;
/**
* @var object 对象实例
*/
protected static $instance;
/**
* 构造方法,用于构造存储实例
* @param string $driver 要使用的存储驱动 Qiniu-七牛存储驱动
* @param array $driverConfig
* @throws \Exception
*/
public function __construct($driver = null, $driverConfig = null)
{
if (empty($driver)) {
$storageSetting = cmf_get_option('storage');
if (empty($storageSetting)) {
$driver = 'Local';
$driverConfig = [];
} else {
$driver = isset($storageSetting['type']) ? $storageSetting['type'] : 'Local';
if ($driver == 'Local') {
$driverConfig = [];
} else {
$driverConfig = $storageSetting['storages'][$driver];
}
}
}
if (empty($driverConfig['driver'])) {
$storageDriverClass = "\\cmf\\lib\\storage\\$driver";
} else {
$storageDriverClass = $driverConfig['driver'];
}
$storage = new $storageDriverClass($driverConfig);
$this->driver = $storage;
}
/**
* 文件上传
* @param string $file 上传文件路径
* @param string $filePath 文件路径相对于upload目录
* @param string $fileType 文件类型,image,video,audio,file
* @param array $param 额外参数
* @return mixed
*/
public function upload($file, $filePath, $fileType = 'image', $param = null)
{
return $this->driver->upload($file, $filePath, $fileType, $param);
}
/**
* 初始化
* @param $type
* @param $config
* @return \cmf\lib\Storage
*/
public static function instance($type = null, $config = null)
{
if (is_null(self::$instance)) {
self::$instance = new static($type, $config);
}
return self::$instance;
}
/**
* 获取图片预览地址
* @param string $file
* @param string $style
* @return mixed
*/
public function getPreviewUrl($file, $style = '')
{
return $this->driver->getPreviewUrl($file, $style);
}
/**
* 获取图片地址
* @param string $file
* @param string $style
* @return mixed
*/
public function getImageUrl($file, $style = '')
{
return $this->driver->getImageUrl($file, $style);
}
/**
* 获取文件地址
* @param string $file
* @param string $style
* @return mixed
*/
public function getUrl($file, $style = '')
{
return $this->driver->getUrl($file, $style);
}
/**
* 获取文件下载地址
* @param string $file
* @param int $expires
* @return mixed
*/
public function getFileDownloadUrl($file, $expires = 3600)
{
return $this->driver->getFileDownloadUrl($file, $expires);
}
/**
* 获取云存储域名
* @return mixed
*/
public function getDomain()
{
return $this->driver->getDomain();
}
/**
* 获取文件相对上传目录路径
* @param string $url
* @return mixed
*/
public function getFilePath($url)
{
return $this->driver->getFilePath($url);
}
}