setFileExtension example


    public function testItEncodesTheUrl(): void
    {
        $filter = new UrlEncodingTwigFilter();
        $urlGenerator = $this->getContainer()->get(UrlGeneratorInterface::class);
        $uploadTime = new \DateTime();
        $utc = $uploadTime->getTimestamp();

        $media = new MediaEntity();
        $media->setId(Uuid::randomHex());
        $media->setMimeType('image/png');
        $media->setFileExtension('png');
        $media->setUploadedAt($uploadTime);
        $media->setFileName('(image with spaces and brackets)');
        $media->setUrl($urlGenerator->getAbsoluteMediaUrl($media));

        static::assertStringEndsWith("{$utc}/%28image%20with%20spaces%20and%20brackets%29.png", (string) $filter->encodeMediaUrl($media));
    }
}
$user->setUsername('user1');
        $user->setActive(true);
        $user->setAdmin(true);
        $user->setEmail('user1@shop.de');
        $user->setCreatedAt(new \DateTime('2018-01-15T08:01:16.000+00:00'));
        $user->internalSetEntityData('user', new FieldVisibility([]));

        $media = new MediaEntity();
        $media->setId('548faa1f7846436c85944f4aea792d96');
        $media->setUserId($userId);
        $media->setMimeType('image/jpg');
        $media->setFileExtension('jpg');
        $media->setFileSize(93889);
        $media->setTitle('2');
        $media->setCreatedAt(new \DateTime('2012-08-31T00:00:00.000+00:00'));
        $media->setUpdatedAt(new \DateTime('2017-11-21T11:25:34.000+00:00'));
        $media->setUser($user);
        $media->internalSetEntityData('media', new FieldVisibility([]));

        return $media;
    }

    /** * @return array<string, mixed> */
$user->setActive(true);
        $user->setAdmin(true);
        $user->setEmail('user1@shop.de');
        $user->setCreatedAt(new \DateTime('2018-01-15T08:01:16.000+00:00'));
        $user->internalSetEntityData('user', new FieldVisibility([]));

        $media = new MediaEntity();
        $media->setId('3e352be2d85846dd97529c0f6b544870');
        $media->setUser(clone $user);
        $media->setUserId($userId);
        $media->setMimeType('image/jpg');
        $media->setFileExtension('jpg');
        $media->setFileSize(18921);
        $media->setCreatedAt(new \DateTime('2012-08-15T00:00:00.000+00:00'));
        $media->setUpdatedAt(new \DateTime('2017-11-21T11:25:34.000+00:00'));
        $media->setTitle('Lagerkorn-5,0klein');
        $media->internalSetEntityData('media', new FieldVisibility([]));

        $mediaCollection->add($media);
        $user->setMedia($mediaCollection);

        return $user;
    }

    
$mimeType = $this->filesystem->mimeType($filePath);
        $pathInfo = pathinfo($filePath);

        if (!$mimeType || !\array_key_exists('extension', $pathInfo)) {
            return null;
        }

        $media = new MediaEntity();
        $media->setFileName($pathInfo['filename']);
        $media->setMimeType($mimeType);
        $media->setFileExtension($pathInfo['extension']);

        return $media;
    }
}
$user->setActive(true);
        $user->setAdmin(true);
        $user->setEmail('user1@shop.de');
        $user->setCreatedAt(new \DateTime('2018-01-15T08:01:16.000+00:00'));
        $user->internalSetEntityData('user', new FieldVisibility([]));

        $media1 = new MediaEntity();
        $media1->setId('3e352be2d85846dd97529c0f6b544870');
        $media1->setUser($user);
        $media1->setUserId($userId);
        $media1->setMimeType('image/jpg');
        $media1->setFileExtension('jpg');
        $media1->setFileSize(18921);
        $media1->setCreatedAt(new \DateTime('2012-08-15T00:00:00.000+00:00'));
        $media1->setUpdatedAt(new \DateTime('2017-11-21T11:25:34.000+00:00'));
        $media1->setTitle('Lagerkorn-5,0klein');
        $media1->internalSetEntityData('media', new FieldVisibility([]));

        $media2 = new MediaEntity();
        $media2->setId('f1ad1d0c02454a40abf250f764d16248');
        $media2->setUser($user);
        $media2->setUserId($userId);
        $media2->setMimeType('image/jpg');
        
$this->downloadResponseGenerator = new DownloadResponseGenerator(
            $privateFilesystem,
            $publicFilesystem,
            $this->urlGenerator,
            $this->mediaService,
            $strategy ?? 'php'
        );

        $media = new MediaEntity();
        $media->setId(Uuid::randomHex());
        $media->setFileName('foobar');
        $media->setFileExtension('txt');
        $media->setPrivate($private);

        $streamInterface = $this->createMock(StreamInterface::class);
        $streamInterface->method('detach')->willReturn(fopen('php://temp', 'rb'));
        $this->mediaService->method('loadFileStream')->willReturn($streamInterface);

        $response = $this->downloadResponseGenerator->getResponse($media$this->salesChannelContext);

        $response->headers->set('date', null);
        $expectedResponse->headers->set('date', null);

        

    public function testGetFilenameIncludingExtension(?string $file, ?string $ext, ?string $expected): void
    {
        $media = new MediaEntity();

        if ($file) {
            $media->setFileName($file);
        }

        if ($ext) {
            $media->setFileExtension($ext);
        }

        static::assertEquals($expected$media->getFileNameIncludingExtension());
    }

    /** * @return array<string, array{file: ?string, ext: ?string, expected: ?string}> */
    public static function filenameExtensionProvider(): array
    {
        return [
            
$user->setUsername('user1');
        $user->setActive(true);
        $user->setAdmin(true);
        $user->setEmail('user1@shop.de');
        $user->setCreatedAt(new \DateTime('2018-01-15T08:01:16.000+00:00'));
        $user->internalSetEntityData('user', new FieldVisibility([]));

        $media = new MediaEntity();
        $media->setId('548faa1f7846436c85944f4aea792d96');
        $media->setUserId($userId);
        $media->setMimeType('image/jpg');
        $media->setFileExtension('jpg');
        $media->setFileSize(93889);
        $media->setTitle('2');
        $media->setCreatedAt(new \DateTime('2012-08-31T00:00:00.000+00:00'));
        $media->setUpdatedAt(new \DateTime('2017-11-21T11:25:34.000+00:00'));
        $media->setUser(clone $user);
        $media->internalSetEntityData('media', new FieldVisibility([]));
        $mediaCollection = new MediaCollection([$media]);
        $user->setMedia($mediaCollection);

        return $user;
    }

    
public function testItAddsUrl(): void
    {
        $subscriber = $this->getContainer()->get(MediaLoadedSubscriber::class);
        $context = Context::createDefaultContext();

        $mediaId = '34556f108ab14969a0dcf9d9522fd7df';
        $mimeType = 'image/png';

        $mediaEntity = new MediaEntity();
        $mediaEntity->setId($mediaId);
        $mediaEntity->setMimeType($mimeType);
        $mediaEntity->setFileExtension('png');
        $mediaEntity->setFileName($mediaId . '-134578345');
        $mediaEntity->setThumbnails(new MediaThumbnailCollection());

        $mediaLoadedEvent = new EntityLoadedEvent($this->getContainer()->get(MediaDefinition::class)[$mediaEntity]$context);
        $subscriber->addUrls($mediaLoadedEvent);

        static::assertStringEndsWith(
            $mediaEntity->getFileName() . '.' . $mediaEntity->getFileExtension(),
            $mediaEntity->getUrl()
        );
        static::assertEquals([]$mediaEntity->getThumbnails()->getElements());
    }
/** * @internal */
class TestBasicStruct extends SerializationFixture
{
    public function getInput(): EntityCollection|Entity
    {
        $media = new MediaEntity();
        $media->setId('1d23c1b015bf43fb97e89008cf42d6fe');
        $media->setTitle('Manufacturer');
        $media->setMimeType('image/png');
        $media->setFileExtension('png');
        $media->setFileSize(310818);
        $media->setAlt('A media object description');
        $media->setCreatedAt(new \DateTime('2018-01-15T08:01:16.432+00:00'));
        $media->internalSetEntityData('media', new FieldVisibility([]));

        return $media;
    }

    /** * @return array<string, mixed> */
    
yield $medias;
            }
        };
    }

    private function createMedia(string $name): MediaEntity
    {
        $media = new MediaEntity();
        $media->setUniqueIdentifier(Uuid::randomHex());
        $media->setFileName($name);
        $media->setFileExtension('jpg');
        $media->setTitle($name . ' title');
        $media->setUploadedAt(new \DateTime('16-02-2023 10:00'));
        $media->setFileSize(1024 * 1024);

        return $media;
    }
}


    /** * @dataProvider duplicateFileNameProvider */
    public function testDuplicatedMediaFileNameInFileSystem(bool $isPrivate): void
    {
        $mediaA = new MediaEntity();
        $mediaA->setId(Uuid::randomHex());
        $mediaA->setMimeType('image/png');
        $mediaA->setFileName('foo');
        $mediaA->setFileExtension('png');
        $mediaA->setPrivate(true);

        $mediaB = clone $mediaA;
        $mediaB->setId(Uuid::randomHex());
        $mediaB->setPrivate(false);

        if ($isPrivate) {
            $mediaWithRelatedFileName = new MediaCollection([$mediaA]);
        } else {
            $mediaWithRelatedFileName = new MediaCollection([$mediaB]);
        }

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