forked from coding/coding-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoding.php
More file actions
167 lines (155 loc) · 6.15 KB
/
Copy pathCoding.php
File metadata and controls
167 lines (155 loc) · 6.15 KB
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
159
160
161
162
163
164
165
166
167
<?php
namespace App;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class Coding
{
private Client $client;
private \ZipArchive $zipArchive;
public function __construct(Client $client = null, \ZipArchive $zipArchive = null)
{
$this->client = $client ?? new Client();
$this->zipArchive = $zipArchive ?? new \ZipArchive();
}
public function createWiki($token, $projectName, $data)
{
$response = $this->client->request('POST', 'https://e.coding.net/open-api', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => "token ${token}",
'Content-Type' => 'application/json'
],
'json' => array_merge([
'Action' => 'CreateWiki',
'ProjectName' => $projectName,
], $data),
]);
return json_decode($response->getBody(), true)['Response']['Data'];
}
public function createUploadToken($token, $projectName, $fileName)
{
$response = $this->client->request('POST', 'https://e.coding.net/open-api', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => "token ${token}",
'Content-Type' => 'application/json'
],
'json' => [
'Action' => 'CreateUploadToken',
'ProjectName' => $projectName,
'FileName' => $fileName,
],
]);
$uploadToken = json_decode($response->getBody(), true)['Response']['Token'];
preg_match_all(
'|https://([a-z0-9\-]+)-(\d+)\.cos\.([a-z0-9\-]+)\.myqcloud\.com|',
$uploadToken['UploadLink'],
$matches
);
$uploadToken['Bucket'] = $matches[1][0] . '-' . $matches[2][0];
$uploadToken['AppId'] = $matches[2][0];
$uploadToken['Region'] = $matches[3][0];
return $uploadToken;
}
public function createMarkdownZip($markdown, $path, $markdownFilename): bool|string
{
$zipFileFullPath = sys_get_temp_dir() . '/' . $markdownFilename . '-' . Str::uuid() . '.zip';
if ($this->zipArchive->open($zipFileFullPath, \ZipArchive::CREATE) !== true) {
Log::error("cannot open <$zipFileFullPath>");
return false;
}
$this->zipArchive->addFromString($markdownFilename, $markdown);
preg_match_all('/!\[\]\((.+)\)/', $markdown, $matches);
if (!empty($matches)) {
foreach ($matches[1] as $attachment) {
$this->zipArchive->addFile($path . $attachment, $attachment);
}
}
$this->zipArchive->close();
return $zipFileFullPath;
}
public function upload(array $uploadToken, string $fileFullPath): bool
{
config(['filesystems.disks.cos.credentials.appId' => $uploadToken['AppId']]);
config(['filesystems.disks.cos.credentials.secretId' => $uploadToken['SecretId']]);
config(['filesystems.disks.cos.credentials.secretKey' => $uploadToken['SecretKey']]);
config(['filesystems.disks.cos.credentials.token' => $uploadToken['UpToken']]);
config(['filesystems.disks.cos.region' => $uploadToken['Region']]);
config(['filesystems.disks.cos.bucket' => $uploadToken['Bucket']]);
$disk = Storage::build(config('filesystems.disks.cos'));
return $disk->put($uploadToken['StorageKey'], File::get($fileFullPath));
}
public function createWikiByZip(string $token, string $projectName, array $uploadToken, array $data)
{
$response = $this->client->request('POST', 'https://e.coding.net/open-api', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => "token ${token}",
'Content-Type' => 'application/json'
],
'json' => [
'Action' => 'CreateWikiByZip',
'ProjectName' => $projectName,
'ParentIid' => $data['ParentIid'],
'FileName' => $data['FileName'],
'Key' => $uploadToken['StorageKey'],
'Time' => $uploadToken['Time'],
'AuthToken' => $uploadToken['AuthToken'],
],
]);
$result = json_decode($response->getBody(), true);
if (isset($result['Response']['JobId'])) {
return $result['Response'];
} else {
return new \Exception('createWikiByZip failed');
}
}
/**
* 获取 Wiki 导入任务的进度(API 文档未展示,其实此接口已上线)
*
* @param string $token
* @param string $projectName
* @param string $jobId
* @return mixed
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getImportJobStatus(string $token, string $projectName, string $jobId)
{
$response = $this->client->request('POST', 'https://e.coding.net/open-api', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => "token ${token}",
'Content-Type' => 'application/json'
],
'json' => [
'Action' => 'DescribeImportJobStatus',
'ProjectName' => $projectName,
'JobId' => $jobId,
],
]);
$result = json_decode($response->getBody(), true);
if (isset($result['Response']['Data']['Status'])) {
return $result['Response']['Data']['Status'];
} else {
// TODO exception message
return new \Exception('failed');
}
}
public function createWikiByUploadZip(string $token, string $projectName, string $zipFileFullPath)
{
$zipFilename = basename($zipFileFullPath);
$uploadToken = $this->createUploadToken(
$token,
$projectName,
$zipFilename
);
$this->upload($uploadToken, $zipFileFullPath);
return $this->createWikiByZip($token, $projectName, $uploadToken, [
'ParentIid' => 0,
'FileName' => $zipFilename,
]);
}
}