FileTypeNotAllowedException example



    public function validate(UploadedFile $file): void
    {
        $valid = $this->checkMimeType($file[
            'jpe|jpg|jpeg' => ['image/jpeg'],
            'png' => ['image/png'],
            'gif' => ['image/gif'],
        ]);

        if (!$valid) {
            throw new FileTypeNotAllowedException($file->getMimeType() ?? '', $this->getType());
        }

        // additional mime type validation         // we detect the mime type over the `getimagesize` extension         $imageSize = getimagesize($file->getPath() . '/' . $file->getFileName());
        if (!isset($imageSize['mime']) || $imageSize['mime'] !== $file->getMimeType()) {
            throw new FileTypeNotAllowedException($file->getMimeType() ?? '', $this->getType());
        }
    }
}

        return 'documents';
    }

    public function validate(UploadedFile $file): void
    {
        $valid = $this->checkMimeType($file[
            'pdf' => ['application/pdf', 'application/x-pdf'],
        ]);

        if (!$valid) {
            throw new FileTypeNotAllowedException($file->getMimeType()$this->getType());
        }
    }
}
$file = $this->getUploadFixture('empty.pdf');
        $result = $this->getUploadService()->upload($file, 'test', 'documents', Context::createDefaultContext());

        $repo = $this->getContainer()->get('media.repository');
        static::assertEquals(1, $repo->search(new Criteria([$result]), Context::createDefaultContext())->getTotal());
        $this->removeMedia($result);
    }

    public function testUploadDocumentFailIllegalFileType(): void
    {
        $this->expectException(FileTypeNotAllowedException::class);
        $this->expectExceptionMessage((new FileTypeNotAllowedException(
            'application/vnd.ms-excel',
            'documents'
        ))->getMessage());

        $file = $this->getUploadFixture('empty.xls');
        $this->getUploadService()->upload($file, 'test', 'documents', Context::createDefaultContext());
    }

    public function testUploadDocumentFailFilenameContainsPhp(): void
    {
        $this->expectException(MediaException::class);
        
Home | Imprint | This part of the site doesn't use cookies.