/** * @param int $mediaId * @param FileVersion $fileVersion * @param string $locale * * @return array */ protected function getPreviewsFromFileVersion($mediaId, $fileVersion, $locale) { $title = ''; /* * @var FileVersionMeta */ foreach ($fileVersion->getMeta() as $key => $meta) { if ($meta->getLocale() == $locale) { $title = $meta->getTitle(); break; } elseif ($key == 0) { // fallback title $title = $meta->getTitle(); } } $mediaFormats = $this->formatManager->getFormats($mediaId, $fileVersion->getName(), $fileVersion->getStorageOptions(), $fileVersion->getVersion(), $fileVersion->getMimeType()); foreach ($mediaFormats as $formatName => $formatUrl) { if ($formatName == $this->collectionPreviewFormat) { return ['url' => $formatUrl, 'title' => $title]; break; } } return []; }
/** * @param FileVersion $fileVersion * @param string $locale * @param string $dispositionType * * @return BinaryFileResponse */ protected function getFileResponse($fileVersion, $locale, $dispositionType = ResponseHeaderBag::DISPOSITION_ATTACHMENT) { $cleaner = $this->get('sulu.content.path_cleaner'); $fileName = $fileVersion->getName(); $fileSize = $fileVersion->getSize(); $storageOptions = $fileVersion->getStorageOptions(); $mimeType = $fileVersion->getMimeType(); $version = $fileVersion->getVersion(); $path = $this->getStorage()->load($fileName, $version, $storageOptions); $response = new BinaryFileResponse($path); $pathInfo = pathinfo($fileName); // Prepare headers $disposition = $response->headers->makeDisposition($dispositionType, $fileName, $cleaner->cleanup($pathInfo['filename'], $locale) . '.' . $pathInfo['extension']); // Set headers $response->headers->set('Content-Type', !empty($mimeType) ? $mimeType : 'application/octet-stream'); $response->headers->set('Content-Disposition', $disposition); $response->headers->set('Content-length', $fileSize); return $response; }
/** * @param FileVersion $fileVersion * @param string $locale * @param string $dispositionType * * @return StreamedResponse */ protected function getFileResponse($fileVersion, $locale, $dispositionType = ResponseHeaderBag::DISPOSITION_ATTACHMENT) { $cleaner = $this->get('sulu.content.path_cleaner'); $fileName = $fileVersion->getName(); $fileSize = $fileVersion->getSize(); $storageOptions = $fileVersion->getStorageOptions(); $mimeType = $fileVersion->getMimeType(); $version = $fileVersion->getVersion(); $path = $this->getStorage()->load($fileName, $version, $storageOptions); $response = new StreamedResponse(function () use($path) { flush(); // send headers $handle = fopen($path, 'r'); while (!feof($handle)) { $buffer = fread($handle, 1024); echo $buffer; flush(); // buffered output } fclose($handle); }); $pathInfo = pathinfo($fileName); // Prepare headers $disposition = $response->headers->makeDisposition($dispositionType, $fileName, $cleaner->cleanup($pathInfo['filename'], $locale) . '.' . $pathInfo['extension']); // Set headers $response->headers->set('Content-Type', !empty($mimeType) ? $mimeType : 'application/octet-stream'); $response->headers->set('Content-Disposition', $disposition); $response->headers->set('Content-length', $fileSize); return $response; }
/** * Purges a file-version of a media with a given id. * * @param int $mediaId * @param FileVersion $fileVersion */ private function purgeMedia($mediaId, FileVersion $fileVersion) { $this->formatManager->purge($mediaId, $fileVersion->getName(), $fileVersion->getStorageOptions()); }
/** * {@inheritdoc} */ public function convert(FileVersion $fileVersion, $formatKey) { $content = $this->storage->loadAsString($fileVersion->getName(), $fileVersion->getVersion(), $fileVersion->getStorageOptions()); $extractedImage = $this->mediaImageExtractor->extract($content); try { $image = $this->imagine->load($extractedImage); } catch (RuntimeException $e) { throw new InvalidFileTypeException($e->getMessage()); } $image = $this->toRGB($image); $format = $this->getFormat($formatKey); $cropParameters = $this->getCropParameters($image, $fileVersion->getFormatOptions()->get($formatKey), $this->formats[$formatKey]); if (isset($cropParameters)) { $image = $this->applyFormatCrop($image, $cropParameters); } if (isset($format['scale']) && $format['scale']['mode'] !== ImageInterface::THUMBNAIL_INSET) { $image = $this->applyFocus($image, $fileVersion, $format['scale']); } if (isset($format['scale'])) { $image = $this->applyScale($image, $format['scale']); } if (isset($format['transformations'])) { $image = $this->applyTransformations($image, $format['transformations']); } $image->strip(); // Set Interlacing to plane for smaller image size. if (count($image->layers()) == 1) { $image->interlace(ImageInterface::INTERLACE_PLANE); } $imagineOptions = $format['options']; $imageExtension = $this->getImageExtension($fileVersion->getName()); return $image->get($imageExtension, $this->getOptionsFromImage($image, $imageExtension, $imagineOptions)); }