11<?php
22
3- namespace App ;
3+ namespace App \ Coding ;
44
55use Exception ;
6- use GuzzleHttp \Client ;
7- use Illuminate \Support \Facades \File ;
86use Illuminate \Support \Facades \Log ;
9- use Illuminate \Support \Facades \Storage ;
107use Illuminate \Support \Str ;
118use ZipArchive ;
129
13- class Coding
10+ class Wiki extends Base
1411{
15- private Client $ client ;
16- private ZipArchive $ zipArchive ;
17-
18- public function __construct (Client $ client = null , ZipArchive $ zipArchive = null )
19- {
20- $ this ->client = $ client ?? new Client ();
21- $ this ->zipArchive = $ zipArchive ?? new ZipArchive ();
22- }
23-
2412 public function createWiki ($ token , $ projectName , $ data )
2513 {
2614 $ response = $ this ->client ->request ('POST ' , 'https://e.coding.net/open-api ' , [
@@ -37,32 +25,6 @@ public function createWiki($token, $projectName, $data)
3725 return json_decode ($ response ->getBody (), true )['Response ' ]['Data ' ];
3826 }
3927
40- public function createUploadToken ($ token , $ projectName , $ fileName )
41- {
42- $ response = $ this ->client ->request ('POST ' , 'https://e.coding.net/open-api ' , [
43- 'headers ' => [
44- 'Accept ' => 'application/json ' ,
45- 'Authorization ' => "token $ {token}" ,
46- 'Content-Type ' => 'application/json '
47- ],
48- 'json ' => [
49- 'Action ' => 'CreateUploadToken ' ,
50- 'ProjectName ' => $ projectName ,
51- 'FileName ' => $ fileName ,
52- ],
53- ]);
54- $ uploadToken = json_decode ($ response ->getBody (), true )['Response ' ]['Token ' ];
55- preg_match_all (
56- '|https://([a-z0-9\-]+)-(\d+)\.cos\.([a-z0-9\-]+)\.myqcloud\.com| ' ,
57- $ uploadToken ['UploadLink ' ],
58- $ matches
59- );
60- $ uploadToken ['Bucket ' ] = $ matches [1 ][0 ] . '- ' . $ matches [2 ][0 ];
61- $ uploadToken ['AppId ' ] = $ matches [2 ][0 ];
62- $ uploadToken ['Region ' ] = $ matches [3 ][0 ];
63- return $ uploadToken ;
64- }
65-
6628 public function createMarkdownZip ($ markdown , $ path , $ markdownFilename ): bool |string
6729 {
6830 $ zipFileFullPath = sys_get_temp_dir () . '/ ' . $ markdownFilename . '- ' . Str::uuid () . '.zip ' ;
@@ -84,19 +46,6 @@ public function createMarkdownZip($markdown, $path, $markdownFilename): bool|str
8446 return $ zipFileFullPath ;
8547 }
8648
87- public function upload (array $ uploadToken , string $ fileFullPath ): bool
88- {
89- config (['filesystems.disks.cos.credentials.appId ' => $ uploadToken ['AppId ' ]]);
90- config (['filesystems.disks.cos.credentials.secretId ' => $ uploadToken ['SecretId ' ]]);
91- config (['filesystems.disks.cos.credentials.secretKey ' => $ uploadToken ['SecretKey ' ]]);
92- config (['filesystems.disks.cos.credentials.token ' => $ uploadToken ['UpToken ' ]]);
93- config (['filesystems.disks.cos.region ' => $ uploadToken ['Region ' ]]);
94- config (['filesystems.disks.cos.bucket ' => $ uploadToken ['Bucket ' ]]);
95-
96- $ disk = Storage::build (config ('filesystems.disks.cos ' ));
97- return $ disk ->put ($ uploadToken ['StorageKey ' ], File::get ($ fileFullPath ));
98- }
99-
10049 public function createWikiByZip (string $ token , string $ projectName , array $ uploadToken , array $ data )
10150 {
10251 $ response = $ this ->client ->request ('POST ' , 'https://e.coding.net/open-api ' , [
@@ -202,57 +151,20 @@ public function updateWikiTitle(string $token, string $projectName, int $id, str
202151 return $ result ['Response ' ]['Data ' ]['Title ' ] == $ title ;
203152 }
204153
205- /**
206- * 创建网盘目录,不可重名,如已存在,仍然正常返回 id
207- *
208- * @param string $token
209- * @param string $projectName
210- * @param string $folderName
211- * @param int $parentId
212- * @return int
213- * @throws \GuzzleHttp\Exception\GuzzleException
214- */
215- public function createFolder (string $ token , string $ projectName , string $ folderName , int $ parentId ): int
154+ public function replaceAttachments (string $ markdown , array $ codingAttachments ): string
216155 {
217- $ response = $ this ->client ->request ('POST ' , 'https://e.coding.net/open-api ' , [
218- 'headers ' => [
219- 'Accept ' => 'application/json ' ,
220- 'Authorization ' => "token $ {token}" ,
221- 'Content-Type ' => 'application/json '
222- ],
223- 'json ' => [
224- 'Action ' => 'CreateFolder ' ,
225- 'ProjectName ' => $ projectName ,
226- 'FolderName ' => $ folderName ,
227- 'ParentId ' => $ parentId ,
228- ],
229- ]);
230- $ result = json_decode ($ response ->getBody (), true );
231- return $ result ['Response ' ]['Data ' ]['Id ' ];
232- }
233-
234- /**
235- * @param string $token
236- * @param string $projectName
237- * @param array $data
238- * @return int
239- * @throws \GuzzleHttp\Exception\GuzzleException
240- * @todo data 数组无法强类型校验内部字段,考虑用对象
241- */
242- public function createFile (string $ token , string $ projectName , array $ data ): array
243- {
244- $ response = $ this ->client ->request ('POST ' , 'https://e.coding.net/open-api ' , [
245- 'headers ' => [
246- 'Accept ' => 'application/json ' ,
247- 'Authorization ' => "token $ {token}" ,
248- 'Content-Type ' => 'application/json '
249- ],
250- 'json ' => array_merge ([
251- 'Action ' => 'CreateFile ' ,
252- 'ProjectName ' => $ projectName ,
253- ], $ data ),
254- ]);
255- $ result = json_decode ($ response ->getBody (), true );
256- return $ result ['Response ' ]['Data ' ];
156+ if (empty ($ codingAttachments )) {
157+ return $ markdown ;
158+ }
159+ $ markdown .= "\n\nAttachments \n--- \n" ;
160+ foreach ($ codingAttachments as $ attachmentPath => $ codingAttachment ) {
161+ $ markdown .= "\n- # $ {codingAttachment['ResourceCode ' ]} $ {codingAttachment['FileName ' ]}" ;
162+ $ markdown = preg_replace (
163+ "|\[.*\]\( $ {attachmentPath}\)| " ,
164+ " # $ {codingAttachment['ResourceCode ' ]} ` $ {codingAttachment['FileName ' ]}` " ,
165+ $ markdown
166+ );
167+ }
168+ return $ markdown ;
257169 }
258170}
0 commit comments