55use Qiniu \Config ;
66use Qiniu \Http \Client ;
77use Qiniu \Http \Error ;
8+ use Qiniu \Enum \SplitUploadVersion ;
89
910/**
1011 * 断点续上传类, 该类主要实现了断点续上传中的分块上传,
@@ -70,9 +71,14 @@ public function __construct(
7071 $ this ->finishedEtags = array ("etags " =>array (), "uploadId " =>"" , "expiredAt " =>0 , "uploaded " =>0 );
7172 $ this ->config = $ config ;
7273 $ this ->resumeRecordFile = $ resumeRecordFile ? $ resumeRecordFile : null ;
73- $ this ->version = $ version ? $ version : 'v1 ' ;
7474 $ this ->partSize = $ partSize ? $ partSize : config::BLOCK_SIZE ;
7575
76+ try {
77+ $ this ->version = SplitUploadVersion::from ($ version ? $ version : 'v1 ' );
78+ } catch (\Exception $ e ) {
79+ throw new \Exception ("only support v1/v2 now! " , 0 , $ e );
80+ }
81+
7682 list ($ accessKey , $ bucket , $ err ) = \Qiniu \explodeUpToken ($ upToken );
7783 $ this ->bucket = $ bucket ;
7884 if ($ err != null ) {
@@ -92,7 +98,7 @@ public function __construct(
9298 public function upload ($ fname )
9399 {
94100 $ uploaded = 0 ;
95- if ($ this ->version == ' v2 ' ) {
101+ if ($ this ->version == SplitUploadVersion:: V2 ) {
96102 $ partNumber = 1 ;
97103 $ encodedObjectName = $ this ->key ? \Qiniu \base64_urlSafeEncode ($ this ->key ) : '~ ' ;
98104 };
@@ -125,13 +131,13 @@ public function upload($fname)
125131 }
126132
127133 if ($ blkputRets ) {
128- if ($ this ->version == ' v1 ' ) {
134+ if ($ this ->version == SplitUploadVersion:: V1 ) {
129135 if (isset ($ blkputRets ['contexts ' ]) && isset ($ blkputRets ['uploaded ' ]) &&
130136 is_array ($ blkputRets ['contexts ' ]) && is_int ($ blkputRets ['uploaded ' ])) {
131137 $ this ->contexts = $ blkputRets ['contexts ' ];
132138 $ uploaded = $ blkputRets ['uploaded ' ];
133139 }
134- } elseif ($ this ->version == ' v2 ' ) {
140+ } elseif ($ this ->version == SplitUploadVersion:: V2 ) {
135141 if (isset ($ blkputRets ["etags " ]) && isset ($ blkputRets ["uploadId " ]) &&
136142 isset ($ blkputRets ["expiredAt " ]) && $ blkputRets ["expiredAt " ] > time ()
137143 && $ blkputRets ["uploaded " ] > 0 && is_array ($ blkputRets ["etags " ]) &&
@@ -149,13 +155,13 @@ public function upload($fname)
149155 throw new \Exception ("only support v1/v2 now! " );
150156 }
151157 } else {
152- if ($ this ->version == ' v2 ' ) {
158+ if ($ this ->version == SplitUploadVersion:: V2 ) {
153159 $ this ->makeInitReq ($ encodedObjectName );
154160 }
155161 }
156162 } else {
157163 // init a Multipart Upload task if choose v2
158- if ($ this ->version == ' v2 ' ) {
164+ if ($ this ->version == SplitUploadVersion:: V2 ) {
159165 $ this ->makeInitReq ($ encodedObjectName );
160166 }
161167 }
@@ -166,10 +172,10 @@ public function upload($fname)
166172 if ($ data === false ) {
167173 throw new \Exception ("file read failed " , 1 );
168174 }
169- if ($ this ->version == ' v1 ' ) {
175+ if ($ this ->version == SplitUploadVersion:: V1 ) {
170176 $ crc = \Qiniu \crc32_data ($ data );
171177 $ response = $ this ->makeBlock ($ data , $ blockSize );
172- } else {
178+ } elseif ( $ this -> version == SplitUploadVersion:: V2 ) {
173179 $ md5 = md5 ($ data );
174180 $ response = $ this ->uploadPart (
175181 $ data ,
@@ -178,6 +184,8 @@ public function upload($fname)
178184 $ encodedObjectName ,
179185 $ md5
180186 );
187+ } else {
188+ throw new \Exception ("only support v1/v2 now! " );
181189 }
182190
183191 $ ret = null ;
@@ -193,7 +201,7 @@ public function upload($fname)
193201 $ this ->host = $ upHostBackup ;
194202 }
195203
196- if ($ this ->version == ' v1 ' ) {
204+ if ($ this ->version == SplitUploadVersion:: V1 ) {
197205 if ($ response ->needRetry () || !isset ($ ret ['crc32 ' ]) || $ crc != $ ret ['crc32 ' ]) {
198206 $ response = $ this ->makeBlock ($ data , $ blockSize );
199207 $ ret = $ response ->json ();
@@ -203,7 +211,7 @@ public function upload($fname)
203211 return array (null , new Error ($ this ->currentUrl , $ response ));
204212 }
205213 array_push ($ this ->contexts , $ ret ['ctx ' ]);
206- } else {
214+ } elseif ( $ this -> version == SplitUploadVersion:: V2 ) {
207215 if ($ response ->needRetry () || !isset ($ ret ['md5 ' ]) || $ md5 != $ ret ['md5 ' ]) {
208216 $ response = $ this ->uploadPart (
209217 $ data ,
@@ -221,22 +229,26 @@ public function upload($fname)
221229 $ blockStatus = array ('etag ' => $ ret ['etag ' ], 'partNumber ' => $ partNumber );
222230 array_push ($ this ->finishedEtags ['etags ' ], $ blockStatus );
223231 $ partNumber += 1 ;
232+ } else {
233+ throw new \Exception ("only support v1/v2 now! " );
224234 }
225235
226236 $ uploaded += $ blockSize ;
227- if ($ this ->version == ' v2 ' ) {
237+ if ($ this ->version == SplitUploadVersion:: V2 ) {
228238 $ this ->finishedEtags ['uploaded ' ] = $ uploaded ;
229239 }
230240
231241 if ($ this ->resumeRecordFile !== null ) {
232- if ($ this ->version == ' v1 ' ) {
242+ if ($ this ->version == SplitUploadVersion:: V1 ) {
233243 $ recordData = array (
234244 'contexts ' => $ this ->contexts ,
235245 'uploaded ' => $ uploaded
236246 );
237247 $ recordData = json_encode ($ recordData );
238- } else {
248+ } elseif ( $ this -> version == SplitUploadVersion:: V2 ) {
239249 $ recordData = json_encode ($ this ->finishedEtags );
250+ } else {
251+ throw new \Exception ("only support v1/v2 now! " );
240252 }
241253 if ($ recordData ) {
242254 $ isWritten = file_put_contents ($ this ->resumeRecordFile , $ recordData );
@@ -248,10 +260,12 @@ public function upload($fname)
248260 }
249261 }
250262 }
251- if ($ this ->version == ' v1 ' ) {
263+ if ($ this ->version == SplitUploadVersion:: V1 ) {
252264 return $ this ->makeFile ($ fname );
253- } else {
265+ } elseif ( $ this -> version == SplitUploadVersion:: V2 ) {
254266 return $ this ->completeParts ($ fname , $ this ->finishedEtags ['uploadId ' ], $ encodedObjectName );
267+ } else {
268+ throw new \Exception ("only support v1/v2 now! " );
255269 }
256270 }
257271
0 commit comments