<?php
namespace Engine\Mvc\Response;

/**
 * Response used for file-downloads
 * This object should be used for large downloads and the download file should exist on disk
 * @author Oana
 */
class BigFileDownload extends GeneralAbstract
{
	// TODO: rename BigFileDownload to 	FileDownloadFromDisk, or split it into DownloadTemporaryFileFromDisk and DownloadPermanentFileFromDisk
	// TODO: rename FileDownload to 	FileDownloadFromMemory
	// TODO: rename File to 			FileOnDisk
	// TODO: remove debug options & user_abort options
	// TODO: add option to close session on download, it should probably always be enabled


	/**
	 * Change typehint for body to File
	 * @var \Engine\Mvc\Response\Components\File
	 */
	public $body = null;


	protected $filename = null;
	protected $filesize = null;

	/**
	 * Can be 'gzip', null.
	 * @var String
	 */
	public $compressionMethod = null;

	/**
	 * (non-PHPdoc)
	 * @see \Engine\Mvc\Response\GeneralAbstract::initialize()
	 */
	protected function initialize($filePath, array $headersArr)
	{
		$this->body = new Components\File($filePath);
		$this->headers = new Components\Headers($headersArr);

		$this->generateDownloadHeaders();

		if ($this->body->hasContent()) {
			$this->setFilesize($this->body->get()->getSize());
		}
	}

	/**
	 * Set the file name
	 * @param string $filename
	 */
	public function setFilename($filename)
	{
		$this->filename = $filename;
		// Add file details to download window
		$this->headers->append('Content-Disposition', sprintf('attachment; filename="%s";', $this->filename));
	}

	/**
	 * Set the file size (in bytes)
	 * @param int $filesize
	 */
	private function setFilesize($filesize)
	{
		$this->filesize = $filesize;
		$this->headers->append('Content-Length', $this->filesize);
	}

	/**
	 * Generate the list of headers
	 */
	protected function generateDownloadHeaders()
	{
		// Force download window
// 		$this->headers->append('Content-Type', 'application/force-download'); // TODO: these 3 headers seem to be useless; no need to do this - Content-Disposition does the trick
// 		$this->headers->append('Content-Type', 'application/octet-stream');
// 		$this->headers->append('Content-Type', 'application/download');
	}
}
