diff --git a/libs/Console/RunAction.php b/libs/Console/RunAction.php index 531e09e..e2fbf9a 100644 --- a/libs/Console/RunAction.php +++ b/libs/Console/RunAction.php @@ -4,16 +4,21 @@ use Symfony\Component\Console\Output\OutputInterface; trait RunAction { + protected function getLength($content) { + return function_exists('mb_strlen') ? mb_strlen($content) : strlen($content); + } + protected function runAction($title, OutputInterface $output, $width, \Closure $closure) { $output->write($title); - $length = function_exists('mb_strlen') ? mb_strlen($title) : strlen($title); - // 8 is the length of the label + 2 let it breathe - $padding = $width - $length - 10; + $padding = $width - $this->getLength($title) - 10; try { - $response = $closure(); + $response = $closure(function ($content) use ($output, &$padding) { + $padding -= $this->getLength($content); + $output->write($content); + }); } catch (\Exception $e) { $output->writeln(str_pad(' ', $padding) . '[ FAIL ]'); throw $e; diff --git a/libs/Format/Confluence/Api.php b/libs/Format/Confluence/Api.php index 43dfc67..29da007 100644 --- a/libs/Format/Confluence/Api.php +++ b/libs/Format/Confluence/Api.php @@ -260,8 +260,9 @@ class Api /** * @param int $id * @param array $attachment + * @param callback $write Write output to the console */ - public function uploadAttachment($id, $attachment) + public function uploadAttachment($id, $attachment, $write) { // Check if an attachment with // this name is uploaded @@ -277,6 +278,18 @@ class Api // If the attachment is already uploaded, // the update URL is different if (count($result['results'])) { + + if (array_key_exists('file', $attachment)) { + $size = filesize($attachment['file']->getPath()); + } else { + $size = function_exists('mb_strlen') ? mb_strlen($attachment['content']) : strlen($attachment['content']); + } + + if ($size == $result['results'][0]['extensions']['fileSize']) { + $write(" ( An attachment of the same size already exists, skipping. )"); + return; + } + $url .= "/{$result['results'][0]['id']}/data"; } diff --git a/libs/Format/Confluence/Publisher.php b/libs/Format/Confluence/Publisher.php index aa41b0d..682a3a8 100644 --- a/libs/Format/Confluence/Publisher.php +++ b/libs/Format/Confluence/Publisher.php @@ -289,8 +289,8 @@ class Publisher foreach ($entry['page']->attachments as $attachment) { $this->run( " With attachment: $attachment[filename]", - function () use ($published, $attachment) { - $this->client->uploadAttachment($published['id'], $attachment); + function ($write) use ($published, $attachment) { + $this->client->uploadAttachment($published['id'], $attachment, $write); } ); }