nextcloud/lib/public/AppFramework/Http/DownloadResponse.php
Christoph Wurst d1d24e65a4 fix(http): avoid iconv for header ascii fallback
iconv transliteration is locale- and config-dependent and fails silently
on some setups. UnicodeString::ascii() from symfony/string uses a built-in
transliteration table backed by symfony/polyfill-intl-normalizer, so it
works on all setups without requiring optional PHP extensions.

Assisted-by: Claude:claude-sonnet-4-6
Signed-off-by: Christoph Wurst <1374172+ChristophWurst@users.noreply.github.com>
2026-05-22 14:58:14 +02:00

39 lines
1.4 KiB
PHP

<?php
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCP\AppFramework\Http;
use OCP\AppFramework\Http;
use Symfony\Component\HttpFoundation\HeaderUtils;
use Symfony\Component\String\UnicodeString;
/**
* Prompts the user to download the a file
* @since 7.0.0
* @template-covariant S of Http::STATUS_*
* @template-covariant C of string
* @template-covariant H of array<string, mixed>
* @template-extends Response<Http::STATUS_*, array<string, mixed>>
*/
class DownloadResponse extends Response {
/**
* Creates a response that prompts the user to download the file
* @param string $filename the name that the downloaded file should have
* @param C $contentType the mimetype that the downloaded file should have
* @param S $status
* @param H $headers
* @since 7.0.0
*/
public function __construct(string $filename, string $contentType, int $status = Http::STATUS_OK, array $headers = []) {
parent::__construct($status, $headers);
$sanitized = str_replace(['/', '\\'], '-', $filename);
$fallback = str_replace('%', '', (new UnicodeString($sanitized))->ascii()->toString());
$this->addHeader('Content-Disposition', HeaderUtils::makeDisposition(HeaderUtils::DISPOSITION_ATTACHMENT, $sanitized, $fallback));
$this->addHeader('Content-Type', $contentType);
}
}