Don't upload attachments if they are already uploaded and have the same size

This commit is contained in:
Stéphane Goetz 2017-11-06 23:20:42 +01:00
parent 59b8c04161
commit e5ee061ddd
3 changed files with 25 additions and 7 deletions

View File

@ -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) . '[ <fg=red>FAIL</fg=red> ]');
throw $e;

View File

@ -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";
}

View File

@ -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);
}
);
}