public function getMediaType() { if ($this->info->isDir()) { return new MediaType('httpd/unix-directory'); } return new MediaType(Filesystem::guessMimeTypeFromFilename($this->info->getFilename())); }
/** * {@inheritdoc} */ public function processSource($contents, FileSource $source) { $file = $source->getFileInfo(); $contents = preg_replace_callback("'(\\Wurl\\s*\\()([^\\)]+)\\)'i", function (array $m) use($file) { $url = trim($m[2], '"\''); if (preg_match("'^[^:]+://.+'", $url)) { return $m[0]; } $base = dirname($file->getPathname()); $loc = $base . '/' . ltrim($url, '/\\'); $loc = preg_replace("'[^/]+/\\.\\./'", '', $loc); $params = []; if (false !== strpos($loc, '?')) { list($loc, $tmp) = explode('?', $loc, 2); parse_str($tmp, $params); } if ($this->publisher->isPublic($loc)) { return $m[1] . "'" . $this->publisher->getResourceUri($loc, $params) . "')"; } if (!is_file($loc)) { throw new \OutOfBoundsException(sprintf('Resource "%s" referenced by "%s" was not found', $loc, $file->getPathname())); } if (false === ($embed = @file_get_contents($loc))) { throw new \OutOfBoundsException(sprintf('Unable to load contents of resource "%s" embedded into "%s"', $loc, $file->getPathname())); } return sprintf("%s'data:%s;base64,%s')", $m[1], Filesystem::guessMimeTypeFromFilename($loc), base64_encode($embed)); }, $contents); return $contents; }
/** * {@inheritdoc} */ public function prepareMessage(HttpMessage $message) : HttpMessage { if (!$message->hasHeader('Content-Type')) { $message = $message->withHeader('Content-Type', Filesystem::guessMimeTypeFromFilename($this->file)); } return $message; }
public function __construct($filename, StreamInterface $stream, $mediaType = NULL) { if (!$stream->isReadable()) { throw new \InvalidArgumentException(sprintf('Input stream of file "%s" must be readable')); } $this->fileName = new UnicodeString($filename); $this->stream = $stream; $this->mediaType = $mediaType === NULL ? Filesystem::guessMimeTypeFromFilename($this->fileName) : new MediaType($mediaType); }
public function prepare(HttpMessage $message) { parent::prepare($message); if (!$message->hasHeader('Content-Type')) { $message->setHeader('Content-Type', Filesystem::guessMimeTypeFromFilename($this->file->getPathname())); } if ($this->download) { $message->setHeader('Content-Disposition', sprintf('attachement; filename="%s"', str_replace(['\'', '"'], '', $this->file->getFilename()))); } }
/** * Create HTTP file response. * * @param string $file Absolute path of the file to be transfered. * @param string $type Media type of the file (will be guessed from extension if not provided). * @param string $charset Charset to be supplied when media type is text format. */ public function __construct(string $file, string $type = null, string $charset = null) { $type = new ContentType($type ?? Filesystem::guessMimeTypeFromFilename($file)); if ($type->getMediaType()->isText()) { $type->setParam('charset', $charset ?? 'utf-8'); } parent::__construct(Http::OK, ['Content-Type' => (string) $type]); $this->body = new FileBody($file); $this->file = $file; }
public function process(DispatchRequest $dispatch) { if (!$dispatch->isMaster()) { return $dispatch->proceed(); } $request = $dispatch->getHttpRequest(); $path = $request->getPathInfo(); $m = NULL; if (!preg_match("'^_res/+(.+)\$'i", $path, $m)) { return $dispatch->proceed(); } $path = $m[1]; if ('app/' === substr($path, 0, 4)) { $resource = 'k2://app/' . substr($path, 4); } else { $parts = explode('/', $path, 2); if (count($parts) !== 2) { return new HttpResponse(Http::CODE_NOT_FOUND); } $resource = 'k2://' . $parts[0] . '/' . $parts[1]; } if (!is_file($resource)) { return new HttpResponse(Http::CODE_NOT_FOUND); } if (!$this->publisher->isPublic($resource)) { return new HttpResponse(Http::CODE_FORBIDDEN); } $response = new HttpResponse(); // Conditional caching: $etag = sprintf('"%x-%x"', filemtime($resource), filesize($resource)); $response->setHeader('Access-Control-Allow-Origin', '*'); $response->setHeader('Cache-Control', 'public, max-age=7200'); $response->setHeader('ETag', $etag); $response->setHeader(new ExpiresHeader(new \DateTimeImmutable('@' . (time() + 7200)))); if ($etag === $request->getHeader('If-None-Match', '')) { $response->setStatus(Http::CODE_NOT_MODIFIED); return $response; } $mediaType = new MediaType(Filesystem::guessMimeTypeFromFilename($resource)); $response->setHeader('X-Content-Type-Options', 'nosniff'); if ($mediaType->isType('text')) { $response->setHeader('Content-Type', $mediaType . '; charset="utf-8"'); } else { $response->setHeader('Content-Type', (string) $mediaType); } $response->setEntity(new FileEntity(new \SplFileInfo($resource))); return $response; }
public function createResource(ResourceInterface $parent, $name, StreamInterface $stream) { $id = $this->storeResource($parent, $name); $size = 0; $md5 = NULL; $this->storeResourceContents($id, $stream, $size, $md5); $this->conn->insert('#__webdav_file', ['resource_id' => $id, 'size' => $size, 'md5' => new StringStream(hex2bin($md5)), 'media_type' => Filesystem::guessMimeTypeFromFilename($name)]); return $this->getByIdentifier($id); }
public function render(ViewModelInterface $model, $onlyEntity = false) { $m = NULL; if (!preg_match("'.+\\.([a-z0-9]+)\\.xml\$'i", $model->getResource(), $m)) { throw new \RuntimeException(sprintf('Unable to determine output format of view "%s"', $model->getResource())); } $typeName = $this->factory->createView($this, $model->getResource()); $view = new $typeName($this->factory, $this); $response = new HttpResponse(); $format = strtolower($m[1]); $out = $view->render(new OutputBuffer(), $model, ['@format' => $format, '@response' => $response]); if ($onlyEntity) { return (string) $out; } if (!$response->hasHeader('Content-Type')) { $type = new MediaType(Filesystem::guessMimeTypeFromFilename($format)); if ($type->isText()) { $response->setHeader('Content-Type', $type . '; charset="utf-8"'); } else { $response->setHeader('Content-Type', $type); } if ($type->is('*/xml')) { $out = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $out; } } return $response->setEntity(ltrim($out)); }
public function __construct(string $file, string $type = null, array $vars = []) { $this->file = $file; $this->type = $type ?? Filesystem::guessMimeTypeFromFilename($file, 'text/plain'); $this->vars = $vars; }