public static function prepareAttachmentHeaders(
        ResponseInterface $response,
        ?string $filename = null
    ): ResponseInterface {
        $response = $response->withHeader('Content-Type', 'application/octet-stream')
            ->withHeader('Cache-Control', 'no-store, no-cache, must-revalidate')
            ->withHeader('Content-Transfer-Encoding', 'binary')
            ->withHeader('Content-Encoding', 'none');

        if ($filename !== null) {
            $response = $response->withHeader(
                'Content-Disposition',
                'attachment; filename*=utf-8\'\'' . rawurlencode($filename)
            );
        }

        return $response;
    }