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 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) protected function runAction($title, OutputInterface $output, $width, \Closure $closure)
{ {
$output->write($title); $output->write($title);
$length = function_exists('mb_strlen') ? mb_strlen($title) : strlen($title);
// 8 is the length of the label + 2 let it breathe // 8 is the length of the label + 2 let it breathe
$padding = $width - $length - 10; $padding = $width - $this->getLength($title) - 10;
try { try {
$response = $closure(); $response = $closure(function ($content) use ($output, &$padding) {
$padding -= $this->getLength($content);
$output->write($content);
});
} catch (\Exception $e) { } catch (\Exception $e) {
$output->writeln(str_pad(' ', $padding) . '[ <fg=red>FAIL</fg=red> ]'); $output->writeln(str_pad(' ', $padding) . '[ <fg=red>FAIL</fg=red> ]');
throw $e; throw $e;

View File

@ -260,8 +260,9 @@ class Api
/** /**
* @param int $id * @param int $id
* @param array $attachment * @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 // Check if an attachment with
// this name is uploaded // this name is uploaded
@ -277,6 +278,18 @@ class Api
// If the attachment is already uploaded, // If the attachment is already uploaded,
// the update URL is different // the update URL is different
if (count($result['results'])) { 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"; $url .= "/{$result['results'][0]['id']}/data";
} }

View File

@ -289,8 +289,8 @@ class Publisher
foreach ($entry['page']->attachments as $attachment) { foreach ($entry['page']->attachments as $attachment) {
$this->run( $this->run(
" With attachment: $attachment[filename]", " With attachment: $attachment[filename]",
function () use ($published, $attachment) { function ($write) use ($published, $attachment) {
$this->client->uploadAttachment($published['id'], $attachment); $this->client->uploadAttachment($published['id'], $attachment, $write);
} }
); );
} }