| @@ -40,14 +40,14 @@ discard block | ||
| 40 | 40 | use FOS\RestBundle\Controller\Annotations\Route; | 
| 41 | 41 | |
| 42 | 42 |  class ContentPushController extends AbstractController { | 
| 43 | - private EventDispatcherInterface $eventDispatcher; | |
| 44 | - private FormFactoryInterface $formFactory; | |
| 45 | - private MessageBusInterface $messageBus;//swp_multi_tenancy.tenant_context | |
| 46 | - private DataTransformerInterface $dataTransformer; // swp_bridge.transformer.json_to_package | |
| 47 | - private MediaManagerInterface $mediaManager; // swp_content_bundle.manager.media | |
| 48 | - private EntityManagerInterface $entityManager; // swp.object_manager.media | |
| 49 | - private PackageRepository $packageRepository;//swp.repository.package | |
| 50 | - private FileProviderInterface $fileProvider; | |
| 43 | + private EventDispatcherInterface $eventDispatcher; | |
| 44 | + private FormFactoryInterface $formFactory; | |
| 45 | + private MessageBusInterface $messageBus;//swp_multi_tenancy.tenant_context | |
| 46 | + private DataTransformerInterface $dataTransformer; // swp_bridge.transformer.json_to_package | |
| 47 | + private MediaManagerInterface $mediaManager; // swp_content_bundle.manager.media | |
| 48 | + private EntityManagerInterface $entityManager; // swp.object_manager.media | |
| 49 | + private PackageRepository $packageRepository;//swp.repository.package | |
| 50 | + private FileProviderInterface $fileProvider; | |
| 51 | 51 | |
| 52 | 52 | /** | 
| 53 | 53 | * @param EventDispatcherInterface $eventDispatcher | 
| @@ -59,11 +59,11 @@ discard block | ||
| 59 | 59 | * @param PackageRepository $packageRepository | 
| 60 | 60 | * @param FileProviderInterface $fileProvider | 
| 61 | 61 | */ | 
| 62 | - public function __construct(EventDispatcherInterface $eventDispatcher, FormFactoryInterface $formFactory, | |
| 63 | - MessageBusInterface $messageBus, | |
| 64 | - DataTransformerInterface $dataTransformer, MediaManagerInterface $mediaManager, | |
| 65 | - EntityManagerInterface $entityManager, PackageRepository $packageRepository, | |
| 66 | -                              FileProviderInterface    $fileProvider) { | |
| 62 | + public function __construct(EventDispatcherInterface $eventDispatcher, FormFactoryInterface $formFactory, | |
| 63 | + MessageBusInterface $messageBus, | |
| 64 | + DataTransformerInterface $dataTransformer, MediaManagerInterface $mediaManager, | |
| 65 | + EntityManagerInterface $entityManager, PackageRepository $packageRepository, | |
| 66 | +                                FileProviderInterface    $fileProvider) { | |
| 67 | 67 | $this->eventDispatcher = $eventDispatcher; | 
| 68 | 68 | $this->formFactory = $formFactory; | 
| 69 | 69 | $this->messageBus = $messageBus; | 
| @@ -72,13 +72,13 @@ discard block | ||
| 72 | 72 | $this->entityManager = $entityManager; | 
| 73 | 73 | $this->packageRepository = $packageRepository; | 
| 74 | 74 | $this->fileProvider = $fileProvider; | 
| 75 | - } | |
| 75 | + } | |
| 76 | 76 | |
| 77 | 77 | |
| 78 | - /** | |
| 79 | -   * @Route("/api/{version}/content/push", methods={"POST"}, options={"expose"=true}, defaults={"version"="v2"}, name="swp_api_content_push") | |
| 80 | - */ | |
| 81 | -  public function pushContentAction(Request $request, TenantContextInterface $tenantContext): SingleResourceResponseInterface { | |
| 78 | + /** | |
| 79 | +     * @Route("/api/{version}/content/push", methods={"POST"}, options={"expose"=true}, defaults={"version"="v2"}, name="swp_api_content_push") | |
| 80 | + */ | |
| 81 | +    public function pushContentAction(Request $request, TenantContextInterface $tenantContext): SingleResourceResponseInterface { | |
| 82 | 82 | $package = $this->dataTransformer->transform($request->getContent()); | 
| 83 | 83 | $this->eventDispatcher->dispatch(new GenericEvent($package), Events::SWP_VALIDATION); | 
| 84 | 84 | |
| @@ -87,27 +87,27 @@ discard block | ||
| 87 | 87 | $this->messageBus->dispatch(new ContentPushMessage($currentTenant->getId(), $request->getContent())); | 
| 88 | 88 | |
| 89 | 89 | return new SingleResourceResponse(['status' => 'OK'], new ResponseContext(201)); | 
| 90 | - } | |
| 90 | + } | |
| 91 | 91 | |
| 92 | - /** | |
| 93 | -   * @Route("/api/{version}/assets/push", methods={"POST"}, options={"expose"=true}, defaults={"version"="v2"}, name="swp_api_assets_push") | |
| 94 | - */ | |
| 95 | -  public function pushAssetsAction(Request $request): SingleResourceResponseInterface { | |
| 92 | + /** | |
| 93 | +     * @Route("/api/{version}/assets/push", methods={"POST"}, options={"expose"=true}, defaults={"version"="v2"}, name="swp_api_assets_push") | |
| 94 | + */ | |
| 95 | +    public function pushAssetsAction(Request $request): SingleResourceResponseInterface { | |
| 96 | 96 |      $form = $this->formFactory->createNamed('', MediaFileType::class); | 
| 97 | 97 | $form->handleRequest($request); | 
| 98 | 98 | |
| 99 | 99 |      if ($form->isSubmitted() && $form->isValid()) { | 
| 100 | - $mediaManager = $this->mediaManager; | |
| 101 | - $uploadedFile = $form->getData()['media']; | |
| 102 | -      $mediaId = $request->request->get('mediaId'); | |
| 100 | + $mediaManager = $this->mediaManager; | |
| 101 | + $uploadedFile = $form->getData()['media']; | |
| 102 | +        $mediaId = $request->request->get('mediaId'); | |
| 103 | 103 | |
| 104 | -      if ($uploadedFile->isValid()) { | |
| 104 | +        if ($uploadedFile->isValid()) { | |
| 105 | 105 | $fileProvider = $this->fileProvider; | 
| 106 | 106 | $file = $fileProvider->getFile(ArticleMedia::handleMediaId($mediaId), $uploadedFile->guessExtension()); | 
| 107 | 107 | |
| 108 | 108 |          if (null === $file) { | 
| 109 | - $file = $mediaManager->handleUploadedFile($uploadedFile, $mediaId); | |
| 110 | - $this->entityManager->flush(); | |
| 109 | + $file = $mediaManager->handleUploadedFile($uploadedFile, $mediaId); | |
| 110 | + $this->entityManager->flush(); | |
| 111 | 111 | } | 
| 112 | 112 | |
| 113 | 113 | return new SingleResourceResponse( | 
| @@ -120,23 +120,23 @@ discard block | ||
| 120 | 120 | ], | 
| 121 | 121 | new ResponseContext(201) | 
| 122 | 122 | ); | 
| 123 | - } | |
| 123 | + } | |
| 124 | 124 | |
| 125 | -      throw new \Exception('Uploaded file is not valid:' . $uploadedFile->getErrorMessage()); | |
| 125 | +        throw new \Exception('Uploaded file is not valid:' . $uploadedFile->getErrorMessage()); | |
| 126 | 126 | } | 
| 127 | 127 | |
| 128 | 128 | return new SingleResourceResponse($form); | 
| 129 | - } | |
| 129 | + } | |
| 130 | 130 | |
| 131 | - /** | |
| 132 | -   * @Route("/api/{version}/assets/{action}/{mediaId}.{extension}", methods={"GET"}, options={"expose"=true}, defaults={"version"="v2"}, requirements={"mediaId"=".+", "action"="get|push"}, name="swp_api_assets_get") | |
| 133 | - */ | |
| 134 | -  public function getAssetsAction(string $mediaId, string $extension): SingleResourceResponseInterface { | |
| 131 | + /** | |
| 132 | +     * @Route("/api/{version}/assets/{action}/{mediaId}.{extension}", methods={"GET"}, options={"expose"=true}, defaults={"version"="v2"}, requirements={"mediaId"=".+", "action"="get|push"}, name="swp_api_assets_get") | |
| 133 | + */ | |
| 134 | +    public function getAssetsAction(string $mediaId, string $extension): SingleResourceResponseInterface { | |
| 135 | 135 | $fileProvider = $this->fileProvider; | 
| 136 | 136 | $file = $fileProvider->getFile(ArticleMedia::handleMediaId($mediaId), $extension); | 
| 137 | 137 | |
| 138 | 138 |      if (null === $file) { | 
| 139 | -      throw new NotFoundHttpException('Media don\'t exist in storage'); | |
| 139 | +        throw new NotFoundHttpException('Media don\'t exist in storage'); | |
| 140 | 140 | } | 
| 141 | 141 | |
| 142 | 142 | $mediaManager = $this->mediaManager; | 
| @@ -148,9 +148,9 @@ discard block | ||
| 148 | 148 | 'mime_type' => MimeTypes::getDefault()->getMimeTypes($file->getFileExtension())[0], | 
| 149 | 149 | 'filemeta' => [], | 
| 150 | 150 | ]); | 
| 151 | - } | |
| 151 | + } | |
| 152 | 152 | |
| 153 | -  protected function getPackageRepository() { | |
| 153 | +    protected function getPackageRepository() { | |
| 154 | 154 | return $this->packageRepository; | 
| 155 | - } | |
| 155 | + } | |
| 156 | 156 | } | 
| @@ -47,7 +47,7 @@ | ||
| 47 | 47 | } | 
| 48 | 48 | } | 
| 49 | 49 | $mim = MimeTypes::getDefault()->getMimeTypes($extension)[0]; | 
| 50 | -                $response->headers->set('Content-Type', $mim .'; charset=UTF-8'); | |
| 50 | +                $response->headers->set('Content-Type', $mim.'; charset=UTF-8'); | |
| 51 | 51 | } | 
| 52 | 52 | } | 
| 53 | 53 | } | 
| @@ -26,138 +26,138 @@ | ||
| 26 | 26 | use Symfony\Component\Routing\Annotation\Route; | 
| 27 | 27 | |
| 28 | 28 |  class StaticThemeAssetsController extends Controller { | 
| 29 | - /** | |
| 30 | - * Directory with assets inside theme. | |
| 31 | - */ | |
| 32 | - const ASSETS_DIRECTORY = 'public'; | |
| 33 | - | |
| 34 | - private TenantAwareThemeContextInterface $tenantAwareThemeContext; | |
| 35 | - private ThemeLoaderInterface $themeLoader; | |
| 36 | - private ThemeRepositoryInterface $themeRepository; | |
| 37 | - | |
| 38 | - /** | |
| 39 | - * @param TenantAwareThemeContextInterface $tenantAwareThemeContext | |
| 40 | - * @param ThemeLoaderInterface $themeLoader | |
| 41 | - * @param ThemeRepositoryInterface $themeRepository | |
| 42 | - */ | |
| 43 | - public function __construct(TenantAwareThemeContextInterface $tenantAwareThemeContext, | |
| 44 | - ThemeLoaderInterface $themeLoader, | |
| 45 | -                              ThemeRepositoryInterface         $themeRepository) { | |
| 29 | + /** | |
| 30 | + * Directory with assets inside theme. | |
| 31 | + */ | |
| 32 | + const ASSETS_DIRECTORY = 'public'; | |
| 33 | + | |
| 34 | + private TenantAwareThemeContextInterface $tenantAwareThemeContext; | |
| 35 | + private ThemeLoaderInterface $themeLoader; | |
| 36 | + private ThemeRepositoryInterface $themeRepository; | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * @param TenantAwareThemeContextInterface $tenantAwareThemeContext | |
| 40 | + * @param ThemeLoaderInterface $themeLoader | |
| 41 | + * @param ThemeRepositoryInterface $themeRepository | |
| 42 | + */ | |
| 43 | + public function __construct(TenantAwareThemeContextInterface $tenantAwareThemeContext, | |
| 44 | + ThemeLoaderInterface $themeLoader, | |
| 45 | +                                ThemeRepositoryInterface         $themeRepository) { | |
| 46 | 46 | $this->tenantAwareThemeContext = $tenantAwareThemeContext; | 
| 47 | 47 | $this->themeLoader = $themeLoader; | 
| 48 | 48 | $this->themeRepository = $themeRepository; | 
| 49 | - } | |
| 49 | + } | |
| 50 | 50 | |
| 51 | 51 | |
| 52 | - /** | |
| 53 | -   * @Route("/{fileName}.{fileExtension}", methods={"GET"}, name="static_theme_assets_root", requirements={"fileName": "sw|manifest|favicon|ads|OneSignalSDKWorker|OneSignalSDKUpdaterWorker|amp-web-push-helper-frame|amp-web-push-permission-dialog"}) | |
| 54 | -   * @Route("/public-{fileName}.{fileExtension}", methods={"GET"}, name="static_theme_assets_root_public", requirements={"fileName"=".+"}) | |
| 55 | -   * @Route("/public/{fileName}.{fileExtension}", methods={"GET"}, name="static_theme_assets_public", requirements={"fileName"=".+"}) | |
| 56 | - */ | |
| 57 | -  public function rootAction($fileName, $fileExtension, ThemeHierarchyProviderInterface $themeHierarchyProvider) { | |
| 52 | + /** | |
| 53 | +     * @Route("/{fileName}.{fileExtension}", methods={"GET"}, name="static_theme_assets_root", requirements={"fileName": "sw|manifest|favicon|ads|OneSignalSDKWorker|OneSignalSDKUpdaterWorker|amp-web-push-helper-frame|amp-web-push-permission-dialog"}) | |
| 54 | +     * @Route("/public-{fileName}.{fileExtension}", methods={"GET"}, name="static_theme_assets_root_public", requirements={"fileName"=".+"}) | |
| 55 | +     * @Route("/public/{fileName}.{fileExtension}", methods={"GET"}, name="static_theme_assets_public", requirements={"fileName"=".+"}) | |
| 56 | + */ | |
| 57 | +    public function rootAction($fileName, $fileExtension, ThemeHierarchyProviderInterface $themeHierarchyProvider) { | |
| 58 | 58 | $themes = $themeHierarchyProvider->getThemeHierarchy( | 
| 59 | 59 | $this->tenantAwareThemeContext->getTheme() | 
| 60 | 60 | ); | 
| 61 | 61 | |
| 62 | 62 | $fileName = (null === $fileExtension) ? basename($fileName) : $fileName . '.' . $fileExtension; | 
| 63 | 63 |      foreach ($themes as $theme) { | 
| 64 | - $filePath = $theme->getPath() . '/' . self::ASSETS_DIRECTORY . '/' . $fileName; | |
| 65 | -      if (null !== $response = $this->handleFileLoading($filePath)) { | |
| 64 | + $filePath = $theme->getPath() . '/' . self::ASSETS_DIRECTORY . '/' . $fileName; | |
| 65 | +        if (null !== $response = $this->handleFileLoading($filePath)) { | |
| 66 | 66 | return $response; | 
| 67 | - } | |
| 67 | + } | |
| 68 | 68 | } | 
| 69 | 69 | |
| 70 | 70 |      throw new NotFoundHttpException('File was not found.'); | 
| 71 | - } | |
| 72 | - | |
| 73 | - /** | |
| 74 | -   * @Route("/themes/{type}/{themeName}/screenshots/{fileName}", methods={"GET"}, name="static_theme_screenshots", requirements={ | |
| 75 | - * "type": "organization|tenant" | |
| 76 | - * }) | |
| 77 | - */ | |
| 78 | -  public function screenshotsAction(string $type, string $themeName, $fileName) { | |
| 71 | + } | |
| 72 | + | |
| 73 | + /** | |
| 74 | +     * @Route("/themes/{type}/{themeName}/screenshots/{fileName}", methods={"GET"}, name="static_theme_screenshots", requirements={ | |
| 75 | + * "type": "organization|tenant" | |
| 76 | + * }) | |
| 77 | + */ | |
| 78 | +    public function screenshotsAction(string $type, string $themeName, $fileName) { | |
| 79 | 79 |      if ('organization' === $type) { | 
| 80 | -      $theme = $this->loadOrganizationTheme(str_replace('__', '/', $themeName)); | |
| 80 | +        $theme = $this->loadOrganizationTheme(str_replace('__', '/', $themeName)); | |
| 81 | 81 |      } elseif ('tenant' === $type) { | 
| 82 | -      $theme = $this->loadTenantTheme(str_replace('__', '/', $themeName)); | |
| 82 | +        $theme = $this->loadTenantTheme(str_replace('__', '/', $themeName)); | |
| 83 | 83 |      } else { | 
| 84 | -      throw new NotFoundHttpException('File was not found.'); | |
| 84 | +        throw new NotFoundHttpException('File was not found.'); | |
| 85 | 85 | } | 
| 86 | 86 | |
| 87 | 87 | $filePath = $theme->getPath() . '/screenshots/' . $fileName; | 
| 88 | 88 |      if (null !== $response = $this->handleFileLoading($filePath)) { | 
| 89 | - return $response; | |
| 89 | + return $response; | |
| 90 | 90 | } | 
| 91 | 91 | |
| 92 | 92 |      throw new NotFoundHttpException('File was not found.'); | 
| 93 | - } | |
| 94 | - | |
| 95 | - /** | |
| 96 | - * @param $filePath | |
| 97 | - * | |
| 98 | - * @return Response | |
| 99 | - */ | |
| 100 | -  private function handleFileLoading($filePath) { | |
| 93 | + } | |
| 94 | + | |
| 95 | + /** | |
| 96 | + * @param $filePath | |
| 97 | + * | |
| 98 | + * @return Response | |
| 99 | + */ | |
| 100 | +    private function handleFileLoading($filePath) { | |
| 101 | 101 |      if (file_exists($filePath)) { | 
| 102 | - $response = new Response(file_get_contents($filePath)); | |
| 103 | - $disposition = $response->headers->makeDisposition( | |
| 104 | - ResponseHeaderBag::DISPOSITION_INLINE, | |
| 105 | - basename($filePath) | |
| 106 | - ); | |
| 107 | -      $response->headers->set('Content-Disposition', $disposition); | |
| 108 | - | |
| 109 | -      try { | |
| 102 | + $response = new Response(file_get_contents($filePath)); | |
| 103 | + $disposition = $response->headers->makeDisposition( | |
| 104 | + ResponseHeaderBag::DISPOSITION_INLINE, | |
| 105 | + basename($filePath) | |
| 106 | + ); | |
| 107 | +        $response->headers->set('Content-Disposition', $disposition); | |
| 108 | + | |
| 109 | +        try { | |
| 110 | 110 | $mimes = MimeTypes::getDefault()->getMimeTypes($type->getExtension()); | 
| 111 | 111 | $mime = $mimes[0] === 'text/javascript' ? 'application/javascript' : $mimes[0]; | 
| 112 | -      } catch (\Exception $e) { | |
| 112 | +        } catch (\Exception $e) { | |
| 113 | 113 | $mime = 'text/plain'; | 
| 114 | - } | |
| 114 | + } | |
| 115 | 115 | |
| 116 | -      $response->headers->set('Content-Type', $mime); | |
| 117 | - $response->setStatusCode(Response::HTTP_OK); | |
| 118 | - $response->setPublic(); | |
| 119 | - $response->setMaxAge(3600); | |
| 120 | - $response->setSharedMaxAge(7200); | |
| 116 | +        $response->headers->set('Content-Type', $mime); | |
| 117 | + $response->setStatusCode(Response::HTTP_OK); | |
| 118 | + $response->setPublic(); | |
| 119 | + $response->setMaxAge(3600); | |
| 120 | + $response->setSharedMaxAge(7200); | |
| 121 | 121 | |
| 122 | - return $response; | |
| 122 | + return $response; | |
| 123 | + } | |
| 123 | 124 | } | 
| 124 | - } | |
| 125 | 125 | |
| 126 | - /** | |
| 127 | - * @return mixed | |
| 128 | - */ | |
| 129 | -  private function loadOrganizationTheme(string $themeName) { | |
| 126 | + /** | |
| 127 | + * @return mixed | |
| 128 | + */ | |
| 129 | +    private function loadOrganizationTheme(string $themeName) { | |
| 130 | 130 | $loadedThemes = $this->themeLoader->load(); | 
| 131 | 131 | |
| 132 | 132 | return $this->filterThemes($loadedThemes, $themeName); | 
| 133 | - } | |
| 133 | + } | |
| 134 | 134 | |
| 135 | - /** | |
| 136 | - * @return mixed | |
| 137 | - */ | |
| 138 | -  private function loadTenantTheme(string $themeName) { | |
| 135 | + /** | |
| 136 | + * @return mixed | |
| 137 | + */ | |
| 138 | +    private function loadTenantTheme(string $themeName) { | |
| 139 | 139 | $loadedThemes = $this->themeRepository->findAll(); | 
| 140 | 140 | |
| 141 | 141 | return $this->filterThemes($loadedThemes, $themeName); | 
| 142 | - } | |
| 143 | - | |
| 144 | - /** | |
| 145 | - * @param array $loadedThemes | |
| 146 | - * | |
| 147 | - * @return mixed | |
| 148 | - */ | |
| 149 | -  private function filterThemes($loadedThemes, string $themeName) { | |
| 142 | + } | |
| 143 | + | |
| 144 | + /** | |
| 145 | + * @param array $loadedThemes | |
| 146 | + * | |
| 147 | + * @return mixed | |
| 148 | + */ | |
| 149 | +    private function filterThemes($loadedThemes, string $themeName) { | |
| 150 | 150 | $themes = array_filter( | 
| 151 | 151 | $loadedThemes, | 
| 152 | 152 |          function ($element) use (&$themeName) { | 
| 153 | - return $element->getName() === $themeName; | |
| 153 | + return $element->getName() === $themeName; | |
| 154 | 154 | } | 
| 155 | 155 | ); | 
| 156 | 156 | |
| 157 | 157 |      if (0 === count($themes)) { | 
| 158 | -      throw new NotFoundHttpException(sprintf('Theme with name "%s" was not found in organization themes.', $themeName)); | |
| 158 | +        throw new NotFoundHttpException(sprintf('Theme with name "%s" was not found in organization themes.', $themeName)); | |
| 159 | 159 | } | 
| 160 | 160 | |
| 161 | 161 | return reset($themes); | 
| 162 | - } | |
| 162 | + } | |
| 163 | 163 | } | 
| @@ -29,50 +29,50 @@ | ||
| 29 | 29 | |
| 30 | 30 |  class ThemeLogoController extends Controller { | 
| 31 | 31 | |
| 32 | - private Filesystem $filesystem; | |
| 33 | - private ThemeLogoUploaderInterface $themeLogoUploader; | |
| 34 | - private CacheInterface $cacheInterface; | |
| 32 | + private Filesystem $filesystem; | |
| 33 | + private ThemeLogoUploaderInterface $themeLogoUploader; | |
| 34 | + private CacheInterface $cacheInterface; | |
| 35 | 35 | |
| 36 | - /** | |
| 37 | - * @param Filesystem $filesystem | |
| 38 | - * @param ThemeLogoUploaderInterface $themeLogoUploader | |
| 39 | - * @param CacheInterface $cacheInterface | |
| 40 | - */ | |
| 41 | - public function __construct(Filesystem $filesystem, ThemeLogoUploaderInterface $themeLogoUploader, | |
| 42 | -                              CacheInterface $cacheInterface) { | |
| 36 | + /** | |
| 37 | + * @param Filesystem $filesystem | |
| 38 | + * @param ThemeLogoUploaderInterface $themeLogoUploader | |
| 39 | + * @param CacheInterface $cacheInterface | |
| 40 | + */ | |
| 41 | + public function __construct(Filesystem $filesystem, ThemeLogoUploaderInterface $themeLogoUploader, | |
| 42 | +                                CacheInterface $cacheInterface) { | |
| 43 | 43 | $this->filesystem = $filesystem; | 
| 44 | 44 | $this->themeLogoUploader = $themeLogoUploader; | 
| 45 | 45 | $this->cacheInterface = $cacheInterface; | 
| 46 | - } | |
| 46 | + } | |
| 47 | 47 | |
| 48 | - /** | |
| 49 | -   * @Route("/theme_logo/{id}", options={"expose"=true}, requirements={"id"=".+"}, methods={"GET"}, name="swp_theme_logo_get") | |
| 50 | - */ | |
| 51 | -  public function getLogoAction(string $id) { | |
| 48 | + /** | |
| 49 | +     * @Route("/theme_logo/{id}", options={"expose"=true}, requirements={"id"=".+"}, methods={"GET"}, name="swp_theme_logo_get") | |
| 50 | + */ | |
| 51 | +    public function getLogoAction(string $id) { | |
| 52 | 52 | $cacheKey = md5(serialize(['upload', $id])); | 
| 53 | 53 |      return $this->cacheInterface->get($cacheKey, function (CacheItemInterface $item, &$save) use ($id) { | 
| 54 | - $item->expiresAfter(63072000); | |
| 54 | + $item->expiresAfter(63072000); | |
| 55 | 55 | |
| 56 | - $fileSystem = $this->filesystem; | |
| 57 | - $themeLogoUploader = $this->themeLogoUploader; | |
| 58 | - $id = $themeLogoUploader->getThemeLogoUploadPath($id); | |
| 59 | - $file = $fileSystem->has($id); | |
| 60 | -      if (!$file) { | |
| 56 | + $fileSystem = $this->filesystem; | |
| 57 | + $themeLogoUploader = $this->themeLogoUploader; | |
| 58 | + $id = $themeLogoUploader->getThemeLogoUploadPath($id); | |
| 59 | + $file = $fileSystem->has($id); | |
| 60 | +        if (!$file) { | |
| 61 | 61 | $save = false; | 
| 62 | 62 |          throw new NotFoundHttpException('File was not found.'); | 
| 63 | - } | |
| 63 | + } | |
| 64 | 64 | |
| 65 | - $path = $fileSystem->get($id)->getPath(); | |
| 66 | - $response = new Response(); | |
| 67 | - $disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_INLINE, pathinfo($path, PATHINFO_BASENAME)); | |
| 68 | -      $response->headers->set('Content-Disposition', $disposition); | |
| 69 | -      $response->headers->set('Content-Type', MimeTypes::getDefault()->guessMimeType($path)); | |
| 70 | - $response->setPublic(); | |
| 71 | - $response->setMaxAge(63072000); | |
| 72 | - $response->setSharedMaxAge(63072000); | |
| 73 | - $response->setContent($fileSystem->read($path)); | |
| 65 | + $path = $fileSystem->get($id)->getPath(); | |
| 66 | + $response = new Response(); | |
| 67 | + $disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_INLINE, pathinfo($path, PATHINFO_BASENAME)); | |
| 68 | +        $response->headers->set('Content-Disposition', $disposition); | |
| 69 | +        $response->headers->set('Content-Type', MimeTypes::getDefault()->guessMimeType($path)); | |
| 70 | + $response->setPublic(); | |
| 71 | + $response->setMaxAge(63072000); | |
| 72 | + $response->setSharedMaxAge(63072000); | |
| 73 | + $response->setContent($fileSystem->read($path)); | |
| 74 | 74 | |
| 75 | - return $response; | |
| 75 | + return $response; | |
| 76 | 76 | }); | 
| 77 | - } | |
| 77 | + } | |
| 78 | 78 | } | 
| @@ -34,87 +34,87 @@ discard block | ||
| 34 | 34 | use Symfony\Component\Mime\MimeTypes; | 
| 35 | 35 | |
| 36 | 36 |  final class DownloadArticleAuthorAvatarListener { | 
| 37 | - /** | |
| 38 | - * @var MediaManagerInterface | |
| 39 | - */ | |
| 40 | - private $authorMediaManager; | |
| 41 | - | |
| 42 | - /** | |
| 43 | - * @var EntityManagerInterface | |
| 44 | - */ | |
| 45 | - private $entityManager; | |
| 46 | - | |
| 47 | - /** | |
| 48 | - * @var string | |
| 49 | - */ | |
| 50 | - private $cacheDirectory; | |
| 51 | - | |
| 52 | -  public function __construct(EntityManagerInterface $entityManager, $cacheDirectory) { | |
| 37 | + /** | |
| 38 | + * @var MediaManagerInterface | |
| 39 | + */ | |
| 40 | + private $authorMediaManager; | |
| 41 | + | |
| 42 | + /** | |
| 43 | + * @var EntityManagerInterface | |
| 44 | + */ | |
| 45 | + private $entityManager; | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * @var string | |
| 49 | + */ | |
| 50 | + private $cacheDirectory; | |
| 51 | + | |
| 52 | +    public function __construct(EntityManagerInterface $entityManager, $cacheDirectory) { | |
| 53 | 53 | $this->entityManager = $entityManager; | 
| 54 | 54 | $this->cacheDirectory = $cacheDirectory; | 
| 55 | - } | |
| 55 | + } | |
| 56 | 56 | |
| 57 | - /** | |
| 58 | - * @param MediaManagerInterface $authorMediaManager | |
| 59 | - */ | |
| 60 | -  public function setAuthorMediaManager(MediaManagerInterface $authorMediaManager) { | |
| 57 | + /** | |
| 58 | + * @param MediaManagerInterface $authorMediaManager | |
| 59 | + */ | |
| 60 | +    public function setAuthorMediaManager(MediaManagerInterface $authorMediaManager) { | |
| 61 | 61 | $this->authorMediaManager = $authorMediaManager; | 
| 62 | - } | |
| 62 | + } | |
| 63 | 63 | |
| 64 | - /** | |
| 65 | - * @param GenericEvent $event | |
| 66 | - */ | |
| 67 | -  public function processAuthors(GenericEvent $event): void { | |
| 64 | + /** | |
| 65 | + * @param GenericEvent $event | |
| 66 | + */ | |
| 67 | +    public function processAuthors(GenericEvent $event): void { | |
| 68 | 68 | $package = $event->getSubject(); | 
| 69 | 69 | |
| 70 | 70 |      if (!$package instanceof PackageInterface) { | 
| 71 | - throw new UnexpectedTypeException($package, PackageInterface::class); | |
| 71 | + throw new UnexpectedTypeException($package, PackageInterface::class); | |
| 72 | 72 | } | 
| 73 | 73 | |
| 74 | 74 | $authors = []; | 
| 75 | 75 | /** @var ArticleAuthorInterface $packageAuthor */ | 
| 76 | 76 |      foreach ($package->getAuthors() as $packageAuthor) { | 
| 77 | - $authors[] = $this->handle($packageAuthor); | |
| 77 | + $authors[] = $this->handle($packageAuthor); | |
| 78 | 78 | } | 
| 79 | 79 | $package->setAuthors(new ArrayCollection($authors)); | 
| 80 | - } | |
| 81 | - | |
| 82 | - /** | |
| 83 | - * @param ArticleAuthorInterface $object | |
| 84 | - * | |
| 85 | - * @return ArticleAuthorInterface | |
| 86 | - */ | |
| 87 | -  private function handle(ArticleAuthorInterface $object): ArticleAuthorInterface { | |
| 80 | + } | |
| 81 | + | |
| 82 | + /** | |
| 83 | + * @param ArticleAuthorInterface $object | |
| 84 | + * | |
| 85 | + * @return ArticleAuthorInterface | |
| 86 | + */ | |
| 87 | +    private function handle(ArticleAuthorInterface $object): ArticleAuthorInterface { | |
| 88 | 88 |      if (null !== $object->getAvatarUrl()) { | 
| 89 | - $filesystem = new Filesystem(); | |
| 90 | - $pathParts = \pathinfo($object->getAvatarUrl()); | |
| 91 | - $assetId = \sha1($pathParts['filename']); | |
| 92 | -      if (null !== $object->getSlug()) { | |
| 89 | + $filesystem = new Filesystem(); | |
| 90 | + $pathParts = \pathinfo($object->getAvatarUrl()); | |
| 91 | + $assetId = \sha1($pathParts['filename']); | |
| 92 | +        if (null !== $object->getSlug()) { | |
| 93 | 93 | $assetId = $object->getSlug() . '_' . $assetId; | 
| 94 | - } | |
| 95 | - $existingAvatar = $this->entityManager->getRepository(Image::class)->findBy(['assetId' => $assetId]); | |
| 96 | -      if (\count($existingAvatar) > 0) { | |
| 94 | + } | |
| 95 | + $existingAvatar = $this->entityManager->getRepository(Image::class)->findBy(['assetId' => $assetId]); | |
| 96 | +        if (\count($existingAvatar) > 0) { | |
| 97 | 97 | $object->setAvatarUrl((string)\reset($existingAvatar)); | 
| 98 | 98 | |
| 99 | 99 | return $object; | 
| 100 | - } | |
| 100 | + } | |
| 101 | 101 | |
| 102 | -      try { | |
| 102 | +        try { | |
| 103 | 103 | $tempDirectory = $this->cacheDirectory . \DIRECTORY_SEPARATOR . 'downloaded_avatars'; | 
| 104 | 104 |          $tempLocation = $tempDirectory . \DIRECTORY_SEPARATOR . \sha1($assetId . date('his')); | 
| 105 | 105 |          if (!$filesystem->exists($tempDirectory)) { | 
| 106 | - $filesystem->mkdir($tempDirectory); | |
| 106 | + $filesystem->mkdir($tempDirectory); | |
| 107 | 107 | } | 
| 108 | 108 | |
| 109 | 109 | $avatarUrl = $object->getAvatarUrl(); | 
| 110 | 110 |          if (strpos($avatarUrl, "http://") === 0 || strpos($avatarUrl, "https://") === 0) { | 
| 111 | - self::downloadFile($avatarUrl, $tempLocation); | |
| 111 | + self::downloadFile($avatarUrl, $tempLocation); | |
| 112 | 112 |          } else { | 
| 113 | - $file = \file_get_contents($avatarUrl); | |
| 114 | -          if (false === $file) { | |
| 113 | + $file = \file_get_contents($avatarUrl); | |
| 114 | +            if (false === $file) { | |
| 115 | 115 |              throw new \Exception('File can\'t be downloaded'); | 
| 116 | - } | |
| 117 | - $filesystem->dumpFile($tempLocation, $file); | |
| 116 | + } | |
| 117 | + $filesystem->dumpFile($tempLocation, $file); | |
| 118 | 118 | } | 
| 119 | 119 | |
| 120 | 120 | $uploadedFile = new UploadedFile($tempLocation, | 
| @@ -123,29 +123,29 @@ discard block | ||
| 123 | 123 | \filesize($tempLocation), | 
| 124 | 124 | true | 
| 125 | 125 | ); | 
| 126 | -      } catch (\Exception $e) { | |
| 126 | +        } catch (\Exception $e) { | |
| 127 | 127 | return $object; | 
| 128 | - } | |
| 129 | - /** @var Image $image */ | |
| 130 | - $image = $this->authorMediaManager->handleUploadedFile($uploadedFile, $assetId); | |
| 131 | - $avatar = $this->createAuthorMedia($object, $image); | |
| 132 | - $this->entityManager->persist($avatar); | |
| 133 | - $this->entityManager->persist($image); | |
| 134 | - | |
| 135 | - $object->setAvatar($avatar); | |
| 136 | - $object->setAvatarUrl((string)$image); | |
| 128 | + } | |
| 129 | + /** @var Image $image */ | |
| 130 | + $image = $this->authorMediaManager->handleUploadedFile($uploadedFile, $assetId); | |
| 131 | + $avatar = $this->createAuthorMedia($object, $image); | |
| 132 | + $this->entityManager->persist($avatar); | |
| 133 | + $this->entityManager->persist($image); | |
| 134 | + | |
| 135 | + $object->setAvatar($avatar); | |
| 136 | + $object->setAvatarUrl((string)$image); | |
| 137 | 137 | } | 
| 138 | 138 | |
| 139 | 139 | return $object; | 
| 140 | - } | |
| 140 | + } | |
| 141 | 141 | |
| 142 | -  private function createAuthorMedia(ArticleAuthorInterface $articleAuthor, Image $image): AuthorMediaInterface { | |
| 142 | +    private function createAuthorMedia(ArticleAuthorInterface $articleAuthor, Image $image): AuthorMediaInterface { | |
| 143 | 143 |      return new AuthorMedia('avatar', $articleAuthor, $image); | 
| 144 | - } | |
| 144 | + } | |
| 145 | 145 | |
| 146 | -  private static function downloadFile($url, $location) { | |
| 146 | +    private static function downloadFile($url, $location) { | |
| 147 | 147 | $handlerStack = HandlerStack::create(new CurlHandler()); | 
| 148 | 148 | $client = new Client(['handler' => $handlerStack]); | 
| 149 | 149 |      $client->request('GET', $url, ['sink' => $location]); | 
| 150 | - } | |
| 150 | + } | |
| 151 | 151 | } |