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

feat(api): added possibility for backup of content-folder #3003

Merged
merged 3 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 7 additions & 2 deletions docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
{
"name": "type",
"in": "path",
"description": "The backup type. Can be \"data\" or \"logs\".",
"description": "The backup type. Can be \"data\", \"logs\" or \"content\".",
"required": true,
"schema": {
"type": "string"
Expand All @@ -91,7 +91,7 @@
],
"responses": {
"200": {
"description": "The current backup as a file.",
"description": "The current backup as a file or ZipArchive in case of \"content\"-type.",
"headers": {
"Accept-Language": {
"description": "The language code for the login.",
Expand All @@ -111,6 +111,11 @@
"schema": {
"type": "string"
}
},
"application/zip": {
"schema": {
"type": "string"
}
}
}
},
Expand Down
7 changes: 5 additions & 2 deletions docs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ paths:
parameters:
- name: type
in: path
description: 'The backup type. Can be "data" or "logs".'
description: 'The backup type. Can be "data", "logs" or "content".'
required: true
schema:
type: string
responses:
'200':
description: 'The current backup as a file.'
description: 'The current backup as a file or ZipArchive in case of "content"-type.'
headers:
Accept-Language:
description: 'The language code for the login.'
Expand All @@ -77,6 +77,9 @@ paths:
application/octet-stream:
schema:
type: string
application/zip:
schema:
type: string
'400':
description: 'If the backup type is wrong'
headers:
Expand Down
33 changes: 33 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Administration/Backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
namespace phpMyFAQ\Administration;

use phpMyFAQ\Configuration;
use phpMyFAQ\Core\Exception;
use phpMyFAQ\Database;
use phpMyFAQ\Database\DatabaseHelper;
use phpMyFAQ\Enums\BackupType;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SodiumException;
use ZipArchive;

/**
* Class Backup
Expand Down Expand Up @@ -153,4 +157,33 @@ private function getBackupHeader(string $tableNames): array
'-- Otherwise this backup will be broken.'
];
}

public function createContentFolderBackup(): string|bool
{
$zipFile = PMF_ROOT_DIR . DIRECTORY_SEPARATOR . 'content.zip';

$zipArchive = new ZipArchive();
if ($zipArchive->open($zipFile, ZipArchive::CREATE) !== true) {
return false;
modelrailroader marked this conversation as resolved.
Show resolved Hide resolved
}

$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(PMF_CONTENT_DIR)
);

foreach ($files as $file) {
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen(PMF_CONTENT_DIR) + 1);

if ($file->isDir()) {
$zipArchive->addEmptyDir($relativePath);
} else {
$zipArchive->addFile($filePath, $relativePath);
}
}

$zipArchive->close();

return file_exists($zipFile) ? $zipFile : false;
modelrailroader marked this conversation as resolved.
Show resolved Hide resolved
}
}
46 changes: 40 additions & 6 deletions phpmyfaq/src/phpMyFAQ/Controller/Api/BackupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ class BackupController extends AbstractController
)]
#[OA\Parameter(
name: 'type',
description: 'The backup type. Can be "data" or "logs".',
description: 'The backup type. Can be "data", "logs" or "content".',
in: 'path',
required: true,
schema: new OA\Schema(type: 'string')
)]
#[OA\Response(
response: 200,
description: 'The current backup as a file.',
description: 'The current backup as a file or a ZipArchive in case of "content"-type.',
content: new OA\MediaType(
mediaType: 'application/octet-stream',
schema: new OA\Schema(type: 'string')
mediaType: 'application/octet-stream or application/zip',
schema: new OA\Schema(type: 'string'),
)
)]
#[OA\Response(
response: 400,
description: 'If the backup type is wrong',
description: 'If the backup type is wrong or an internal error occurred',
content: new OA\MediaType(
mediaType: 'application/octet-stream',
schema: new OA\Schema(type: 'string')
Expand All @@ -75,12 +75,43 @@ public function download(Request $request): Response
case 'logs':
$backupType = BackupType::BACKUP_TYPE_LOGS;
break;
case 'content':
$backupType = BackupType::BACKUP_TYPE_CONTENT;
break;
default:
return new Response('Invalid backup type.', Response::HTTP_BAD_REQUEST);
}

$dbHelper = new DatabaseHelper($this->configuration);
$backup = new Backup($this->configuration, $dbHelper);

// Create ZipArchive of content-folder
if ($backupType === BackupType::BACKUP_TYPE_CONTENT) {
$backupFile = $backup->createContentFolderBackup();
if ($backupFile !== false) {
$response = new Response(file_get_contents($backupFile));

$backupFileName = sprintf('content_%s.zip', date('dmY_H-i'));

$disposition = HeaderUtils::makeDisposition(
HeaderUtils::DISPOSITION_ATTACHMENT,
urlencode($backupFileName)
);

$response->headers->set('Content-Type', 'application/zip');
$response->headers->set('Content-Disposition', $disposition);
$response->setStatusCode(Response::HTTP_OK);
// Remove temporary ZipArchive
unlink($backupFile);
return $response->send();
} else {
return new Response(
'An error occurred while creating the zip-file.',
Response::HTTP_INTERNAL_SERVER_ERROR
);
}
}

$tableNames = $backup->getBackupTableNames($backupType);
$backupQueries = $backup->generateBackupQueries($tableNames);

Expand All @@ -99,7 +130,10 @@ public function download(Request $request): Response
$response->setStatusCode(Response::HTTP_OK);
return $response->send();
} catch (SodiumException) {
return new Response('An error occurred while creating the backup.', Response::HTTP_INTERNAL_SERVER_ERROR);
return new Response(
'An error occurred while creating the backup.',
Response::HTTP_INTERNAL_SERVER_ERROR
);
}
}
}
1 change: 1 addition & 0 deletions phpmyfaq/src/phpMyFAQ/Enums/BackupType.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ enum BackupType: string
{
case BACKUP_TYPE_DATA = 'data';
case BACKUP_TYPE_LOGS = 'logs';
case BACKUP_TYPE_CONTENT = 'content';
}
2 changes: 2 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Faq/Statistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public function __construct(private readonly Configuration $configuration)

if ($this->configuration->get('security.permLevel') !== 'basic') {
$this->groupSupport = true;
} else {
$this->groupSupport = false;
modelrailroader marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down