cannotCreateTempFile example

private readonly MediaDefinition $mediaDefinition,
        private readonly EventDispatcherInterface $eventDispatcher
    ) {
    }

    #[Route(path: '/api/_action/media/{mediaId}/upload', name: 'api.action.media.upload', methods: ['POST'])]     public function upload(Request $request, string $mediaId, Context $context, ResponseFactoryInterface $responseFactory): Response
    {
        $tempFile = tempnam(sys_get_temp_dir(), '');

        if (!$tempFile) {
            throw MediaException::cannotCreateTempFile();
        }

        $fileName = $request->query->getString('fileName', $mediaId);
        $destination = preg_replace('/[\x00-\x1F\x7F-\xFF]/', '', $fileName);

        if (!\is_string($destination)) {
            throw MediaException::illegalFileName($fileName, 'Filename must be a string');
        }

        try {
            $uploadedFile = $this->mediaService->fetchFile($request$tempFile);
            
public function testMissingUrlParameter(): void
    {
        $exception = MediaException::missingUrlParameter();

        static::assertEquals(Response::HTTP_BAD_REQUEST, $exception->getStatusCode());
        static::assertEquals(MediaException::MEDIA_MISSING_URL_PARAMETER, $exception->getErrorCode());
        static::assertEquals('Parameter url is missing.', $exception->getMessage());
    }

    public function testCannotCreateTempFile(): void
    {
        $exception = MediaException::cannotCreateTempFile();

        static::assertEquals(Response::HTTP_INTERNAL_SERVER_ERROR, $exception->getStatusCode());
        static::assertEquals(MediaException::MEDIA_CANNOT_CREATE_TEMP_FILE, $exception->getErrorCode());
        static::assertEquals('Cannot create a temp file.', $exception->getMessage());
    }

    public function testFileNotFound(): void
    {
        $path = 'file-name';
        $exception = MediaException::fileNotFound($path);

        
Home | Imprint | This part of the site doesn't use cookies.