/** * Construct an attachment with a stream and a name. * * @param \Hoa\Stream $stream Stream that contains the attachment. * @param string $name Name of the attachment (if null, will * use the stream basename). * @param string $mimeType Force a MIME type. * @return void */ public function __construct(Stream $stream, $name = null, $mimeType = null) { parent::__construct(); if (null === $name) { if ($stream instanceof Stream\IStream\Pathable) { $name = $stream->getBasename(); } else { $name = basename($stream->getStreamName()); } } if (null === $mimeType) { $mimeType = null; try { $mime = new Mime($stream); $mimeType = $mime->getMime(); } catch (Mime\Exception $e) { } if (null === $mimeType) { $mimeType = 'application/octet-stream'; } } $size = null; if ($stream instanceof Stream\IStream\Statable && false !== ($_size = $stream->getSize())) { $size = '; size=' . $_size; } $this['content-type'] = $mimeType; $this['content-disposition'] = 'attachment; filename="' . str_replace('"', '-', $name) . '"' . $size; $this->setStream($stream); return; }
<?php require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Bootstrap.php'; use Hoa\Dispatcher; use Hoa\File; use Hoa\Mime; use Hoa\Router; $router = new Router\Http(); $router->any('a', '.*', function (Dispatcher\Kit $_this) { $uri = $_this->router->getURI(); $file = __DIR__ . DS . $uri; if (!empty($uri) && true === file_exists($file)) { $stream = new File\Read($file); try { $mime = new Mime($stream); $_mime = $mime->getMime(); } catch (Mime\Exception $e) { $_mime = 'text/plain'; } header('Content-Type: ' . $_mime); echo $stream->readAll(); return; } require 'index.php'; }); $dispatcher = new Dispatcher\Basic(); $dispatcher->dispatch($router);
/** * Triggered by a `PROPFIND` or `GET`. The goal is to set an appropriated * Content-Type. * * @param SabreDav\PropFind $propFind PropFind object. * @param SabreDav\INode $node Node. */ function propFind(SabreDav\PropFind $propFind, SabreDav\INode $node) { $propFind->handle('{DAV:}getcontenttype', function () use($propFind) { Mime::compute('katana://resource/mime.types'); $extension = pathinfo($propFind->getPath(), PATHINFO_EXTENSION); return Mime::getMimeFromExtension($extension); }); }
public function case_stream() { $this->given($file = 'hoa://Test/Vfs/index.html')->when($type = new LUT(new File\Read($file)))->then->string($type->getExtension())->isEqualTo('html')->array($type->getOtherExtensions())->isEqualTo([0 => 'htm'])->string($type->getMime())->isEqualTo('text/html')->boolean($type->isExperimental())->isFalse(); }