diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index 96fe825f..5ce2865c 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -721,6 +721,7 @@ class PostController { $this->context->layoutName = 'layout-file'; $post = self::locatePost($name); + R::preload($post, ['tag']); PrivilegesHelper::confirmWithException($this->context->user, Privilege::RetrievePost); PrivilegesHelper::confirmWithException($this->context->user, Privilege::RetrievePost, PostSafety::toString($post->safety)); @@ -731,6 +732,16 @@ class PostController if (!is_readable($path)) throw new SimpleException('Post file is not readable'); + $ext = substr($post->orig_name, strrpos($post->orig_name, '.') + 1); + if (strpos($post->orig_name, '.') === false) + $ext = '.dat'; + $fn = sprintf('%s_%s_%s.%s', + $this->config->main->title, + $post->id, join(',', array_map(function($tag) { return $tag->name; }, $post->sharedTag)), + $ext); + $fn = preg_replace('/[[:^print:]]/', '', $fn); + + $this->context->transport->customFileName = $fn; $this->context->transport->mimeType = $post->mimeType; $this->context->transport->filePath = $path; } diff --git a/src/Views/layout-file.phtml b/src/Views/layout-file.phtml index 4a89de1d..e4ea92e6 100644 --- a/src/Views/layout-file.phtml +++ b/src/Views/layout-file.phtml @@ -6,7 +6,17 @@ if (!empty($this->context->transport->errorMessage)) } else { - \Chibi\HeadersHelper::set('Content-Type', $this->context->transport->mimeType); + if (isset($this->context->transport->customFileName)) + { + \Chibi\HeadersHelper::set( + 'Content-Disposition', + 'inline; filename=' . $this->context->transport->customFileName); + } + + \Chibi\HeadersHelper::set( + 'Content-Type', + $this->context->transport->mimeType); + readfile($this->context->transport->filePath); flush(); }