Skip to content

Commit 5120502

Browse files
committed
fix: #58 upload to cos failed
1 parent 46c20e7 commit 5120502

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

app/Coding/Disk.php

+16-10
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,22 @@ public function uploadAttachments(string $token, string $projectName, string $da
7272
$projectName,
7373
$filename
7474
);
75-
$this->upload($uploadToken, $dataPath . $path);
76-
$result = $this->createFile($token, $projectName, [
77-
"OriginalFileName" => $filename,
78-
"MimeType" => mime_content_type($dataPath . $path),
79-
"FileSize" => filesize($dataPath . $path),
80-
"StorageKey" => $uploadToken['StorageKey'],
81-
"Time" => $uploadToken['Time'],
82-
"AuthToken" => $uploadToken['AuthToken'],
83-
"FolderId" => $folderId,
84-
]);
75+
$result = [];
76+
try {
77+
$this->upload($uploadToken, $dataPath . $path);
78+
$result = $this->createFile($token, $projectName, [
79+
"OriginalFileName" => $filename,
80+
"MimeType" => mime_content_type($dataPath . $path),
81+
"FileSize" => filesize($dataPath . $path),
82+
"StorageKey" => $uploadToken['StorageKey'],
83+
"Time" => $uploadToken['Time'],
84+
"AuthToken" => $uploadToken['AuthToken'],
85+
"FolderId" => $folderId,
86+
]);
87+
} catch (\Exception $e) {
88+
// TODO laravel log
89+
error_log('ERROR: ' . $e->getMessage());
90+
}
8591
$data[$path] = $result;
8692
}
8793
return $data;

app/Coding/Wiki.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,14 @@ public function replaceAttachments(string $markdown, array $codingAttachments):
188188
if (empty($codingAttachments)) {
189189
return $markdown;
190190
}
191-
$markdown .= "\n\nAttachments\n---\n";
191+
$markdown .= "\n\nAttachments\n---\n\n";
192192
foreach ($codingAttachments as $attachmentPath => $codingAttachment) {
193-
$markdown .= "\n- #${codingAttachment['ResourceCode']} ${codingAttachment['FileName']}";
193+
$resourceCode = $codingAttachment['ResourceCode'] ?? 0;
194+
$filename = $codingAttachment['FileName'] ?? '此文件迁移失败';
195+
$markdown .= "- #${resourceCode} ${filename}\n";
194196
$markdown = preg_replace(
195197
"|\[.*\]\(${attachmentPath}\)|",
196-
" #${codingAttachment['ResourceCode']} `${codingAttachment['FileName']}`",
198+
" #${resourceCode} `${filename}`",
197199
$markdown
198200
);
199201
}

tests/Unit/CodingWikiTest.php

+31-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public function testGetWiki()
243243
$this->assertEquals(json_decode($responseBody, true)['Response']['Data'], $result);
244244
}
245245

246-
public function testupdateTitle()
246+
public function testUpdateTitle()
247247
{
248248
$responseBody = file_get_contents($this->dataDir . 'coding/ModifyWikiTitleResponse.json');
249249
$codingToken = $this->faker->md5;
@@ -276,4 +276,34 @@ public function testupdateTitle()
276276
$result = $coding->updateTitle($codingToken, $codingProjectUri, $id, $title);
277277
$this->assertTrue($result);
278278
}
279+
280+
public function testReplaceAttachments()
281+
{
282+
$codingAttachments = [
283+
'attachments/123/1.pdf' => [
284+
"FileId" => 1234561,
285+
"FileName" => "foo.pdf",
286+
"ResourceCode" => 11,
287+
],
288+
'attachments/123/2.ppt' => [
289+
"FileId" => 1234562,
290+
"FileName" => "bar.ppt",
291+
"ResourceCode" => 12,
292+
],
293+
'attachments/123/3.mp4' => [],
294+
];
295+
$markdown = "hello [foo.pdf](attachments/123/1.pdf)\n"
296+
. "world [bar.ppt](attachments/123/2.ppt) [hello.mp4](attachments/123/3.mp4)";
297+
$expectedMarkdown = "hello #11 `foo.pdf`\n"
298+
. "world #12 `bar.ppt` #0 `此文件迁移失败`\n\n"
299+
. "Attachments\n"
300+
. "---\n\n"
301+
. "- #11 foo.pdf\n"
302+
. "- #12 bar.ppt\n"
303+
. "- #0 此文件迁移失败\n"
304+
;
305+
$wiki = new Wiki();
306+
$newMarkdown = $wiki->replaceAttachments($markdown, $codingAttachments);
307+
$this->assertEquals($expectedMarkdown, $newMarkdown);
308+
}
279309
}

0 commit comments

Comments
 (0)