Skip to content

Commit 6f55417

Browse files
committed
feat: #13 get import job status
1 parent 5b395e6 commit 6f55417

File tree

4 files changed

+89
-7
lines changed

4 files changed

+89
-7
lines changed

app/Coding.php

+39-7
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace App;
44

55
use GuzzleHttp\Client;
6+
use Illuminate\Support\Facades\File;
67
use Illuminate\Support\Facades\Log;
78
use Illuminate\Support\Facades\Storage;
9+
use Illuminate\Support\Str;
810

911
class Coding
1012
{
@@ -59,16 +61,14 @@ public function createUploadToken($token, $projectName, $fileName)
5961
return $uploadToken;
6062
}
6163

62-
public function createMarkdownZip($markdown, $path, $filename): bool|string
64+
public function createMarkdownZip($markdown, $path, $markdownFilename): bool|string
6365
{
64-
$tmpFile = tempnam(sys_get_temp_dir(), $filename);
65-
$zipFileFullPath = $tmpFile . '.zip';
66-
rename($tmpFile, $zipFileFullPath);
67-
if ($this->zipArchive->open($zipFileFullPath, \ZipArchive::OVERWRITE) !== true) {
66+
$zipFileFullPath = sys_get_temp_dir() . '/' . $markdownFilename . '-' . Str::uuid() . '.zip';
67+
if ($this->zipArchive->open($zipFileFullPath, \ZipArchive::CREATE) !== true) {
6868
Log::error("cannot open <$zipFileFullPath>");
6969
return false;
7070
}
71-
$this->zipArchive->addFromString($filename, $markdown);
71+
$this->zipArchive->addFromString($markdownFilename, $markdown);
7272
preg_match_all('/!\[\]\((.+)\)/', $markdown, $matches);
7373
if (!empty($matches)) {
7474
foreach ($matches[1] as $attachment) {
@@ -88,6 +88,38 @@ public function upload(array $uploadToken, string $fileFullPath): bool
8888
config(['filesystems.disks.cos.region' => $uploadToken['Region']]);
8989
config(['filesystems.disks.cos.bucket' => $uploadToken['Bucket']]);
9090

91-
return Storage::disk('cos')->put(basename($fileFullPath), $fileFullPath);
91+
return Storage::disk('cos')->put(basename($fileFullPath), File::get($fileFullPath));
92+
}
93+
94+
/**
95+
* 获取 Wiki 导入任务的进度(API 文档未展示,其实此接口已上线)
96+
*
97+
* @param string $token
98+
* @param string $projectName
99+
* @param string $jobId
100+
* @return mixed
101+
* @throws \GuzzleHttp\Exception\GuzzleException
102+
*/
103+
public function getImportJobStatus(string $token, string $projectName, string $jobId)
104+
{
105+
$response = $this->client->request('POST', 'https://e.coding.net/open-api', [
106+
'headers' => [
107+
'Accept' => 'application/json',
108+
'Authorization' => "token ${token}",
109+
'Content-Type' => 'application/json'
110+
],
111+
'json' => [
112+
'Action' => 'DescribeImportJobStatus',
113+
'ProjectName' => $projectName,
114+
'JobId' => $jobId,
115+
],
116+
]);
117+
$result = json_decode($response->getBody(), true);
118+
if (isset($result['Response']['Data']['Status'])) {
119+
return $result['Response']['Data']['Status'];
120+
} else {
121+
// TODO exception message
122+
return new \Exception('failed');
123+
}
92124
}
93125
}

tests/Unit/CodingTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,36 @@ public function testUpload()
122122

123123
Storage::disk('cos')->assertExists(basename($file));
124124
}
125+
126+
public function testGetImportJobStatus()
127+
{
128+
$responseBody = file_get_contents($this->dataDir . 'coding/DescribeImportJobStatusResponse.json');
129+
$codingToken = $this->faker->md5;
130+
$codingProjectUri = $this->faker->slug;
131+
$jobId = '123456ad-f123-4ac2-9586-42ebe5d1234d';
132+
133+
$clientMock = $this->getMockBuilder(Client::class)->getMock();
134+
$clientMock->expects($this->once())
135+
->method('request')
136+
->with(
137+
'POST',
138+
'https://e.coding.net/open-api',
139+
[
140+
'headers' => [
141+
'Accept' => 'application/json',
142+
'Authorization' => "token ${codingToken}",
143+
'Content-Type' => 'application/json'
144+
],
145+
'json' => [
146+
'Action' => 'DescribeImportJobStatus',
147+
'ProjectName' => $codingProjectUri,
148+
'JobId' => $jobId,
149+
],
150+
]
151+
)
152+
->willReturn(new Response(200, [], $responseBody));
153+
$coding = new Coding($clientMock);
154+
$result = $coding->getImportJobStatus($codingToken, $codingProjectUri, $jobId);
155+
$this->assertEquals('success', $result);
156+
}
125157
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Response" : {
3+
"Data": {
4+
"JobId": "123456ad-f123-4ac2-9586-42ebe5d1234d",
5+
"Status": "success"
6+
},
7+
"RequestId": "78d6ecf8-9123-574f-8da9-23bb44c467f1"
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Response" : {
3+
"Error" : {
4+
"Code" : "InvalidParameterValue",
5+
"Message" : "资源未找到"
6+
},
7+
"RequestId" : "cca345b2-762e-c257-da19-b98d27a62ab2"
8+
}
9+
}

0 commit comments

Comments
 (0)