CloudChunkUploadTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <?php
  2. declare(strict_types=1);
  3. use OSS\OssClient;
  4. use Tinywan\Storage\Adapter\CosAdapter;
  5. use Tinywan\Storage\Adapter\OssAdapter;
  6. use Tinywan\Storage\Adapter\QiniuAdapter;
  7. use Tinywan\Storage\Adapter\S3Adapter;
  8. use Tinywan\Storage\Exception\StorageException;
  9. use Webman\Http\UploadFile;
  10. /*
  11. * 云端分片测试替身:全部以 fake 客户端替换 getInstance(),
  12. * 记录调用序列、返回预设响应,不产生任何真实网络请求。
  13. */
  14. class FakeOssClient extends OssClient
  15. {
  16. /** @var array<int, array{0: string, 1: array}> */
  17. public $calls = [];
  18. public function initiateMultipartUpload($bucket, $object, $options = null)
  19. {
  20. $this->calls[] = ['initiateMultipartUpload', [$bucket, $object]];
  21. return 'fake-upload-id';
  22. }
  23. public function uploadPart($bucket, $object, $uploadId, $options = null)
  24. {
  25. $this->calls[] = ['uploadPart', [$bucket, $object, $uploadId, $options]];
  26. return 'etag-' . $options[OssClient::OSS_PART_NUM];
  27. }
  28. public function completeMultipartUpload($bucket, $object, $uploadId, $listParts, $options = null)
  29. {
  30. $this->calls[] = ['completeMultipartUpload', [$bucket, $object, $uploadId, $listParts]];
  31. return null;
  32. }
  33. public function copyObject($fromBucket, $fromObject, $toBucket, $toObject, $options = null)
  34. {
  35. $this->calls[] = ['copyObject', [$fromBucket, $fromObject, $toBucket, $toObject]];
  36. return null;
  37. }
  38. public function deleteObject($bucket, $object, $options = null)
  39. {
  40. $this->calls[] = ['deleteObject', [$bucket, $object]];
  41. return null;
  42. }
  43. /**
  44. * @return string[]
  45. */
  46. public function callNames(): array
  47. {
  48. $names = [];
  49. foreach ($this->calls as $call) {
  50. $names[] = (string) $call[0];
  51. }
  52. return $names;
  53. }
  54. }
  55. class FakeCosClient extends \Qcloud\Cos\Client
  56. {
  57. /** @var array<int, array{0: string, 1: array}> */
  58. public $calls = [];
  59. /**
  60. * 测试替身:返回真实 Result 对象(实现 ArrayAccess),适配器按数组方式访问
  61. */
  62. public function __call($method, array $args)
  63. {
  64. $this->calls[] = [$method, $args];
  65. if ('createMultipartUpload' === $method) {
  66. return new \GuzzleHttp\Command\Result(['UploadId' => 'fake-upload-id']);
  67. }
  68. if ('uploadPart' === $method) {
  69. return new \GuzzleHttp\Command\Result(['ETag' => 'etag-' . $args[0]['PartNumber']]);
  70. }
  71. return new \GuzzleHttp\Command\Result([]);
  72. }
  73. /**
  74. * @return string[]
  75. */
  76. public function callNames(): array
  77. {
  78. $names = [];
  79. foreach ($this->calls as $call) {
  80. $names[] = (string) $call[0];
  81. }
  82. return $names;
  83. }
  84. }
  85. class FakeS3Client extends \Aws\S3\S3Client
  86. {
  87. /** @var array<int, array{0: string, 1: array}> */
  88. public $calls = [];
  89. public function __construct(array $args = [])
  90. {
  91. // 测试替身:跳过父类构造(SDK 服务解析/凭证校验),所有命令由 __call 拦截
  92. }
  93. /**
  94. * 测试替身:返回真实 Result 对象(实现 ArrayAccess),适配器按数组方式访问
  95. */
  96. public function __call($name, array $args)
  97. {
  98. $this->calls[] = [$name, $args];
  99. if ('createMultipartUpload' === $name) {
  100. return new \Aws\Result(['UploadId' => 'fake-upload-id']);
  101. }
  102. if ('uploadPart' === $name) {
  103. return new \Aws\Result(['ETag' => 'etag-' . $args[0]['PartNumber']]);
  104. }
  105. return new \Aws\Result([]);
  106. }
  107. /**
  108. * @return string[]
  109. */
  110. public function callNames(): array
  111. {
  112. $names = [];
  113. foreach ($this->calls as $call) {
  114. $names[] = (string) $call[0];
  115. }
  116. return $names;
  117. }
  118. }
  119. function makeCloudChunkRoot(): string
  120. {
  121. return sys_get_temp_dir() . '/storage_cloud_' . uniqid() . '/';
  122. }
  123. function makeCloudChunkFile(string $content): UploadFile
  124. {
  125. $path = tempnam(sys_get_temp_dir(), 'storage_cloud_chunk_');
  126. file_put_contents($path, $content);
  127. return new UploadFile($path, 'chunk.part', 'application/octet-stream', UPLOAD_ERR_OK);
  128. }
  129. class OssChunkTestAdapter extends OssAdapter
  130. {
  131. /** @var FakeOssClient|null */
  132. public $fakeClient;
  133. public function getInstance(): OssClient
  134. {
  135. if (is_null($this->fakeClient)) {
  136. $this->fakeClient = new FakeOssClient('fake-ak', 'fake-sk', 'oss-cn-hangzhou.aliyuncs.com');
  137. }
  138. return $this->fakeClient;
  139. }
  140. }
  141. class CosChunkTestAdapter extends CosAdapter
  142. {
  143. /** @var FakeCosClient|null */
  144. public $fakeClient;
  145. public function getInstance(): \Qcloud\Cos\Client
  146. {
  147. if (is_null($this->fakeClient)) {
  148. $this->fakeClient = new FakeCosClient([
  149. 'region' => 'ap-shanghai',
  150. 'credentials' => ['secretId' => 'fake-ak', 'secretKey' => 'fake-sk'],
  151. ]);
  152. }
  153. return $this->fakeClient;
  154. }
  155. }
  156. class S3ChunkTestAdapter extends S3Adapter
  157. {
  158. /** @var FakeS3Client|null */
  159. public $fakeClient;
  160. public function getInstance(): \Aws\S3\S3Client
  161. {
  162. if (is_null($this->fakeClient)) {
  163. $this->fakeClient = new FakeS3Client([
  164. 'version' => 'latest',
  165. 'region' => 'us-east-1',
  166. 'credentials' => ['key' => 'fake-ak', 'secret' => 'fake-sk'],
  167. ]);
  168. }
  169. return $this->fakeClient;
  170. }
  171. }
  172. class QiniuChunkTestAdapter extends QiniuAdapter
  173. {
  174. /** @var array<int, array{0: string, 1: string}> */
  175. public $uploads = [];
  176. protected function uploadMergedFile(string $object, string $file_path): void
  177. {
  178. $this->uploads[] = [$object, (string) file_get_contents($file_path)];
  179. }
  180. }
  181. function makeOssChunkAdapter(string $root, array $overrides = []): OssChunkTestAdapter
  182. {
  183. return new OssChunkTestAdapter(array_merge([
  184. 'adapter' => OssAdapter::class,
  185. 'accessKeyId' => 'fake-ak',
  186. 'accessKeySecret' => 'fake-sk',
  187. 'bucket' => 'test-bucket',
  188. 'dirname' => 'storage',
  189. 'domain' => 'http://cdn.example.com',
  190. 'endpoint' => 'oss-cn-hangzhou.aliyuncs.com',
  191. 'algo' => 'sha1',
  192. 'chunk_path' => $root . '.chunks',
  193. '_is_file_upload' => false,
  194. ], $overrides));
  195. }
  196. function makeCosChunkAdapter(string $root, array $overrides = []): CosChunkTestAdapter
  197. {
  198. return new CosChunkTestAdapter(array_merge([
  199. 'adapter' => CosAdapter::class,
  200. 'secretId' => 'fake-ak',
  201. 'secretKey' => 'fake-sk',
  202. 'bucket' => 'test-bucket',
  203. 'dirname' => 'storage',
  204. 'domain' => 'http://cdn.example.com',
  205. 'region' => 'ap-shanghai',
  206. 'chunk_path' => $root . '.chunks',
  207. '_is_file_upload' => false,
  208. ], $overrides));
  209. }
  210. function makeS3ChunkAdapter(string $root, array $overrides = []): S3ChunkTestAdapter
  211. {
  212. return new S3ChunkTestAdapter(array_merge([
  213. 'adapter' => S3Adapter::class,
  214. 'key' => 'fake-ak',
  215. 'secret' => 'fake-sk',
  216. 'bucket' => 'test-bucket',
  217. 'dirname' => 'storage',
  218. 'domain' => 'http://cdn.example.com',
  219. 'region' => 'us-east-1',
  220. 'version' => 'latest',
  221. 'use_path_style_endpoint' => true,
  222. 'endpoint' => 'http://s3.example.com',
  223. 'acl' => 'public-read',
  224. 'chunk_path' => $root . '.chunks',
  225. '_is_file_upload' => false,
  226. ], $overrides));
  227. }
  228. function makeQiniuChunkAdapter(string $root, array $overrides = []): QiniuChunkTestAdapter
  229. {
  230. return new QiniuChunkTestAdapter(array_merge([
  231. 'adapter' => QiniuAdapter::class,
  232. 'accessKey' => 'fake-ak',
  233. 'secretKey' => 'fake-sk',
  234. 'bucket' => 'test-bucket',
  235. 'dirname' => 'storage',
  236. 'domain' => 'http://cdn.example.com',
  237. 'chunk_path' => $root . '.chunks',
  238. '_is_file_upload' => false,
  239. ], $overrides));
  240. }
  241. test('OSS 首片上传初始化会话并记录 ETag', function () {
  242. $root = makeCloudChunkRoot();
  243. $adapter = makeOssChunkAdapter($root);
  244. $result = $adapter->uploadChunk(makeCloudChunkFile('aaa'), 'demo-file', 0, md5('aaa'));
  245. /** @var FakeOssClient $client */
  246. $client = $adapter->fakeClient;
  247. expect($client->callNames())->toBe(['initiateMultipartUpload', 'uploadPart']);
  248. expect($client->calls[1][1][3][OssClient::OSS_PART_NUM])->toBe(1);
  249. expect($result['save_path'])->toBe('storage/demo-file');
  250. expect($result['md5'])->toBe(md5('aaa'));
  251. $session = json_decode((string) file_get_contents($root . '.chunks/demo-file/session.json'), true);
  252. expect($session['upload_id'])->toBe('fake-upload-id');
  253. expect($session['object'])->toBe('storage/demo-file');
  254. expect($session['parts'][0]['etag'])->toBe('etag-1');
  255. });
  256. test('OSS 续传复用会话不重复初始化', function () {
  257. $adapter = makeOssChunkAdapter(makeCloudChunkRoot());
  258. $adapter->uploadChunk(makeCloudChunkFile('aaa'), 'demo-file', 0, md5('aaa'));
  259. $adapter->uploadChunk(makeCloudChunkFile('ccc'), 'demo-file', 2, md5('ccc'));
  260. expect(array_count_values($adapter->fakeClient->callNames())['initiateMultipartUpload'])->toBe(1);
  261. $progress = $adapter->chunkProgress('demo-file', 4);
  262. expect($progress['uploaded'])->toBe([0, 2])->and($progress['missing'])->toBe([1, 3]);
  263. });
  264. test('OSS 合并成功后 copy 改名并清理会话', function () {
  265. $root = makeCloudChunkRoot();
  266. $adapter = makeOssChunkAdapter($root);
  267. $adapter->uploadChunk(makeCloudChunkFile('aaa'), 'demo-file', 0, md5('aaa'));
  268. $adapter->uploadChunk(makeCloudChunkFile('bbb'), 'demo-file', 1, md5('bbb'));
  269. $result = $adapter->mergeChunk('demo-file', 2, 'video.mp4', ['file_hash' => 'abc123']);
  270. /** @var FakeOssClient $client */
  271. $client = $adapter->fakeClient;
  272. $completeArgs = $client->calls[3][1];
  273. expect($completeArgs[3])->toBe([
  274. ['PartNumber' => 1, 'ETag' => 'etag-1'],
  275. ['PartNumber' => 2, 'ETag' => 'etag-2'],
  276. ]);
  277. expect($client->calls[4])->toBe([
  278. 'copyObject',
  279. ['test-bucket', 'storage/demo-file', 'test-bucket', 'storage/abc123.mp4'],
  280. ]);
  281. expect($client->calls[5])->toBe(['deleteObject', ['test-bucket', 'storage/demo-file']]);
  282. expect($result['save_path'])->toBe('storage/abc123.mp4');
  283. expect($result['url'])->toBe('http://cdn.example.com/storage/abc123.mp4');
  284. expect($result['unique_id'])->toBe('abc123');
  285. expect($result['size'])->toBe(6);
  286. expect($result['extension'])->toBe('mp4');
  287. expect(is_dir($root . '.chunks/demo-file'))->toBeFalse();
  288. });
  289. test('OSS 未传 file_hash 时使用 file_id 命名', function () {
  290. $adapter = makeOssChunkAdapter(makeCloudChunkRoot());
  291. $adapter->uploadChunk(makeCloudChunkFile('aaa'), 'demo-file', 0, md5('aaa'));
  292. $result = $adapter->mergeChunk('demo-file', 1, 'video.mp4');
  293. expect($result['save_path'])->toBe('storage/demo-file.mp4');
  294. expect($result['unique_id'])->toBe('demo-file');
  295. });
  296. test('OSS 缺片合并抛异常且不发起 complete', function () {
  297. $adapter = makeOssChunkAdapter(makeCloudChunkRoot());
  298. $adapter->uploadChunk(makeCloudChunkFile('aaa'), 'demo-file', 0, md5('aaa'));
  299. expect(fn() => $adapter->mergeChunk('demo-file', 3, 'demo.txt'))->toThrow(StorageException::class, '缺少分片:1,2');
  300. expect($adapter->fakeClient->callNames())->not->toContain('completeMultipartUpload');
  301. });
  302. test('云端合并校验后缀黑名单', function () {
  303. $adapter = makeOssChunkAdapter(makeCloudChunkRoot(), ['exclude' => ['php']]);
  304. $adapter->uploadChunk(makeCloudChunkFile('aaa'), 'demo-file', 0, md5('aaa'));
  305. expect(fn() => $adapter->mergeChunk('demo-file', 1, 'shell.php'))
  306. ->toThrow(StorageException::class, '文件扩展名不合法');
  307. });
  308. test('云端合并锁存在时抛异常', function () {
  309. $root = makeCloudChunkRoot();
  310. $adapter = makeOssChunkAdapter($root);
  311. $adapter->uploadChunk(makeCloudChunkFile('aaa'), 'demo-file', 0, md5('aaa'));
  312. file_put_contents($root . '.chunks/demo-file.lock', 'lock');
  313. expect(fn() => $adapter->mergeChunk('demo-file', 1, 'demo.txt'))->toThrow(StorageException::class, '正在合并中');
  314. });
  315. test('COS 分片全流程经 S3 风格命令完成', function () {
  316. $root = makeCloudChunkRoot();
  317. $adapter = makeCosChunkAdapter($root);
  318. $adapter->uploadChunk(makeCloudChunkFile('aaa'), 'demo-file', 0, md5('aaa'));
  319. $adapter->uploadChunk(makeCloudChunkFile('bbb'), 'demo-file', 1, md5('bbb'));
  320. $result = $adapter->mergeChunk('demo-file', 2, 'video.mp4', ['file_hash' => 'abc123']);
  321. /** @var FakeCosClient $client */
  322. $client = $adapter->fakeClient;
  323. expect($client->callNames())->toBe([
  324. 'createMultipartUpload',
  325. 'uploadPart',
  326. 'uploadPart',
  327. 'completeMultipartUpload',
  328. 'copyObject',
  329. 'deleteObject',
  330. ]);
  331. $completeArgs = $client->calls[3][1][0];
  332. expect($completeArgs['Parts'])->toBe([
  333. ['PartNumber' => 1, 'ETag' => 'etag-1'],
  334. ['PartNumber' => 2, 'ETag' => 'etag-2'],
  335. ]);
  336. $copyArgs = $client->calls[4][1][0];
  337. expect($copyArgs['Key'])->toBe('storage/abc123.mp4');
  338. expect($copyArgs['CopySource'])->toBe('test-bucket.cos.ap-shanghai.myqcloud.com/storage/demo-file');
  339. expect($result['url'])->toBe('http://cdn.example.com/storage/abc123.mp4');
  340. expect($result['size'])->toBe(6);
  341. expect(is_dir($root . '.chunks/demo-file'))->toBeFalse();
  342. });
  343. test('S3 分片全流程经 AWS 命令完成', function () {
  344. $root = makeCloudChunkRoot();
  345. $adapter = makeS3ChunkAdapter($root);
  346. $adapter->uploadChunk(makeCloudChunkFile('aaa'), 'demo-file', 0, md5('aaa'));
  347. $adapter->uploadChunk(makeCloudChunkFile('bbb'), 'demo-file', 1, md5('bbb'));
  348. $result = $adapter->mergeChunk('demo-file', 2, 'video.mp4');
  349. /** @var FakeS3Client $client */
  350. $client = $adapter->fakeClient;
  351. expect($client->callNames())->toBe([
  352. 'createMultipartUpload',
  353. 'uploadPart',
  354. 'uploadPart',
  355. 'completeMultipartUpload',
  356. 'copyObject',
  357. 'deleteObject',
  358. ]);
  359. $completeArgs = $client->calls[3][1][0];
  360. expect($completeArgs['MultipartUpload']['Parts'])->toBe([
  361. ['PartNumber' => 1, 'ETag' => 'etag-1'],
  362. ['PartNumber' => 2, 'ETag' => 'etag-2'],
  363. ]);
  364. $copyArgs = $client->calls[4][1][0];
  365. expect($copyArgs['Key'])->toBe('storage/demo-file.mp4');
  366. expect($copyArgs['CopySource'])->toBe('test-bucket/storage/demo-file');
  367. expect($copyArgs['ACL'])->toBe('public-read');
  368. expect($result['unique_id'])->toBe('demo-file');
  369. expect(is_dir($root . '.chunks/demo-file'))->toBeFalse();
  370. });
  371. test('七牛分片本地暂存后合并经 putFile 上传', function () {
  372. $root = makeCloudChunkRoot();
  373. $adapter = makeQiniuChunkAdapter($root);
  374. $adapter->uploadChunk(makeCloudChunkFile('aaa'), 'demo-file', 0, md5('aaa'));
  375. $adapter->uploadChunk(makeCloudChunkFile('bbb'), 'demo-file', 1, md5('bbb'));
  376. $progress = $adapter->chunkProgress('demo-file', 2);
  377. expect($progress['finished'])->toBeTrue();
  378. $result = $adapter->mergeChunk('demo-file', 2, 'video.mp4', ['file_hash' => 'abc123']);
  379. expect($adapter->uploads)->toBe([['storage/abc123.mp4', 'aaabbb']]);
  380. expect($result['save_path'])->toBe('storage/abc123.mp4');
  381. expect($result['url'])->toBe('http://cdn.example.com/storage/abc123.mp4');
  382. expect($result['size'])->toBe(6);
  383. expect(is_dir($root . '.chunks/demo-file'))->toBeFalse();
  384. });