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

Adjust to atk4/ui PSR-7 response #32

Merged
merged 5 commits into from
Apr 8, 2023
Merged
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"require-release": {
"php": ">=7.4 <8.3",
"atk4/ui": "~4.0.0",
"atk4/ui": "~5.0.0",
"league/flysystem": "^2.0"
},
"require-dev": {
Expand Down
4 changes: 1 addition & 3 deletions demos/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@
$form->onSubmit(function (Form $form) use ($gr) {
$form->model->save();

return [
$gr->jsReload(),
];
return $gr->jsReload();
});

View::addTo($app, ['ui' => 'divider']);
Expand Down
98 changes: 28 additions & 70 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,95 +6,53 @@

use Atk4\Filestore\Model\File;
use Atk4\Ui\App;
use Nyholm\Psr7\Factory\Psr17Factory;

class Helper
{
/**
* @return never
*/
public static function download(File $model, App $app = null): void
public static function download(File $model, App $app): void
{
$headers = [
'Content-Description' => 'File Transfer',
'Content-Type' => 'application/octet-stream',
'Cache-Control' => 'must-revalidate',
'Expires' => '-1',
'Content-Disposition' => 'attachment; filename="' . $model->get('meta_filename') . '"',
'Content-Length' => (string) $model->get('meta_size'),
'Pragma' => 'public',
'Accept-Ranges' => 'bytes',
];

static::output($model, $headers, $app);
$app->setResponseHeader('Content-Description', 'File Transfer');
$app->setResponseHeader('Content-Type', 'application/octet-stream');
$app->setResponseHeader('Cache-Control', 'must-revalidate');
$app->setResponseHeader('Expires', '-1');
$app->setResponseHeader('Content-Disposition', 'attachment; filename="' . $model->get('meta_filename') . '"');
$app->setResponseHeader('Content-Length', (string) $model->get('meta_size'));
$app->setResponseHeader('Pragma', 'public');
$app->setResponseHeader('Accept-Ranges', 'bytes');

static::output($model, $app);
}

/**
* @param array<string, string> $headers
*
* @return never
*/
protected static function output(File $model, array $headers, App $app = null): void
protected static function output(File $model, App $app): void
{
// TODO move to App and add support for streams

$headers = self::normalizeHeaders($headers);

$location = $model->get('location');

if ($app !== null) {
$app->terminate($model->flysystem->read($location), $headers);
}

$isCli = \PHP_SAPI === 'cli'; // for phpunit

foreach ($headers as $k => $v) {
if (!$isCli) {
$kCamelCase = preg_replace_callback('~(?<![a-zA-Z])[a-z]~', function ($matches) {
return strtoupper($matches[0]);
}, $k);

header($kCamelCase . ': ' . $v);
}
}

fpassthru($model->flysystem->readStream($location));
$path = $model->get('location');
$resource = $model->flysystem->readStream($path);
$stream = (new Psr17Factory())->createStreamFromResource($resource);

exit;
$app->terminate($stream);
}

/**
* @return never
*/
public static function view(File $model, App $app = null): void
public static function view(File $model, App $app): void
{
$headers = [
'Content-Description' => 'File Transfer',
'Content-Type' => $model->get('meta_mime_type'),
'Cache-Control' => 'must-revalidate',
'Expires' => '-1',
'Content-Disposition' => 'inline; filename="' . $model->get('meta_filename') . '"',
'Content-Length' => (string) $model->get('meta_size'),
'Pragma' => 'public',
'Accept-Ranges' => 'bytes',
];

static::output($model, $headers, $app);
}

/**
* Copied from atk4/ui App class.
*
* @param array<string, string> $headers
*
* @return array<string, string>
*/
private static function normalizeHeaders(array $headers): array
{
$res = [];
foreach ($headers as $k => $v) {
$res[strtolower(trim($k))] = trim($v);
}

return $res;
$app->setResponseHeader('Content-Description', 'File Transfer');
$app->setResponseHeader('Content-Type', $model->get('meta_mime_type'));
$app->setResponseHeader('Cache-Control', 'must-revalidate');
$app->setResponseHeader('Expires', '-1');
$app->setResponseHeader('Content-Disposition', 'inline; filename="' . $model->get('meta_filename') . '"');
$app->setResponseHeader('Content-Length', (string) $model->get('meta_size'));
$app->setResponseHeader('Pragma', 'public');
$app->setResponseHeader('Accept-Ranges', 'bytes');

static::output($model, $app);
}
}