<commit_before><?php
namespace packages\base\response;
use \packages\base\IO;
class file{
	private $stream;
	private $location;
	private $mimeType;
	private $size;
	private $name;
	public function setStream($stream){
		if(is_resource($stream)){
			$this->stream = $stream;
		}else{
			throw new \Exception;
		}
	}
	public function setLocation($location){
		if(is_file($location) and is_readable($location)){
			$this->location = $location;
		}
	}
	public function setSize($size){
		if($size > 0){
			$this->size = $size;
		}
	}
	public function setName($name){
		if($name){
			$this->name = $name;
		}
	}
	public function setMimeType($type){
		$this->type = $type;
	}
	public function getStream(){
		if(is_resource($this->stream)){
			return $this->stream;
		}elseif($this->location){
			return fopen($this->location, 'r');
		}
	}
	public function getSize(){
		if($this->size){
			return $this->size;
		}elseif($this->location){
			return filesize($this->location);
		}
	}
	public function getName(){
		if($this->name){
			return $this->name;
		}elseif($this->location){
			return basename($this->location);
		}
	}
	public function getMimeType(){
		if($this->mimeType){
			return $this->mimeType;
		}elseif($this->getName()){
			return IO\mime_type($this->getName());
		}
	}
	public function output(){
		$stream = $this->getStream();
		$size = $this->size;
		$readed = 0;
		while ($size > $readed ) {
			$buffer = fread($stream, 8192);
			$readed += strlen($buffer);
			echo $buffer;
		}
		fclose($stream);
	}
}<commit_msg>accept io\file object as file location in response\file<commit_after><?php
namespace packages\base\response;

use packages\base\{IO, IO\Buffer, Exception};

class File {
	/** @var packages\base\IO\buffer|null */
	private $stream;

	/** @var packages\base\IO\File|null */
	private $location;

	private $mimeType;
	private $size;
	private $name;

	/**
	 * Set a resource for response.
	 * 
	 * @param packages\base\IO\buffer|resource|null $stream
	 * @throws packages\base\Exception if passed argument was not resource nor null
	 * @return void
	 */
	public function setStream($stream): void {
		if (is_resource($stream)) {
			$stream = new Buffer($stream);
		}
		if (!is_null($stream) and !($stream instanceof Buffer)) {
			throw new Exception("argument 1 passed must be a resource or " . Buffer::class . " instance");
		}
		$this->stream = $stream;
	}


	/**
	 * Get setted buffer or new read-only buffer of file.
	 * 
	 * @return packages\base\IO\Buffer|null
	 */
	public function getStream(): ?Buffer {
		if ($this->stream) {
			return $this->stream;
		}
		if ($this->location and ($this->location instanceof IO\File\Local or $this->location instanceof IO\File\sftp)) {
			return $this->stream = $this->location->open('r');
			return $this->stream;
		}
	}

	/**
	 * Set a file location or file object for response
	 * 
	 * @param packages\base\IO\File|string|null $location
	 * @throws packages\base\Exception for unkonwn arguments
	 * @throws packages\base\IO\NotFoundException if file wasn't exist.
	 * @return void
	 */
	public function setLocation($location): void {
		if (is_string($location)) {
			$location = new IO\File\Local($location);
		}
		if (!is_null($location) and !($location instanceof IO\File)) {
			throw new Exception("argument 1 passed must be a string or " . IO\File::class . " instance");
		}
		if (!is_null($location)) {
			if (!$location->exists()) {
				throw new IO\NotFoundException($location);
			}
		}
		$this->location = $location;
	}

	/**
	 * Getter for file object
	 * 
	 * @return packages\base\IO\File|null
	 */
	public function getLocation(): ?IO\File {
		return $this->location;
	}

	/**
	 * Set original size of stream or file
	 * 
	 * @param int|null $size
	 * @return void
	 */
	public function setSize(?int $size): void {
		$this->size = $size;
	}

	/**
	 * Get setted size or calculated size of file
	 * 
	 * @return int|null
	 */
	public function getSize(): ?int {
		if ($this->size) {
			return $this->size;
		}
		if ($this->location) {
			return $this->location->size();
		}
	}

	/**
	 * Set original name of stream or file
	 * 
	 * @param string|null $name
	 * @return void
	 */
	public function setName(?string $name): void {
		$this->name = $name;
	}

	/**
	 * Get setted name or original name of file
	 * 
	 * @return string|null
	 */
	public function getName(): ?string {
		if ($this->name) {
			return $this->name;
		}
		if ($this->location) {
			return $this->location->basename;
		}
	}

	/**
	 * Set mime-type of stream or file
	 * 
	 * @param string $type
	 * @return void
	 */
	public function setMimeType(string $type): void {
		$this->mimeType = $type;
	}

	/**
	 * Get setted mime-type or dettected type of name
	 */
	public function getMimeType(): ?string {
		if ($this->mimeType) {
			return $this->mimeType;
		}
		if ($this->getName()) {
			return IO\mime_type($this->getName());
		}
	}

	/**
	 * Echo output of stream or file.
	 * 
	 * @return void
	 */
	public function output(): void {
		$stream = $this->getStream();
		if ($stream) {
			$size = $this->getSize();
			$read = 0;
			$chunkSize = 8192;
			$lastRead = $chunkSize;
			while ((!$size and $lastRead == $lastRead) or ($size and $read < $size)) {
				$chunk = $stream->read($chunkSize);
				$lastRead = strlen($chunk);
				$read += $lastRead;
				echo $chunk;
			}
		} elseif ($this->location) {
			echo $this->location->read(); 
		}
	}
}
{'fraction_non_alphanumeric': 0.16691422205718529, 'fraction_numerical': 0.0024136650575566285, 'mean_word_length': 3.330385852090032, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 64, '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': '4434541', 'n_tokens_mistral': 2029, 'n_tokens_neox': 1778, 'n_words': 462}