Skip to content
Merged
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
22 changes: 21 additions & 1 deletion formwork/src/Http/FileResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Formwork\Http\Utils\Header;
use Formwork\Utils\FileSystem;
use LogicException;
use RuntimeException;

class FileResponse extends Response
Expand Down Expand Up @@ -32,9 +33,10 @@ public function __construct(
protected string $path,
ResponseStatus $responseStatus = ResponseStatus::OK,
array $headers = [],
bool $download = false,
protected bool $download = false,
protected bool $autoEtag = false,
protected bool $autoLastModified = false,
protected bool $deleteAfterSend = false,
) {
$this->fileSize = FileSystem::fileSize($path);

Expand Down Expand Up @@ -101,6 +103,10 @@ public function send(): void
}

$this->flush();

if ($this->deleteAfterSend) {
unlink($this->path);
}
}

public function prepare(Request $request): static
Expand Down Expand Up @@ -160,4 +166,18 @@ public function prepare(Request $request): static

return $this;
}

/**
* Set filename for download
*
* @throws LogicException If the response is not a download response
*/
public function setFilename(string $filename): static
{
if (!$this->download) {
throw new LogicException('Cannot set filename for inline file response');
}
$this->headers->set('Content-Disposition', Header::make(['attachment', 'filename' => $filename]));
return $this;
}
}