1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AliMedia\Utils; |
4
|
|
|
|
5
|
|
|
use AliMedia\Conf\Conf; |
6
|
|
|
|
7
|
|
|
class UploadOption |
8
|
|
|
{ |
9
|
|
|
/*optionType用于标识UploadOption的类型,即:普通上传、初始化分片上传、分片上传、分片上传完成*/ |
10
|
|
|
public $optionType; |
11
|
|
|
|
12
|
|
|
/*以下属性是上传时的可选参数。即Rest API中Http请求Body中所需的可选参数*/ |
13
|
|
|
public $dir; // 顽兔空间的图片路径(如果UploadPolicy中不指定dir属性,则生效) |
14
|
|
|
public $name; // 上传到服务端的文件名(如果UploadPolicy中不指定name属性,则生效) |
15
|
|
|
public $metaArray; // 用户自定义的文件meta信息("meta-"为参数前缀, "*"为用户用于渲染的自定义Meta信息名) |
16
|
|
|
public $varArray; // 用户自定义的魔法变量("var-"为参数前缀, "*"为用户用于渲染的自定义魔法变量名) |
17
|
|
|
private $md5; // 文件md5值(推荐提供此参数进行一致性检查) |
18
|
|
|
private $size; // 文件大小 |
|
|
|
|
19
|
|
|
private $content; // 文件内容(在http请求体Body中必须位于参数的最后一位) |
20
|
|
|
|
21
|
|
|
/*以下属性是用户根据自己应用需求,可选的配置*/ |
22
|
|
|
public $blockSize; // 文件分片的大小。针对分片上传。 |
23
|
|
|
public $timeout; // 进行http连接的超时时间 |
24
|
|
|
public $httpReTry; // http失败自动重试。0 or 1 |
25
|
|
|
|
26
|
|
|
/*以下属性是用于分片上传时的参数,仅用于分片上传。用户在调用分片上传时可以选择配置。*/ |
27
|
|
|
private $uploadId; // OSS分片上传ID(OSS用于区分上传的id) |
28
|
|
|
private $uniqueId; // 服务上传唯一ID(多媒体服务用于区分上传的id) |
29
|
|
|
private $partNumber; // 分片文件块上传成功后返回的文件块编号 |
30
|
|
|
private $eTag; // 分片文件块上传成功后返回的Tag标签(由md5和其他标记组成) |
31
|
|
|
private $array_PartNum_ETag; // 分片上传服务端返回的所有 块编号partNumber 和 标记ETag |
32
|
|
|
|
33
|
|
|
public function __construct() |
34
|
|
|
{ |
35
|
|
|
$this->optionType = UpOptionType::COMMON_UPLOAD_TYPE; //默认普通上传类型 |
36
|
|
|
$this->metaArray = array(); |
37
|
|
|
$this->varArray = array(); |
38
|
|
|
$this->blockSize = Conf::BLOCK_DEFF_SIZE; //默认2M |
39
|
|
|
$this->timeout = Conf::HTTP_TIMEOUT; //默认超时30s |
40
|
|
|
$this->httpReTry = Conf::HTTP_RETRY; //默认重试 |
41
|
|
|
$this->array_PartNum_ETag = array(); |
42
|
|
|
} |
43
|
|
|
/**得到上传时http请求体所需的参数*/ |
44
|
|
|
public function getParaArray() |
45
|
|
|
{ |
46
|
|
|
switch ($this->optionType) { |
47
|
|
|
case UpOptionType::COMMON_UPLOAD_TYPE: |
48
|
|
|
case UpOptionType::BLOCK_INIT_UPLOAD: |
49
|
|
|
return $this->getParasCommonBlockInit(); |
50
|
|
|
case UpOptionType::BLOCK_RUN_UPLOAD: |
51
|
|
|
return $this->getParasBlockRun(); |
52
|
|
|
case UpOptionType::BLOCK_COMPLETE_UPLOAD: |
53
|
|
|
return $this->getParasBlockComplete(); |
54
|
|
|
case UpOptionType::BLOCK_CANCEL_UPLOAD: |
55
|
|
|
return $this->getParasBlockCancel(); |
56
|
|
|
default: |
57
|
|
|
return null; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
/** 构造 普通上传 或者 初始化分片上传 所需的参数 */ |
61
|
|
|
private function getParasCommonBlockInit() |
62
|
|
|
{ |
63
|
|
|
$paraArray = array(); |
64
|
|
|
if (isset($this->dir)) { |
65
|
|
|
$paraArray['dir'] = $this->dir; |
66
|
|
|
} |
67
|
|
|
if (isset($this->name)) { |
68
|
|
|
$paraArray['name'] = $this->name; |
69
|
|
|
} |
70
|
|
|
$paraArray['md5'] = md5($this->content); // 计算文件md5 |
71
|
|
|
$paraArray['size'] = strlen($this->content); // 计算文件大小 |
72
|
|
|
$paraArray['content'] = $this->content; |
73
|
|
|
$this->createMetaVars($paraArray, "meta", $this->metaArray); |
74
|
|
|
$this->createMetaVars($paraArray, "var", $this->varArray); |
75
|
|
|
return $paraArray; |
76
|
|
|
} |
77
|
|
|
/** 构造 分片上传过程中 所需的参数 */ |
78
|
|
|
private function getParasBlockRun() |
79
|
|
|
{ |
80
|
|
|
$paraArray = array(); |
81
|
|
|
$paraArray['uploadId'] = $this->uploadId; |
82
|
|
|
$paraArray['id'] = $this->uniqueId; |
83
|
|
|
$paraArray['partNumber'] = $this->partNumber; |
84
|
|
|
$paraArray['md5'] = md5($this->content); // 计算文件md5 |
85
|
|
|
$paraArray['size'] = strlen($this->content); // 计算文件大小 |
86
|
|
|
$paraArray['content'] = $this->content; |
87
|
|
|
return $paraArray; |
88
|
|
|
} |
89
|
|
|
/** 构造 分片上传完成时 所需的参数 */ |
90
|
|
|
private function getParasBlockComplete() |
91
|
|
|
{ |
92
|
|
|
$paraArray = array(); |
93
|
|
|
$paraArray['uploadId'] = $this->uploadId; |
94
|
|
|
$paraArray['id'] = $this->uniqueId; |
95
|
|
|
$paraArray['md5'] = $this->md5; |
96
|
|
|
$parts = EncodeUtils::encodeWithURLSafeBase64(json_encode($this->array_PartNum_ETag)); |
97
|
|
|
$paraArray['parts'] = $parts; // 所有文件块的编号partNumber 和 标记ETag,需要进行base64的编码 |
98
|
|
|
return $paraArray; |
99
|
|
|
} |
100
|
|
|
/** 构造 分片上传取消 所需的参数 */ |
101
|
|
|
private function getParasBlockCancel() |
102
|
|
|
{ |
103
|
|
|
return array('id' => $this->uniqueId, 'uploadId' => $this->uploadId); |
104
|
|
|
} |
105
|
|
|
/** |
106
|
|
|
* 构建上传http请求体的meta-*和var-*选项参数。将tempArr中的元素加上前缀prefix,然后保存到paraArr中 |
107
|
|
|
* @param array $paraArr 最终的数组 |
108
|
|
|
* @param string $prefix 前缀 |
109
|
|
|
* @param array $tempArr 待添加的数组 |
110
|
|
|
* @return array $paraArr 最终的数组 |
111
|
|
|
*/ |
112
|
|
|
private function createMetaVars($paraArr, $prefix, $tempArr) |
113
|
|
|
{ |
114
|
|
|
foreach ($tempArr as $key => $val) { |
115
|
|
|
$key = $prefix . '-' . $key; |
116
|
|
|
$paraArr[$key] = $val; |
117
|
|
|
} |
118
|
|
|
return $paraArr; |
119
|
|
|
} |
120
|
|
|
/**设置待上传的数据。该方法开发者不需要调用,该方法根据用户数据自动调用 |
121
|
|
|
* @param string $data 字符串 */ |
122
|
|
|
public function setContent($data) |
123
|
|
|
{ |
124
|
|
|
$this->content = $data; |
125
|
|
|
} |
126
|
|
|
/**设置MD5值。该方法主要用于在分片上传完成时调用 |
127
|
|
|
* @param string $value md5值 */ |
128
|
|
|
public function setMd5($value) |
129
|
|
|
{ |
130
|
|
|
$this->md5 = $value; |
131
|
|
|
} |
132
|
|
|
/**得到MD5值。该方法主要用于在分片上传完成时调用*/ |
133
|
|
|
public function getMd5() |
134
|
|
|
{ |
135
|
|
|
return $this->md5; |
136
|
|
|
} |
137
|
|
|
/*下面四个函数均是用于分片上传时的设置,开发者不需要调用*/ |
138
|
|
|
/**分片上传时用于设置uploadId */ |
139
|
|
|
public function setUploadId($uploadId) |
140
|
|
|
{ |
141
|
|
|
$this->uploadId = $uploadId; |
142
|
|
|
} |
143
|
|
|
/**分片上传时用于设置id */ |
144
|
|
|
public function setUniqueIdId($id) |
145
|
|
|
{ |
146
|
|
|
$this->uniqueId = $id; |
147
|
|
|
} |
148
|
|
|
/**分片上传时,用于获取分片上传时的块编号partNumber */ |
149
|
|
|
public function getPartNumber() |
150
|
|
|
{ |
151
|
|
|
return $this->partNumber; |
152
|
|
|
} |
153
|
|
|
/**分片上传时,用于设置分片上传时的块编号partNumber */ |
154
|
|
|
public function setPartNumber($partNumber) |
155
|
|
|
{ |
156
|
|
|
$this->partNumber = $partNumber; |
157
|
|
|
} |
158
|
|
|
/**分片上传过程中,用于保存所有的 块编号partNumber 和 标记ETag*/ |
159
|
|
|
public function addPartNumberAndETag($partNumber, $eTag) |
160
|
|
|
{ |
161
|
|
|
$this->eTag = $eTag; |
162
|
|
|
array_push($this->array_PartNum_ETag, array("partNumber" => $partNumber, "eTag" => $eTag)); |
163
|
|
|
} |
164
|
|
|
/**检测分片上传的参数。即uploadId、uniqueId是否有值*/ |
165
|
|
|
public function checkMutipartParas() |
166
|
|
|
{ |
167
|
|
|
return isset($this->uploadId) && isset($this->uniqueId); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|