createFileResponse example

/** * @dataProvider dataProviderInvalidAccessToken */
    public function testInvalidAccessToken(ImportExportFileEntity $fileEntity, string $accessToken): void
    {
        static::expectException(InvalidFileAccessTokenException::class);
        static::expectExceptionMessage('Access to file denied due to invalid access token');
        /** @var StaticEntityRepository<EntityCollection<ImportExportFileEntity>> $fileRepository */
        $fileRepository = new StaticEntityRepository([new EntityCollection([$fileEntity])]);

        $downloadService = new DownloadService($this->createMock(FilesystemOperator::class)$fileRepository);
        $downloadService->createFileResponse(Context::createDefaultContext()$fileEntity->getId()$accessToken);
    }

    /** * @dataProvider dataProviderNotFoundFile */
    public function testNotFoundFile(ImportExportFileEntity $fileEntity, string $accessToken, string $fileId): void
    {
        static::expectException(FileNotFoundException::class);
        static::expectExceptionMessage(sprintf('Cannot find import/export file with id %s', $fileId));

        /** @var StaticEntityRepository<EntityCollection<ImportExportFileEntity>> $fileRepository */
        
'originalName' => $asciiName . ' öäüß',
            'path' => 'test.csv',
            'expireDate' => new \DateTime(),
        ];
        $filesystem->write($fileData['path']$fileData['originalName']);
        $context = Context::createDefaultContext();
        $fileRepository->create([$fileData]$context);

        $downloadService = new DownloadService($filesystem$fileRepository);
        $accessToken = $downloadService->regenerateToken($context$fileData['id']);

        $response = $downloadService->createFileResponse($context$fileData['id']$accessToken);
        static::assertIsString($header = $response->headers->get('Content-Disposition'));
        static::assertStringContainsString($asciiName$header);

        $response->sendContent();
        $this->expectOutputString($fileData['originalName']);
    }

    public function testSlashFilename(): void
    {
        $filesystem = $this->getPrivateFilesystem();
        $fileRepository = $this->getContainer()->get('import_export_file.repository');

        
#[Route(path: '/api/_action/import-export/file/download', name: 'api.action.import_export.file.download', defaults: ['auth_required' => false], methods: ['GET'])]     public function download(Request $request, Context $context): Response
    {
        /** @var array<string> $params */
        $params = $request->query->all();
        $definition = new DataValidationDefinition();
        $definition->add('fileId', new NotBlank()new Type('string'));
        $definition->add('accessToken', new NotBlank()new Type('string'));
        $this->dataValidator->validate($params$definition);

        return $this->downloadService->createFileResponse($context$params['fileId']$params['accessToken']);
    }

    #[Route(path: '/api/_action/import-export/cancel', name: 'api.action.import_export.cancel', methods: ['POST'])]     public function cancel(Request $request, Context $context): Response
    {
        $logId = $request->request->get('logId');

        if (!\is_string($logId)) {
            throw RoutingException::invalidRequestParameter('logId');
        }

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