<commit_before><?php declare(strict_types=1);

/**
 * ownCloud - Music app
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author <NAME> <<EMAIL>>
 * @copyright <NAME> 2021, 2022
 */

namespace OCA\Music\Http;

use OCP\AppFramework\Http\ICallbackResponse;
use OCP\AppFramework\Http\IOutput;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http;

/**
 * A renderer for files
 */
class FileStreamResponse extends Response implements ICallbackResponse {
	private $file;
	private $start;
	private $end;

	/**
	 * @param \OCP\Files\File $file file
	 */
	public function __construct($file) {

		$this->file = $file;
		$mime = $file->getMimetype();
		$size = $file->getSize();

		$this->addHeader('Content-type', "$mime; charset=utf-8");

		if (isset($_SERVER['HTTP_RANGE'])) {
			// Note that we do not support Range Header of the type
			// bytes=x-y,z-w
			if (!\preg_match('/^bytes=\d*-\d*$/', $_SERVER['HTTP_RANGE'])) {
				$this->addHeader('Content-Range', 'bytes */' . $size);
				$this->setStatus(Http::STATUS_REQUEST_RANGE_NOT_SATISFIABLE);
			} else {
				$parts = \explode('-', \substr($_SERVER['HTTP_RANGE'], 6));
				$this->start = $parts[0] != '' ? (int)$parts[0] : 0;
				$this->end = $parts[1] != '' ? (int)$parts[1] : $size - 1;
				$this->end = \min($this->end, $size - 1);

				if ($this->start > $this->end) {
					$this->addHeader('Content-Range', 'bytes */' . $size);
					$this->setStatus(Http::STATUS_REQUEST_RANGE_NOT_SATISFIABLE);
				} else {
					$this->addHeader('Accept-Ranges', 'bytes');
					$this->addHeader(
						'Content-Range', 'bytes ' .
						$this->start . '-' .
						$this->end . '/' . $size
					);
					$this->addHeader('Content-Length', $this->end - $this->start + 1);
					$this->setStatus(Http::STATUS_PARTIAL_CONTENT);
				}
			}
		} else {
			$this->start = 0;
			$this->end = $size - 1;
			$this->addHeader('Content-Length', $size);
			$this->setStatus(Http::STATUS_OK);
		}
	}

	public function callback(IOutput $output) {
		$status = $this->getStatus();

		if ($status === Http::STATUS_OK || $status === Http::STATUS_PARTIAL_CONTENT) {
			$fp = $this->file->fopen('r') ?? null;

			if (!is_resource($fp)) {
				$status = Http::STATUS_NOT_FOUND;
			} else {
				if ($this->streamDataToOutput($fp) === false) {
					$status = Http::STATUS_BAD_REQUEST;
				}
				\fclose($fp);
			}
		}

		$output->setHttpResponseCode($status);
	}<commit_msg>Fix a type issue detected by PhpStan<commit_after><?php declare(strict_types=1);

/**
 * ownCloud - Music app
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author <NAME> <<EMAIL>>
 * @copyright <NAME> 2021, 2022
 */

namespace OCA\Music\Http;

use OCP\AppFramework\Http\ICallbackResponse;
use OCP\AppFramework\Http\IOutput;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http;

/**
 * A renderer for files
 */
class FileStreamResponse extends Response implements ICallbackResponse {
	private $file;
	private $start;
	private $end;

	/**
	 * @param \OCP\Files\File $file file
	 */
	public function __construct($file) {

		$this->file = $file;
		$mime = $file->getMimetype();
		$size = $file->getSize();

		$this->addHeader('Content-type', "$mime; charset=utf-8");

		if (isset($_SERVER['HTTP_RANGE'])) {
			// Note that we do not support Range Header of the type
			// bytes=x-y,z-w
			if (!\preg_match('/^bytes=\d*-\d*$/', $_SERVER['HTTP_RANGE'])) {
				$this->addHeader('Content-Range', 'bytes */' . $size);
				$this->setStatus(Http::STATUS_REQUEST_RANGE_NOT_SATISFIABLE);
			} else {
				$parts = \explode('-', \substr($_SERVER['HTTP_RANGE'], 6));
				$this->start = $parts[0] != '' ? (int)$parts[0] : 0;
				$this->end = $parts[1] != '' ? (int)$parts[1] : $size - 1;
				$this->end = \min($this->end, $size - 1);

				if ($this->start > $this->end) {
					$this->addHeader('Content-Range', 'bytes */' . $size);
					$this->setStatus(Http::STATUS_REQUEST_RANGE_NOT_SATISFIABLE);
				} else {
					$this->addHeader('Accept-Ranges', 'bytes');
					$this->addHeader(
						'Content-Range', 'bytes ' .
						$this->start . '-' .
						$this->end . '/' . $size
					);
					$this->addHeader('Content-Length', (string)($this->end - $this->start + 1));
					$this->setStatus(Http::STATUS_PARTIAL_CONTENT);
				}
			}
		} else {
			$this->start = 0;
			$this->end = $size - 1;
			$this->addHeader('Content-Length', (string)$size);
			$this->setStatus(Http::STATUS_OK);
		}
	}

	public function callback(IOutput $output) {
		$status = $this->getStatus();

		if ($status === Http::STATUS_OK || $status === Http::STATUS_PARTIAL_CONTENT) {
			$fp = $this->file->fopen('r') ?? null;

			if (!is_resource($fp)) {
				$status = Http::STATUS_NOT_FOUND;
			} else {
				if ($this->streamDataToOutput($fp) === false) {
					$status = Http::STATUS_BAD_REQUEST;
				}
				\fclose($fp);
			}
		}

		$output->setHttpResponseCode($status);
	}
{'fraction_non_alphanumeric': 0.1991550995775498, 'fraction_numerical': 0.008851337759002213, 'mean_word_length': 3.4432529043789097, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4204693', 'n_tokens_mistral': 2063, 'n_tokens_neox': 1762, 'n_words': 340}