Optimized comment

This commit is contained in:
huangzhhui 2019-08-29 16:25:07 +08:00
parent 333c99c18b
commit d43cabb7ec
2 changed files with 11 additions and 1 deletions

View File

@ -46,7 +46,10 @@ interface ResponseInterface
public function redirect(string $toUrl, int $status = 302, string $schema = 'http'): PsrResponseInterface;
/**
* Create a new file download response.
* Create a file download response.
*
* @param string $file The file path which want to send to client.
* @param string $name The alias name of the file that client receive.
*/
public function download(string $file, string $name = ''): PsrResponseInterface;
}

View File

@ -115,6 +115,12 @@ class Response implements PsrResponseInterface, ResponseInterface
return $this->getResponse()->withStatus($status)->withAddedHeader('Location', $toUrl);
}
/**
* Create a file download response.
*
* @param string $file The file path which want to send to client.
* @param string $name The alias name of the file that client receive.
*/
public function download(string $file, string $name = ''): PsrResponseInterface
{
$file = new \SplFileInfo($file);
@ -140,6 +146,7 @@ class Response implements PsrResponseInterface, ResponseInterface
return $this->withHeader('content-description', 'File Transfer')
->withHeader('content-type', value(function () use ($file) {
$mineType = null;
if (ApplicationContext::hasContainer()) {
$guesser = ApplicationContext::getContainer()->get(MimeTypeExtensionGuesser::class);
$mineType = $guesser->guessMimeType($file->getExtension());