summaryrefslogtreecommitdiff
path: root/app/Http/RequestHandlers/ExportGedcomClient.php
diff options
context:
space:
mode:
authorGreg Roach <greg@subaqua.co.uk>2022-04-20 09:35:50 +0100
committerGreg Roach <greg@subaqua.co.uk>2022-04-20 13:11:57 +0100
commit16ecfcafdc113f0b4074e256aa90a4c870e7b2f0 (patch)
tree3a5e71db430479aa1b753ac6950d22004c701b2c /app/Http/RequestHandlers/ExportGedcomClient.php
parenteee2b8660047cf87c372fc31ab83bf3af851dfad (diff)
downloadwebtrees-16ecfcafdc113f0b4074e256aa90a4c870e7b2f0.tar.gz
webtrees-16ecfcafdc113f0b4074e256aa90a4c870e7b2f0.tar.bz2
webtrees-16ecfcafdc113f0b4074e256aa90a4c870e7b2f0.zip
Add support for GEDZIP exports. Fixes: #4288
Diffstat (limited to 'app/Http/RequestHandlers/ExportGedcomClient.php')
-rw-r--r--app/Http/RequestHandlers/ExportGedcomClient.php109
1 files changed, 7 insertions, 102 deletions
diff --git a/app/Http/RequestHandlers/ExportGedcomClient.php b/app/Http/RequestHandlers/ExportGedcomClient.php
index 79b870b4f0..0980406c4f 100644
--- a/app/Http/RequestHandlers/ExportGedcomClient.php
+++ b/app/Http/RequestHandlers/ExportGedcomClient.php
@@ -19,36 +19,19 @@ declare(strict_types=1);
namespace Fisharebest\Webtrees\Http\RequestHandlers;
-use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Encodings\ANSEL;
use Fisharebest\Webtrees\Encodings\ASCII;
use Fisharebest\Webtrees\Encodings\UTF16BE;
use Fisharebest\Webtrees\Encodings\UTF8;
use Fisharebest\Webtrees\Encodings\Windows1252;
-use Fisharebest\Webtrees\GedcomRecord;
use Fisharebest\Webtrees\Http\ViewResponseTrait;
-use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Services\GedcomExportService;
use Fisharebest\Webtrees\Validator;
-use Illuminate\Database\Capsule\Manager as DB;
-use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemException;
-use League\Flysystem\ZipArchive\FilesystemZipArchiveProvider;
-use League\Flysystem\ZipArchive\ZipArchiveAdapter;
-use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
-use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Server\RequestHandlerInterface;
-use function addcslashes;
-use function fclose;
-use function pathinfo;
-use function strtolower;
-use function tmpfile;
-
-use const PATHINFO_EXTENSION;
-
/**
* Download a GEDCOM file to the client.
*/
@@ -58,108 +41,30 @@ class ExportGedcomClient implements RequestHandlerInterface
private GedcomExportService $gedcom_export_service;
- private ResponseFactoryInterface $response_factory;
-
- private StreamFactoryInterface $stream_factory;
-
/**
* ExportGedcomServer constructor.
*
- * @param GedcomExportService $gedcom_export_service
- * @param ResponseFactoryInterface $response_factory
- * @param StreamFactoryInterface $stream_factory
+ * @param GedcomExportService $gedcom_export_service
*/
- public function __construct(
- GedcomExportService $gedcom_export_service,
- ResponseFactoryInterface $response_factory,
- StreamFactoryInterface $stream_factory
- ) {
+ public function __construct(GedcomExportService $gedcom_export_service)
+ {
$this->gedcom_export_service = $gedcom_export_service;
- $this->response_factory = $response_factory;
- $this->stream_factory = $stream_factory;
}
/**
* @param ServerRequestInterface $request
*
* @return ResponseInterface
- * @throws FilesystemException
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
- $tree = Validator::attributes($request)->tree();
-
- $data_filesystem = Registry::filesystem()->data();
-
- $format = Validator::parsedBody($request)->isInArray(['gedcom', 'zip'])->string('format');
+ $tree = Validator::attributes($request)->tree();
+ $filename = Validator::parsedBody($request)->string('filename');
+ $format = Validator::parsedBody($request)->isInArray(['gedcom', 'zip', 'zipmedia', 'gedzip'])->string('format');
$privacy = Validator::parsedBody($request)->isInArray(['none', 'gedadmin', 'user', 'visitor'])->string('privacy');
$encoding = Validator::parsedBody($request)->isInArray([UTF8::NAME, UTF16BE::NAME, ANSEL::NAME, ASCII::NAME, Windows1252::NAME])->string('encoding');
$line_endings = Validator::parsedBody($request)->isInArray(['CRLF', 'LF'])->string('line_endings');
- $media_path = Validator::parsedBody($request)->string('media_path', '');
-
- $access_levels = [
- 'gedadmin' => Auth::PRIV_NONE,
- 'user' => Auth::PRIV_USER,
- 'visitor' => Auth::PRIV_PRIVATE,
- 'none' => Auth::PRIV_HIDE,
- ];
-
- $access_level = $access_levels[$privacy];
-
- // What to call the downloaded file
- $download_filename = $tree->name();
-
- // Force a ".ged" suffix
- if (strtolower(pathinfo($download_filename, PATHINFO_EXTENSION)) !== 'ged') {
- $download_filename .= '.ged';
- }
-
- if ($format === 'zip') {
- $resource = $this->gedcom_export_service->export($tree, true, $encoding, $access_level, $media_path, $line_endings);
-
- $path = $tree->getPreference('MEDIA_DIRECTORY');
-
- // Create a new/empty .ZIP file
- $temp_zip_file = stream_get_meta_data(tmpfile())['uri'];
- $zip_provider = new FilesystemZipArchiveProvider($temp_zip_file, 0755);
- $zip_adapter = new ZipArchiveAdapter($zip_provider);
- $zip_filesystem = new Filesystem($zip_adapter);
- $zip_filesystem->writeStream($download_filename, $resource);
- fclose($resource);
-
- $media_filesystem = $tree->mediaFilesystem($data_filesystem);
-
- $records = DB::table('media')
- ->where('m_file', '=', $tree->id())
- ->get()
- ->map(Registry::mediaFactory()->mapper($tree))
- ->filter(GedcomRecord::accessFilter());
-
- foreach ($records as $record) {
- foreach ($record->mediaFiles() as $media_file) {
- $from = $media_file->filename();
- $to = $path . $media_file->filename();
- if (!$media_file->isExternal() && $media_filesystem->fileExists($from) && !$zip_filesystem->fileExists($to)) {
- $zip_filesystem->writeStream($to, $media_filesystem->readStream($from));
- }
- }
- }
-
- $stream = $this->stream_factory->createStreamFromFile($temp_zip_file);
- $filename = addcslashes($download_filename, '"') . '.zip';
-
- return $this->response_factory->createResponse()
- ->withBody($stream)
- ->withHeader('content-type', 'application/zip')
- ->withHeader('content-disposition', 'attachment; filename="' . $filename . '"');
- }
-
- $resource = $this->gedcom_export_service->export($tree, true, $encoding, $access_level, $media_path);
- $stream = $this->stream_factory->createStreamFromResource($resource);
- return $this->response_factory->createResponse()
- ->withBody($stream)
- ->withHeader('content-type', 'text/x-gedcom; charset=' . UTF8::NAME)
- ->withHeader('content-disposition', 'attachment; filename="' . addcslashes($download_filename, '"') . '"');
+ return $this->gedcom_export_service->downloadResponse($tree, true, $encoding, $privacy, $line_endings, $filename, $format);
}
}