Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UCRM 6305 - Dropbox plugin - Use writeStream() #341

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified plugins/backup-sync-dropbox/backup-sync-dropbox.zip
Binary file not shown.
4 changes: 2 additions & 2 deletions plugins/backup-sync-dropbox/src/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
},
"require": {
"ext-mbstring": "*",
"ubnt/ucrm-plugin-sdk": "^0.9",
"php-di/php-di": "^7.0",
"psr/log": "^3.0",
"league/flysystem": "^3.12",
"spatie/flysystem-dropbox": "^3.0",
"stevenmaguire/oauth2-dropbox": "^3.1"
"stevenmaguire/oauth2-dropbox": "^3.1",
"ubnt/ucrm-plugin-sdk": "^0.12.0"
}
}
521 changes: 249 additions & 272 deletions plugins/backup-sync-dropbox/src/composer.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions plugins/backup-sync-dropbox/src/main.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use BackupSyncDropbox\Handler\BackupHandler;
use BackupSyncDropbox\Service\UnmsApiDropbox;
use BackupSyncDropbox\TokenProvider\DropboxTokenProvider;
use BackupSyncDropbox\Utility\LogCleaner;
use BackupSyncDropbox\Utility\Logger;
Expand All @@ -14,7 +15,6 @@
use Ubnt\UcrmPluginSdk\Service\PluginConfigManager;
use Ubnt\UcrmPluginSdk\Service\PluginLogManager;
use Ubnt\UcrmPluginSdk\Service\UcrmApi;
use Ubnt\UcrmPluginSdk\Service\UnmsApi;

require_once __DIR__ . '/vendor/autoload.php';

Expand Down Expand Up @@ -42,7 +42,7 @@
$builder->addDefinitions(
[
Filesystem::class => $filesystem,
UnmsApi::class => UnmsApi::create($unmsApiToken),
UnmsApiDropbox::class => UnmsApiDropbox::create($unmsApiToken),
UcrmApi::class => UcrmApi::create(),
PluginLogManager::class => $pluginLogManager,
LoggerInterface::class => $logger,
Expand Down
2 changes: 1 addition & 1 deletion plugins/backup-sync-dropbox/src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"displayName": "Backup synchronization - Dropbox",
"description": "This plugin handles synchronization of your UNMS backups to a Dropbox folder.",
"url": "https://github.com/Ubiquiti-App/UCRM-plugins/tree/master/plugins/backup-sync-dropbox",
"version": "1.2.0",
"version": "1.2.1",
"unmsVersionCompliancy": {
"min": "2.1.0",
"max": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,17 @@
namespace BackupSyncDropbox\DataProvider;

use BackupSyncDropbox\Data\UnmsBackup;
use BackupSyncDropbox\Service\UnmsApiDropbox;
use BackupSyncDropbox\Utility\NmsSettings;
use DateTimeImmutable;
use DateTimeZone;
use Ubnt\UcrmPluginSdk\Service\UnmsApi;

final class BackupDataProvider
{
/**
* @var UnmsApi
*/
private $unmsApi;

/**
* @var NmsSettings
*/
private $nmsSettings;

public function __construct(UnmsApi $unmsApi, NmsSettings $nmsSettings)
{
$this->unmsApi = $unmsApi;
$this->nmsSettings = $nmsSettings;
public function __construct(
private UnmsApiDropbox $unmsApiDropbox,
private NmsSettings $nmsSettings
) {
}

/**
Expand All @@ -36,7 +26,7 @@ public function getListOfUnmsBackups(): array
$list = [];
$nmsTimeZone = $this->nmsSettings->getTimeZone();

$data = $this->unmsApi->get('nms/backups');
$data = $this->unmsApiDropbox->get('nms/backups');
foreach ($data as $item) {
if ($item['state'] !== 'success') {
continue;
Expand Down
43 changes: 21 additions & 22 deletions plugins/backup-sync-dropbox/src/src/Facade/BackupFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,17 @@
namespace BackupSyncDropbox\Facade;

use BackupSyncDropbox\Data\UnmsBackup;
use BackupSyncDropbox\Service\UnmsApiDropbox;
use League\Flysystem\Filesystem;
use Psr\Log\LoggerInterface;
use Ubnt\UcrmPluginSdk\Service\UnmsApi;

final class BackupFacade
{
/**
* @var UnmsApi
*/
private $unmsApi;

/**
* @var Filesystem
*/
private $filesystem;

/**
* @var LoggerInterface
*/
private $logger;

public function __construct(UnmsApi $unmsApi, Filesystem $filesystem, LoggerInterface $logger)
{
$this->unmsApi = $unmsApi;
$this->filesystem = $filesystem;
$this->logger = $logger;
public function __construct(
private UnmsApiDropbox $unmsApiDropbox,
private Filesystem $filesystem,
private LoggerInterface $logger
) {
}

public function upload(UnmsBackup $unmsBackup): void
Expand All @@ -41,7 +26,11 @@ public function upload(UnmsBackup $unmsBackup): void
return;
}

$this->filesystem->write($unmsBackup->filename, $this->unmsApi->get(sprintf('nms/backups/%s', $unmsBackup->id)));
$temporaryFile = $this->getTemporaryFile();
$this->unmsApiDropbox->getSink(sprintf('nms/backups/%s', $unmsBackup->id), $temporaryFile);
$resource = fopen($temporaryFile, 'rb+');
$this->filesystem->writeStream($unmsBackup->filename, $resource);
unlink($temporaryFile);

$this->logger->info(sprintf('Uploaded file "%s".', $unmsBackup->filename));
}
Expand All @@ -63,4 +52,14 @@ public function deleteExcept(array $filenames): void
$this->logger->info(sprintf('Deleted file "%s".', $item['path']));
}
}

private function getTemporaryFile(): string
{
$tempDir = realpath(sys_get_temp_dir());
assert(is_string($tempDir));
$tmpFile = tempnam($tempDir, 'ucrmTmpFile');
assert(is_string($tmpFile));

return $tmpFile;
}
}
24 changes: 3 additions & 21 deletions plugins/backup-sync-dropbox/src/src/Handler/BackupHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,11 @@

final class BackupHandler
{
/**
* @var LoggerInterface
*/
private $logger;

/**
* @var BackupFacade
*/
private $backupFacade;

/**
* @var BackupDataProvider
*/
private $backupDataProvider;

public function __construct(
LoggerInterface $logger,
BackupFacade $backupFacade,
BackupDataProvider $backupDataProvider
private LoggerInterface $logger,
private BackupFacade $backupFacade,
private BackupDataProvider $backupDataProvider
) {
$this->logger = $logger;
$this->backupFacade = $backupFacade;
$this->backupDataProvider = $backupDataProvider;
}

public function sync(): void
Expand Down
22 changes: 22 additions & 0 deletions plugins/backup-sync-dropbox/src/src/Service/UnmsApiDropbox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace BackupSyncDropbox\Service;

use GuzzleHttp\RequestOptions;
use Ubnt\UcrmPluginSdk\Service\UnmsApi;

final class UnmsApiDropbox extends UnmsApi
{
public function getSink(string $endpoint, string $filePath): void
{
$this->request(
'GET',
$endpoint,
[
RequestOptions::SINK => $filePath,
]
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,10 @@

class DropboxTokenProvider implements TokenProvider
{
/**
* @var PluginLogManager
*/
private $pluginLogManager;

/**
* @var PluginConfigManager
*/
private $pluginConfigManager;

public function __construct(PluginLogManager $pluginLogManager, PluginConfigManager $pluginConfigManager)
{
$this->pluginLogManager = $pluginLogManager;
$this->pluginConfigManager = $pluginConfigManager;
public function __construct(
private PluginLogManager $pluginLogManager,
private PluginConfigManager $pluginConfigManager
) {
}

public function getToken(): string
Expand Down
8 changes: 1 addition & 7 deletions plugins/backup-sync-dropbox/src/src/Utility/LogCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,8 @@ final class LogCleaner
{
private const MAX_LINES = 256;

/**
* @var PluginLogManager
*/
private $pluginLogManager;

public function __construct(PluginLogManager $pluginLogManager)
public function __construct(private PluginLogManager $pluginLogManager)
{
$this->pluginLogManager = $pluginLogManager;
}

public function clean(): void
Expand Down
8 changes: 1 addition & 7 deletions plugins/backup-sync-dropbox/src/src/Utility/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,8 @@ final class Logger implements LoggerInterface
{
use LoggerTrait;

/**
* @var PluginLogManager
*/
private $pluginLogManager;

public function __construct(PluginLogManager $pluginLogManager)
public function __construct(private PluginLogManager $pluginLogManager)
{
$this->pluginLogManager = $pluginLogManager;
}

public function log($level, $message, array $context = []): void
Expand Down
12 changes: 3 additions & 9 deletions plugins/backup-sync-dropbox/src/src/Utility/NmsSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,20 @@

namespace BackupSyncDropbox\Utility;

use BackupSyncDropbox\Service\UnmsApiDropbox;
use DateTimeZone;
use Exception;
use Ubnt\UcrmPluginSdk\Service\UnmsApi;

final class NmsSettings
{
/**
* @var UnmsApi
*/
private $unmsApi;

public function __construct(UnmsApi $unmsApi)
public function __construct(private UnmsApiDropbox $unmsApiDropbox)
{
$this->unmsApi = $unmsApi;
}

public function getTimeZone(): DateTimeZone
{
try {
$nmsSettings = $this->unmsApi->get('nms/settings');
$nmsSettings = $this->unmsApiDropbox->get('nms/settings');
$timeZone = is_array($nmsSettings)
? ($nmsSettings['timezone'] ?? null)
: null;
Expand Down
2 changes: 1 addition & 1 deletion plugins_2.1.x.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"displayName": "Backup synchronization - Dropbox",
"description": "This plugin handles synchronization of your UNMS backups to a Dropbox folder.",
"url": "https:\/\/github.com\/Ubiquiti-App\/UCRM-plugins\/tree\/master\/plugins\/backup-sync-dropbox",
"version": "1.2.0",
"version": "1.2.1",
"unmsVersionCompliancy": {
"min": "2.1.0",
"max": null
Expand Down
2 changes: 1 addition & 1 deletion plugins_2.2.x.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"displayName": "Backup synchronization - Dropbox",
"description": "This plugin handles synchronization of your UNMS backups to a Dropbox folder.",
"url": "https:\/\/github.com\/Ubiquiti-App\/UCRM-plugins\/tree\/master\/plugins\/backup-sync-dropbox",
"version": "1.2.0",
"version": "1.2.1",
"unmsVersionCompliancy": {
"min": "2.1.0",
"max": null
Expand Down
2 changes: 1 addition & 1 deletion plugins_2.3.x.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"displayName": "Backup synchronization - Dropbox",
"description": "This plugin handles synchronization of your UNMS backups to a Dropbox folder.",
"url": "https:\/\/github.com\/Ubiquiti-App\/UCRM-plugins\/tree\/master\/plugins\/backup-sync-dropbox",
"version": "1.2.0",
"version": "1.2.1",
"unmsVersionCompliancy": {
"min": "2.1.0",
"max": null
Expand Down
2 changes: 1 addition & 1 deletion plugins_2.4.x.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"displayName": "Backup synchronization - Dropbox",
"description": "This plugin handles synchronization of your UNMS backups to a Dropbox folder.",
"url": "https:\/\/github.com\/Ubiquiti-App\/UCRM-plugins\/tree\/master\/plugins\/backup-sync-dropbox",
"version": "1.2.0",
"version": "1.2.1",
"unmsVersionCompliancy": {
"min": "2.1.0",
"max": null
Expand Down
2 changes: 1 addition & 1 deletion plugins_2.5.x.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"displayName": "Backup synchronization - Dropbox",
"description": "This plugin handles synchronization of your UNMS backups to a Dropbox folder.",
"url": "https:\/\/github.com\/Ubiquiti-App\/UCRM-plugins\/tree\/master\/plugins\/backup-sync-dropbox",
"version": "1.2.0",
"version": "1.2.1",
"unmsVersionCompliancy": {
"min": "2.1.0",
"max": null
Expand Down
2 changes: 1 addition & 1 deletion plugins_3.0.x.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"displayName": "Backup synchronization - Dropbox",
"description": "This plugin handles synchronization of your UNMS backups to a Dropbox folder.",
"url": "https:\/\/github.com\/Ubiquiti-App\/UCRM-plugins\/tree\/master\/plugins\/backup-sync-dropbox",
"version": "1.2.0",
"version": "1.2.1",
"unmsVersionCompliancy": {
"min": "2.1.0",
"max": null
Expand Down
Loading