From bbfb879a6402087cb4f4747001b6d000aa7a308b Mon Sep 17 00:00:00 2001 From: sinkcup Date: Fri, 13 Aug 2021 18:43:04 +0800 Subject: [PATCH] feat: #64 log title when image not exist --- app/Coding/Wiki.php | 4 +- app/Commands/WikiImportCommand.php | 2 +- composer.json | 1 + composer.lock | 147 +++++++++++++++++- config/logging.php | 104 +++++++++++++ tests/Unit/CodingWikiTest.php | 20 ++- tests/data/confluence/image-not-exist-demo.md | 1 + 7 files changed, 274 insertions(+), 5 deletions(-) create mode 100644 config/logging.php create mode 100644 tests/data/confluence/image-not-exist-demo.md diff --git a/app/Coding/Wiki.php b/app/Coding/Wiki.php index 7ae80c1..eac9c32 100644 --- a/app/Coding/Wiki.php +++ b/app/Coding/Wiki.php @@ -25,7 +25,7 @@ public function createWiki($token, $projectName, $data) return json_decode($response->getBody(), true)['Response']['Data']; } - public function createMarkdownZip($markdown, $path, $markdownFilename): bool|string + public function createMarkdownZip($markdown, $path, $markdownFilename, $title): bool|string { $zipFileFullPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $markdownFilename . '-' . Str::uuid() . '.zip'; if ($this->zipArchive->open($zipFileFullPath, ZipArchive::CREATE) !== true) { @@ -41,7 +41,7 @@ public function createMarkdownZip($markdown, $path, $markdownFilename): bool|str $filename = $tmp[0]; $filepath = $path . DIRECTORY_SEPARATOR . $filename; if (!file_exists($filepath)) { - error_log("文件不存在:$filename"); + Log::error("文件不存在", ['filename' => $filename, 'title' => $title]); continue; } $this->zipArchive->addFile($filepath, $filename); diff --git a/app/Commands/WikiImportCommand.php b/app/Commands/WikiImportCommand.php index 85143ee..65aa0df 100644 --- a/app/Commands/WikiImportCommand.php +++ b/app/Commands/WikiImportCommand.php @@ -212,7 +212,7 @@ private function uploadConfluencePages(string $dataPath, array $tree, array $tit if ($this->option('save-markdown')) { file_put_contents($dataPath . $mdFilename, $markdown . "\n"); } - $zipFilePath = $this->codingWiki->createMarkdownZip($markdown, $dataPath, $mdFilename); + $zipFilePath = $this->codingWiki->createMarkdownZip($markdown, $dataPath, $mdFilename, $title); $result = $this->codingWiki->createWikiByUploadZip( $this->codingToken, $this->codingProjectUri, diff --git a/composer.json b/composer.json index 8ed4427..02ffd15 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,7 @@ "ext-json": "*", "ext-libxml": "*", "ext-zip": "*", + "illuminate/log": "^8.0", "laravel-fans/confluence": "^0.1.1", "laravel-zero/framework": "^8.8", "league/html-to-markdown": "^5.0", diff --git a/composer.lock b/composer.lock index db3a6a5..48a5dda 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "cbce9cf01075f5140afc235370aa6a80", + "content-hash": "b78aa1fa10887f82b88b1e7290803200", "packages": [ { "name": "brick/math", @@ -1318,6 +1318,55 @@ }, "time": "2021-06-18T15:48:00+00:00" }, + { + "name": "illuminate/log", + "version": "v8.54.0", + "source": { + "type": "git", + "url": "https://github.com/illuminate/log.git", + "reference": "d21261c49095b49124f7ac8391fb6385621bd0c1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/illuminate/log/zipball/d21261c49095b49124f7ac8391fb6385621bd0c1", + "reference": "d21261c49095b49124f7ac8391fb6385621bd0c1", + "shasum": "" + }, + "require": { + "illuminate/contracts": "^8.0", + "illuminate/support": "^8.0", + "monolog/monolog": "^2.0", + "php": "^7.3|^8.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "8.x-dev" + } + }, + "autoload": { + "psr-4": { + "Illuminate\\Log\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + } + ], + "description": "The Illuminate Log package.", + "homepage": "https://laravel.com", + "support": { + "issues": "https://github.com/laravel/framework/issues", + "source": "https://github.com/laravel/framework" + }, + "time": "2021-06-28T14:05:06+00:00" + }, { "name": "illuminate/macroable", "version": "v8.49.2", @@ -2038,6 +2087,102 @@ ], "time": "2021-01-18T20:58:21+00:00" }, + { + "name": "monolog/monolog", + "version": "2.3.2", + "source": { + "type": "git", + "url": "https://github.com/Seldaek/monolog.git", + "reference": "71312564759a7db5b789296369c1a264efc43aad" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Seldaek/monolog/zipball/71312564759a7db5b789296369c1a264efc43aad", + "reference": "71312564759a7db5b789296369c1a264efc43aad", + "shasum": "" + }, + "require": { + "php": ">=7.2", + "psr/log": "^1.0.1" + }, + "provide": { + "psr/log-implementation": "1.0.0" + }, + "require-dev": { + "aws/aws-sdk-php": "^2.4.9 || ^3.0", + "doctrine/couchdb": "~1.0@dev", + "elasticsearch/elasticsearch": "^7", + "graylog2/gelf-php": "^1.4.2", + "mongodb/mongodb": "^1.8", + "php-amqplib/php-amqplib": "~2.4", + "php-console/php-console": "^3.1.3", + "phpspec/prophecy": "^1.6.1", + "phpstan/phpstan": "^0.12.91", + "phpunit/phpunit": "^8.5", + "predis/predis": "^1.1", + "rollbar/rollbar": "^1.3", + "ruflin/elastica": ">=0.90 <7.0.1", + "swiftmailer/swiftmailer": "^5.3|^6.0" + }, + "suggest": { + "aws/aws-sdk-php": "Allow sending log messages to AWS services like DynamoDB", + "doctrine/couchdb": "Allow sending log messages to a CouchDB server", + "elasticsearch/elasticsearch": "Allow sending log messages to an Elasticsearch server via official client", + "ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)", + "ext-mbstring": "Allow to work properly with unicode symbols", + "ext-mongodb": "Allow sending log messages to a MongoDB server (via driver)", + "graylog2/gelf-php": "Allow sending log messages to a GrayLog2 server", + "mongodb/mongodb": "Allow sending log messages to a MongoDB server (via library)", + "php-amqplib/php-amqplib": "Allow sending log messages to an AMQP server using php-amqplib", + "php-console/php-console": "Allow sending log messages to Google Chrome", + "rollbar/rollbar": "Allow sending log messages to Rollbar", + "ruflin/elastica": "Allow sending log messages to an Elastic Search server" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "2.x-dev" + } + }, + "autoload": { + "psr-4": { + "Monolog\\": "src/Monolog" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jordi Boggiano", + "email": "j.boggiano@seld.be", + "homepage": "https://seld.be" + } + ], + "description": "Sends your logs to files, sockets, inboxes, databases and various web services", + "homepage": "https://github.com/Seldaek/monolog", + "keywords": [ + "log", + "logging", + "psr-3" + ], + "support": { + "issues": "https://github.com/Seldaek/monolog/issues", + "source": "https://github.com/Seldaek/monolog/tree/2.3.2" + }, + "funding": [ + { + "url": "https://github.com/Seldaek", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/monolog/monolog", + "type": "tidelift" + } + ], + "time": "2021-07-23T07:42:52+00:00" + }, { "name": "nesbot/carbon", "version": "2.50.0", diff --git a/config/logging.php b/config/logging.php new file mode 100644 index 0000000..658c1c6 --- /dev/null +++ b/config/logging.php @@ -0,0 +1,104 @@ + env('LOG_CHANNEL', 'stack'), + + /* + |-------------------------------------------------------------------------- + | Log Channels + |-------------------------------------------------------------------------- + | + | Here you may configure the log channels for your application. Out of + | the box, Laravel uses the Monolog PHP logging library. This gives + | you a variety of powerful log handlers / formatters to utilize. + | + | Available Drivers: "single", "daily", "slack", "syslog", + | "errorlog", "monolog", + | "custom", "stack" + | + */ + + 'channels' => [ + 'stack' => [ + 'driver' => 'stack', + 'channels' => ['stderr'], + 'ignore_exceptions' => false, + ], + + 'single' => [ + 'driver' => 'single', + 'path' => storage_path('logs/laravel.log'), + 'level' => 'debug', + ], + + 'daily' => [ + 'driver' => 'daily', + 'path' => storage_path('logs/laravel.log'), + 'level' => 'debug', + 'days' => 14, + ], + + 'slack' => [ + 'driver' => 'slack', + 'url' => env('LOG_SLACK_WEBHOOK_URL'), + 'username' => 'Laravel Log', + 'emoji' => ':boom:', + 'level' => 'critical', + ], + + 'papertrail' => [ + 'driver' => 'monolog', + 'level' => 'debug', + 'handler' => SyslogUdpHandler::class, + 'handler_with' => [ + 'host' => env('PAPERTRAIL_URL'), + 'port' => env('PAPERTRAIL_PORT'), + ], + ], + + 'stderr' => [ + 'driver' => 'monolog', + 'handler' => StreamHandler::class, + 'formatter' => env('LOG_STDERR_FORMATTER'), + 'with' => [ + 'stream' => 'php://stderr', + ], + ], + + 'syslog' => [ + 'driver' => 'syslog', + 'level' => 'debug', + ], + + 'errorlog' => [ + 'driver' => 'errorlog', + 'level' => 'debug', + ], + + 'null' => [ + 'driver' => 'monolog', + 'handler' => NullHandler::class, + ], + + 'emergency' => [ + 'path' => storage_path('logs/laravel.log'), + ], + ], + +]; diff --git a/tests/Unit/CodingWikiTest.php b/tests/Unit/CodingWikiTest.php index 8d84d0c..84bc55c 100644 --- a/tests/Unit/CodingWikiTest.php +++ b/tests/Unit/CodingWikiTest.php @@ -4,6 +4,7 @@ use GuzzleHttp\Client; use GuzzleHttp\Psr7\Response; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Storage; use Tests\TestCase; use ZipArchive; @@ -109,7 +110,7 @@ public function testCreateMarkdownZip() $filename = 'image-demo_65619.md'; $markdown = file_get_contents($path . $filename); $coding = new Wiki(); - $zipFile = $coding->createMarkdownZip($markdown, $path, $filename); + $zipFile = $coding->createMarkdownZip($markdown, $path, $filename, 'hello'); $this->assertTrue(file_exists($zipFile)); $zip = new ZipArchive(); @@ -120,6 +121,23 @@ public function testCreateMarkdownZip() $this->assertEquals('attachments/65619/65623.png', $zip->getNameIndex(2)); } + public function testCreateMarkdownZipButImageNotExist() + { + $path = $this->dataDir . 'confluence/'; + $filename = 'image-not-exist-demo.md'; + $markdown = file_get_contents($path . $filename); + $coding = new Wiki(); + Log::shouldReceive('error') + ->with('文件不存在', ['filename' => 'not/exist.png', 'title' => 'hello']); + $zipFile = $coding->createMarkdownZip($markdown, $path, $filename, 'hello'); + + $this->assertTrue(file_exists($zipFile)); + $zip = new ZipArchive(); + $zip->open($zipFile); + $this->assertEquals(1, $zip->numFiles); + $this->assertEquals($filename, $zip->getNameIndex(0)); + } + public function testGetImportJobStatus() { $responseBody = file_get_contents($this->dataDir . 'coding/DescribeImportJobStatusResponse.json'); diff --git a/tests/data/confluence/image-not-exist-demo.md b/tests/data/confluence/image-not-exist-demo.md new file mode 100644 index 0000000..8223afb --- /dev/null +++ b/tests/data/confluence/image-not-exist-demo.md @@ -0,0 +1 @@ + ![](not/exist.png)world