Skip to content

Commit

Permalink
Cleanup; Enable all kirby roots; Fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMaaarc committed Jul 18, 2023
1 parent 732497b commit e001ef1
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 35 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,21 @@ git submodule add https://github.com/zephir/kirby-contentsync.git site/plugins/k

## 2. Setup

After installation, you need to add the plugin options to your config.
After installation, you simply have to configure the [Options](#3-options), deploy the updated site to the server and use [the Kirby command](#4-usage).

## 3. Options

| Option | Type | Default | Required | Description |
| ------------ | ------ | ----------------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| source | string | null || Source of the content, normally the staging / prod server. URL of the host (e.g. https://getkirby.com) |
| token | string | null || The authentication token, make sure this token is not accessible by the public. Either use an env file/variable or use a private git repository. |
| enabledRoots | array | [see below](#31-enabledroots) || The authentication token, make sure this token is not accessible by the public. Either use an env file/variable or use a private git repository. |
| Option | Type | Default | Required | Description |
| ------------ | ------ | ----------------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| source | string | null || Source of the content, normally the staging / prod server. URL of the host (e.g. https://getkirby.com) |
| token | string | null || The authentication token, make sure this token is not accessible by the public. Either use an env file/variable or use a private git repository. |
| enabledRoots | array | [see below](#31-enabledroots) || Which roots should be synchronized by the plugin. You can see all [available roots here.](https://getkirby.com/docs/guide/configuration#custom-folder-setup__all-configurable-roots) |

### 3.1 enabledRoots

The plugin supports synchronizing the `accounts' and `content' folders. To support different types of folder structures we use the `kirby()->roots()` function (hence the name of the option). You can enable/disable either the `accounts` or `content` roots.

Default:
To support different types of folder structures we use the `kirby()->roots()` function (hence the name of the option).
The plugin supports synchronizing all kirby root paths.
By default the plugin is configured to sync the `accounts` and `content` roots.

```php
[
Expand All @@ -73,16 +73,15 @@ Default:
'zephir.contentsync' => [
'source' => 'https://getkirby.com',
'token' => 'abc123',
'enabledRoots' => [ // Default, not required
'accounts' => true,
'content' => true
'enabledRoots' => [
'license' => true // also sync license
]
]
```

## 4. Usage

After installing, setting up options, and deploying the changes to the server, you can simply run the kirby command:
Kirby CLI Command:

`kirby content:sync`

Expand All @@ -105,6 +104,7 @@ It doesn't sync files that haven't changed.

1. Since we download each file individually, it is possible that a WAF / Firewall will block the requests. You can add an exception for the endpoints.
2. Generating the checksum can put a bit of load on the server, especially for large files. But for "normal" websites it should be fine - even if you have several gigabytes of data / many files.
3. Because we serve all files through PHP and a web endpoint, downloads of large files (200MB+) may be stopped by the server hoster (especially with shared hosting).

## License

Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
],
"require": {
"getkirby/composer-installer": "^1.2",
"getkirby/cli": "^1.1",
"guzzlehttp/guzzle": "^7.7"
"getkirby/cli": "^1.1"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 3 additions & 3 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
AuthProvider::validate();
return FileProvider::fileDownload($fileId);
} catch (Exception $e) {
var_dump($e);
return Response::json($e->toArray(), $e->getHttpCode());
}
}
Expand All @@ -60,10 +59,11 @@
],
'options' => [
'source' => null,
'token' => null,
'token' => 'abc',
'enabledRoots' => [
'content' => true,
'accounts' => true
'accounts' => true,
'license' => true
]
]
]);
12 changes: 10 additions & 2 deletions src/Collections/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Kirby\Toolkit\A;
use Kirby\Filesystem\Dir;
use Zephir\Contentsync\Collections\Roots;
use Zephir\Contentsync\Helpers\Roots;
use Zephir\Contentsync\Models\File;

class Files
Expand Down Expand Up @@ -36,7 +36,15 @@ public function collectFiles(bool $generateChecksum = false)
$files = [];

foreach (Roots::getEnabled() as $kirbyRootName => $rootPath) {
$files = A::merge($files, $this->collectFilesForRoot($kirbyRootName, $rootPath, $generateChecksum));
if (is_dir($rootPath)) {
$files = A::merge($files, $this->collectFilesForRoot($kirbyRootName, $rootPath, $generateChecksum));
} else if (is_file($rootPath)) {
$file = new File($kirbyRootName, $rootPath);
if ($generateChecksum) {
$file->generateChecksum();
}
$files[] = $file;
}
}

$this->all = $files;
Expand Down
3 changes: 2 additions & 1 deletion src/FileProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public static function fileDownload(string $fileId)
*/
private static function returnFileStream($file)
{
Header::contentType(F::extensionToMime(F::extension($file->getAbsolutePath())));
$mime = F::extensionToMime(F::extension($file->getAbsolutePath())) ?? 'text/plain';
Header::contentType($mime);

$buffer = '';
$handle = fopen($file->getAbsolutePath(), 'rb');
Expand Down
10 changes: 2 additions & 8 deletions src/Collections/Roots.php → src/Helpers/Roots.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
<?php

namespace Zephir\Contentsync\Collections;
namespace Zephir\Contentsync\Helpers;

class Roots
{

/**
* @var array
*/
private static $allowedKirbyRoots = ['content', 'accounts'];

/**
* @return array
*/
Expand All @@ -18,8 +13,7 @@ public static function getEnabled()
$enabledRoots = option('zephir.contentsync.enabledRoots');
$roots = [];

foreach (self::$allowedKirbyRoots as $rootName) {
$root = kirby()->root($rootName);
foreach (kirby()->roots()->toArray() as $rootName => $root) {
if (isset($enabledRoots[$rootName]) && $enabledRoots[$rootName] === true) {
$roots[$rootName] = $root;
}
Expand Down
11 changes: 6 additions & 5 deletions src/Models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class File
*/
public function __construct($kirbyRootName, $absolutePath, $checksum = null)
{
$path = str_replace(kirby()->roots()->{$kirbyRootName}, '', $absolutePath);
$path = str_replace(kirby()->root($kirbyRootName), '', $absolutePath);
$this->id = sha1($kirbyRootName . $path);
$this->path = $path;
$this->kirbyRootName = $kirbyRootName;
Expand All @@ -50,7 +50,7 @@ public function getAbsolutePath()
{
return A::join(
[
kirby()->roots()->{$this->kirbyRootName},
kirby()->root($this->kirbyRootName),
$this->path
],
''
Expand Down Expand Up @@ -122,16 +122,17 @@ public function update()
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . option('zephir.contentsync.token')));
curl_setopt($ch, CURLOPT_FILE, $fp);

$response = curl_exec($ch);
curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);
fclose($fp);

if ($httpcode !== 200) {
$response = json_decode($response);
$response = json_decode(F::read($this->getAbsolutePath()));
F::remove($this->getAbsolutePath());
throw new Exception([
'fallback' => 'Server: ' . $response->message . ' in ' . $response->file . ' on line ' . $response->line,
'fallback' => 'Server: ' . $response->message,
'httpCode' => $httpcode
]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/SyncProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private function fetchFiles()
}
if ($httpcode !== 200) {
throw new Exception([
'fallback' => 'Server: ' . $response->message . ' in ' . $response->file . ' on line ' . $response->line,
'fallback' => 'Server: ' . $response->message,
'httpCode' => $httpcode
]);
}
Expand Down

0 comments on commit e001ef1

Please sign in to comment.