<?php

	namespace Mocha\UI\Pages;
	
	use Phast\RenderingEventArgs;
	use Phast\System;
	use Phast\WebPage;

	class AssetPage extends WebPage
	{
		protected function OnInitializing(\Phast\CancelEventArgs $e)
		{
			header("Content-Type: text/plain");
			
			$package = $this->Page->GetPathVariableValue("package");
			$version = $this->Page->GetPathVariableValue("version");
			$path = $this->Page->GetPathVariableValue("path");

			$filename = "/usr/share/mocha/assets/" . $package . "/" . $version . "/" . $path;
			if (file_exists($filename))
			{
				header("HTTP/1.1 200 OK");
				header("Content-Type: " . mime_content_type($filename));
				readfile($filename);
			}
			else if (file_exists($filename . ".png"))
			{
				header("HTTP/1.1 200 OK");
				header("Content-Type: " . mime_content_type($filename . ".png"));
				readfile($filename . ".png");
			}
			else if (file_exists($filename . ".svg"))
			{
				header("HTTP/1.1 200 OK");
				header("Content-Type: " . mime_content_type($filename . ".svg"));
				readfile($filename . ".svg");
			}
			else
			{
				header ("HTTP/1.1 404 Not Found");
				echo ("not found!");
				echo ("package: " . $package . "\n");
				echo ("version: " . $version . "\n");
				echo ("path: " . $path . "\n");
			}
			$e->Cancel = true;
		}
	}

?>