3
3
namespace App ;
4
4
5
5
use GuzzleHttp \Client ;
6
+ use Illuminate \Support \Facades \File ;
6
7
use Illuminate \Support \Facades \Log ;
7
8
use Illuminate \Support \Facades \Storage ;
9
+ use Illuminate \Support \Str ;
8
10
9
11
class Coding
10
12
{
@@ -59,16 +61,14 @@ public function createUploadToken($token, $projectName, $fileName)
59
61
return $ uploadToken ;
60
62
}
61
63
62
- public function createMarkdownZip ($ markdown , $ path , $ filename ): bool |string
64
+ public function createMarkdownZip ($ markdown , $ path , $ markdownFilename ): bool |string
63
65
{
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 ) {
68
68
Log::error ("cannot open < $ zipFileFullPath> " );
69
69
return false ;
70
70
}
71
- $ this ->zipArchive ->addFromString ($ filename , $ markdown );
71
+ $ this ->zipArchive ->addFromString ($ markdownFilename , $ markdown );
72
72
preg_match_all ('/!\[\]\((.+)\)/ ' , $ markdown , $ matches );
73
73
if (!empty ($ matches )) {
74
74
foreach ($ matches [1 ] as $ attachment ) {
@@ -88,6 +88,38 @@ public function upload(array $uploadToken, string $fileFullPath): bool
88
88
config (['filesystems.disks.cos.region ' => $ uploadToken ['Region ' ]]);
89
89
config (['filesystems.disks.cos.bucket ' => $ uploadToken ['Bucket ' ]]);
90
90
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
+ }
92
124
}
93
125
}
0 commit comments