-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStreamUploader.php
158 lines (134 loc) · 3.81 KB
/
StreamUploader.php
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
152
153
154
155
156
157
158
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 流式上传处理器
*
* @package S3Upload
* @author jkjoy
* @version 1.1.0
* @date 2025-03-25
*/
class S3Upload_StreamUploader
{
private $chunkSize = 5242880; // 5MB
private $s3Client;
private $options;
public function __construct()
{
$this->s3Client = S3Upload_S3Client::getInstance();
$this->options = Helper::options()->plugin('S3Upload');
}
/**
* 处理文件上传
*
* @param array $file 上传的文件信息
* @return array|bool
*/
public function handleUpload($file)
{
try {
// 验证文件
if (!$this->validateFile($file)) {
throw new Exception('文件验证失败');
}
// 生成基础存储路径(年月/文件名)
$path = $this->s3Client->generatePath($file);
// 构建完整的存储路径(包含自定义路径前缀)
$fullPath = $path;
if (!empty($this->options->customPath)) {
$customPath = trim($this->options->customPath, '/');
$fullPath = $customPath . '/' . $path;
}
// 上传到S3
$result = $this->s3Client->putObject($fullPath, $file['tmp_name']);
// 保存本地备份(如果需要)
if ($this->shouldSaveLocal()) {
$this->saveLocalCopy($file, $path);
}
// 使用S3Client获取正确的URL
$url = $this->s3Client->getObjectUrl($path);
S3Upload_Utils::log("文件上传成功: 路径={$path}, URL={$url}");
return array(
'name' => $file['name'],
'path' => $path,
'size' => $file['size'],
'type' => $this->getFileExt($file['name']),
'mime' => $this->getMimeType($file['tmp_name']),
'url' => $url
);
} catch (Exception $e) {
S3Upload_Utils::log("上传失败: " . $e->getMessage(), 'error');
throw new Exception('文件上传失败: ' . $e->getMessage());
}
}
/**
* 验证文件
*
* @param array $file
* @return bool
*/
private function validateFile($file)
{
if (empty($file['name'])) {
return false;
}
if (!isset($file['tmp_name']) || !is_readable($file['tmp_name'])) {
return false;
}
if (!isset($file['size']) || $file['size'] <= 0) {
return false;
}
return true;
}
/**
* 获取文件扩展名
*
* @param string $filename
* @return string
*/
private function getFileExt($filename)
{
$ext = pathinfo($filename, PATHINFO_EXTENSION);
return $ext ? strtolower($ext) : '';
}
/**
* 获取MIME类型
*
* @param string $file
* @return string
*/
private function getMimeType($file)
{
return S3Upload_Utils::getMimeType($file);
}
/**
* 是否保存本地备份
*
* @return bool
*/
private function shouldSaveLocal()
{
return $this->options->saveLocal === 'true';
}
/**
* 保存本地备份
*
* @param array $file
* @param string $path
* @return bool
*/
private function saveLocalCopy($file, $path)
{
$uploadDir = __TYPECHO_ROOT_DIR__ . '/usr/uploads/';
$localPath = $uploadDir . $path;
$localDir = dirname($localPath);
if (!is_dir($localDir)) {
mkdir($localDir, 0755, true);
}
if (!copy($file['tmp_name'], $localPath)) {
S3Upload_Utils::log("无法保存本地备份: " . $localPath, 'warning');
return false;
}
return true;
}
}