-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kobo): Sync remote books support
- Loading branch information
Showing
6 changed files
with
159 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DoctrineMigrations; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\Migrations\AbstractMigration; | ||
|
||
/** | ||
* Auto-generated Migration: Please modify to your needs! | ||
*/ | ||
final class Version20241129185216 extends AbstractMigration | ||
{ | ||
public function getDescription(): string | ||
{ | ||
return 'Add upstream_sync on koboDevice'; | ||
} | ||
|
||
public function up(Schema $schema): void | ||
{ | ||
$this->addSql('ALTER TABLE kobo_device ADD upstream_sync TINYINT(1) DEFAULT 0 NOT NULL'); | ||
} | ||
|
||
public function down(Schema $schema): void | ||
{ | ||
$this->addSql('ALTER TABLE kobo_device DROP upstream_sync'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace App\Kobo; | ||
|
||
use App\Entity\KoboDevice; | ||
use App\Kobo\Proxy\KoboStoreProxy; | ||
use App\Kobo\Response\SyncResponse; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class UpstreamSyncMerger | ||
{ | ||
public function __construct( | ||
private readonly KoboStoreProxy $koboStoreProxy, | ||
private readonly LoggerInterface $koboSyncLogger, | ||
) { | ||
} | ||
|
||
public function merge(KoboDevice $device, SyncResponse $syncResponse, Request $request): bool | ||
{ | ||
// Make sure the merge is enabled | ||
if (false === $device->isUpstreamSync() || false === $this->koboStoreProxy->isEnabled()) { | ||
$this->koboSyncLogger->debug('Your device {device} has "upstream sync" disabled', [ | ||
'device' => $device->getId(), | ||
]); | ||
|
||
return false; | ||
} | ||
|
||
$response = $this->koboStoreProxy->proxy($request, ['stream' => false]); | ||
if (false === $response->isOk()) { | ||
$this->koboSyncLogger->error('Sync response is not ok. Got '.$response->getStatusCode()); | ||
|
||
return false; | ||
} | ||
|
||
$json = $this->parseJson($response); | ||
|
||
if ($json === []) { | ||
return false; | ||
} | ||
|
||
$this->koboSyncLogger->info('Merging {count} upstream items', [ | ||
'device' => $device->getId(), | ||
'count' => count($json), | ||
]); | ||
$syncResponse->addRemoteItems($json); | ||
|
||
return $this->shouldContinue($response); | ||
} | ||
|
||
private function parseJson(Response $response): array | ||
{ | ||
try { | ||
$result = $response->getContent(); | ||
if (false === $result) { | ||
throw new \RuntimeException('Response content is false. Code: '.$response->getStatusCode()); | ||
} | ||
|
||
return (array) json_decode($result, true, 512, JSON_THROW_ON_ERROR); | ||
} catch (\Throwable $e) { | ||
$this->koboSyncLogger->warning('Unable to upstream sync response: {content}', [ | ||
'exception' => $e, | ||
'content' => $response->getContent(), | ||
]); | ||
|
||
return []; | ||
} | ||
} | ||
|
||
private function shouldContinue(Response $response): bool | ||
{ | ||
return $response->headers->get('x-kobo-sync') === 'continue'; | ||
} | ||
} |