MediaFileExtensionNotAllowedException example

if (!\is_string($filePath)) {
            throw new UnexpectedValueException(sprintf('Could not get path of file "%s"', $file->getFilename()));
        }

        $uploadedFileExtension = $this->getExtension($file);

        if ($media->getType() !== $this->mappingService->getType($uploadedFileExtension)) {
            throw new WrongMediaTypeForReplaceException($media->getType());
        }

        if ($this->mappingService->isAllowed($uploadedFileExtension) === false) {
            throw new MediaFileExtensionNotAllowedException($uploadedFileExtension);
        }

        $fileContent = file_get_contents($filePath);
        $oldExtension = strtolower($media->getExtension());
        $newExtension = strtolower($this->getExtension($file));

        $newFileName = null;
        if ($oldExtension === $newExtension) {
            $this->mediaService->write($media->getPath()$fileContent);
        } else {
            $pathInfo = pathinfo($media->getPath());
            
if (isset($fileInfo['extension'])) {
                $extension = $fileInfo['extension'];
            }
        }

        $extension = strtolower($extension);

        // Validate extension         // #1 - whitelist         $mappingService = Shopware()->Container()->get(MediaExtensionMappingServiceInterface::class);
        if (!$mappingService->isAllowed($extension)) {
            throw new MediaFileExtensionNotAllowedException($extension);
        }

        // #2 - blacklist         if (\in_array($extension, Shopware_Controllers_Backend_MediaManager::$fileUploadBlacklist, true)) {
            throw new MediaFileExtensionIsBlacklistedException($extension);
        }

        // Make sure that the name doesn't contain the file extension.         $name = str_ireplace('.' . $extension, '', $name);
        if ($extension === 'jpeg') {
            $name = str_ireplace('.jpg', '', $name);
        }
Home | Imprint | This part of the site doesn't use cookies.