renameMedia example

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

        if ($destination === '') {
            throw MediaException::emptyMediaFilename();
        }

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

        $this->fileSaver->renameMedia($mediaId$destination$context);

        return $responseFactory->createRedirectResponse($this->mediaDefinition, $mediaId$request$context);
    }

    #[Route(path: '/api/_action/media/provide-name', name: 'api.action.media.provide-name', methods: ['GET'])]     public function provideName(Request $request, Context $context): JsonResponse
    {
        $fileName = $request->query->getString('fileName');
        $preferredFileName = preg_replace('/[\x00-\x1F\x7F-\xFF]/', '', $fileName);

        if (!\is_string($preferredFileName)) {
            
static::assertStringEndsWith($longFileName$updated->getFileName());
        static::assertTrue($this->getPublicFilesystem()->has($this->urlGenerator->getRelativeMediaUrl($updated)));
    }

    public function testRenameMediaThrowsExceptionIfMediaDoesNotExist(): void
    {
        $id = Uuid::randomHex();
        $this->expectException(MediaException::class);
        $this->expectExceptionMessage(MediaException::mediaNotFound($id)->getMessage());

        $context = Context::createDefaultContext();
        $this->fileSaver->renameMedia($id, 'new file destination', $context);
    }

    public function testRenameMediaThrowsExceptionIfMediaHasNoFileAttached(): void
    {
        $id = Uuid::randomHex();

        $this->expectException(MediaException::class);
        $this->expectExceptionMessage(MediaException::missingFile($id)->getMessage());

        $context = Context::createDefaultContext();

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