Skip to content

Commit 440c3e1

Browse files
committed
feat: #13 markdown and images in zip
1 parent 3548ffe commit 440c3e1

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

app/Coding.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
namespace App;
44

55
use GuzzleHttp\Client;
6+
use Illuminate\Support\Facades\Log;
67

78
class Coding
89
{
910
private Client $client;
11+
private \ZipArchive $zipArchive;
1012

11-
public function __construct(Client $client)
13+
public function __construct(Client $client = null, \ZipArchive $zipArchive = null)
1214
{
13-
$this->client = $client;
15+
$this->client = $client ?? new Client();
16+
$this->zipArchive = $zipArchive ?? new \ZipArchive();
1417
}
1518

1619
public function createWiki($token, $projectName, $data)
@@ -45,4 +48,22 @@ public function createUploadToken($token, $projectName, $fileName)
4548
]);
4649
return json_decode($response->getBody(), true)['Response']['Token'];
4750
}
51+
52+
public function createMarkdownZip($markdown, $path, $filename): bool|string
53+
{
54+
$zipFilename = tempnam(sys_get_temp_dir(), $filename);
55+
if ($this->zipArchive->open($zipFilename, \ZipArchive::OVERWRITE) !== true) {
56+
Log::error("cannot open <$zipFilename>");
57+
return false;
58+
}
59+
$this->zipArchive->addFromString($filename, $markdown);
60+
preg_match_all('/!\[\]\((.+)\)/', $markdown, $matches);
61+
if (!empty($matches)) {
62+
foreach ($matches[1] as $attachment) {
63+
$this->zipArchive->addFile($path . $attachment, $attachment);
64+
}
65+
}
66+
$this->zipArchive->close();
67+
return $zipFilename;
68+
}
4869
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"ext-dom": "*",
2121
"ext-json": "*",
2222
"ext-libxml": "*",
23+
"ext-zip": "*",
2324
"laravel-fans/confluence": "^0.1.1",
2425
"laravel-zero/framework": "^8.8",
2526
"league/html-to-markdown": "^5.0"

composer.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/Unit/CodingTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,21 @@ public function testCreateUploadToken()
7575
$result = $coding->createUploadToken($codingToken, $codingProjectUri, $fileName);
7676
$this->assertEquals(json_decode($responseBody, true)['Response']['Token'], $result);
7777
}
78+
79+
public function testCreateMarkdownZip()
80+
{
81+
$path = $this->dataDir . 'confluence/space1/';
82+
$filename = 'image-demo_65619.md';
83+
$markdown = file_get_contents($path . $filename);
84+
$coding = new Coding();
85+
$zipFile = $coding->createMarkdownZip($markdown, $path, $filename);
86+
87+
$this->assertTrue(file_exists($zipFile));
88+
$zip = new \ZipArchive();
89+
$zip->open($zipFile);
90+
$this->assertEquals(3, $zip->numFiles);
91+
$this->assertEquals('image-demo_65619.md', $zip->getNameIndex(0));
92+
$this->assertEquals('attachments/65619/65624.png', $zip->getNameIndex(1));
93+
$this->assertEquals('attachments/65619/65623.png', $zip->getNameIndex(2));
94+
}
7895
}

0 commit comments

Comments
 (0)