fileNotFound example

'Content-Disposition' => HeaderUtils::makeDisposition(
                'attachment',
                $originalName,
                // only printable ascii                 (string) preg_replace('/[\x00-\x1F\x7F-\xFF]/', '', $originalName)
            ),
            'Content-Length' => $this->filesystem->fileSize($entity->getPath()),
            'Content-Type' => 'application/octet-stream',
        ];
        $stream = $this->filesystem->readStream($entity->getPath());
        if (!\is_resource($stream)) {
            throw ImportExportException::fileNotFound($fileId);
        }

        return new StreamedResponse(function D) use ($stream): void {
            fpassthru($stream);
        }, Response::HTTP_OK, $headers);
    }

    private function findFile(Context $context, string $fileId): ImportExportFileEntity
    {
        $entity = $this->fileRepository->search(new Criteria([$fileId])$context)->get($fileId);

        

    public static function exceptionDataProvider(): iterable
    {
        yield 'CONTENT__IMPORT_EXPORT_FILE_INVALID_ACCESS_TOKEN' => [
            'exception' => ImportExportException::invalidFileAccessToken(),
            'statusCode' => Response::HTTP_BAD_REQUEST,
            'errorCode' => 'CONTENT__IMPORT_EXPORT_FILE_INVALID_ACCESS_TOKEN',
            'message' => 'Access to file denied due to invalid access token',
        ];

        yield 'CONTENT__IMPORT_EXPORT_FILE_NOT_FOUND' => [
            'exception' => ImportExportException::fileNotFound('notFoundFile'),
            'statusCode' => Response::HTTP_NOT_FOUND,
            'errorCode' => 'CONTENT__IMPORT_EXPORT_FILE_NOT_FOUND',
            'message' => 'Cannot find import/export file with id notFoundFile',
        ];

        yield 'CONTENT__IMPORT_EXPORT_PROCESSING_EXCEPTION' => [
            'exception' => ImportExportException::processingError('Cannot merge file'),
            'statusCode' => Response::HTTP_INTERNAL_SERVER_ERROR,
            'errorCode' => 'CONTENT__IMPORT_EXPORT_PROCESSING_EXCEPTION',
            'message' => 'Cannot merge file',
        ];

        

    }

    private function createStreamedResponse(MediaEntity $media, SalesChannelContext $context): StreamedResponse
    {
        $stream = $context->getContext()->scope(
            Context::SYSTEM_SCOPE,
            fn (Context $context): StreamInterface => $this->mediaService->loadFileStream($media->getId()$context)
        );

        if (!$stream instanceof StreamInterface) {
            throw MediaException::fileNotFound($media->getFilename() . '.' . $media->getFileExtension());
        }

        $stream = $stream->detach();

        if (!\is_resource($stream)) {
            if (!Feature::isActive('v6.6.0.0')) {
                throw new FileNotFoundException($media->getFilename() . '.' . $media->getFileExtension());
            }

            throw MediaException::fileNotFound($media->getFilename() . '.' . $media->getFileExtension());
        }

        

        $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);

        static::assertEquals(Response::HTTP_NOT_FOUND, $exception->getStatusCode());
        static::assertEquals(MediaException::MEDIA_FILE_NOT_FOUND, $exception->getErrorCode());
        static::assertEquals('The file "file-name" does not exist', $exception->getMessage());
        static::assertEquals(['path' => $path]$exception->getParameters());
    }
}
Home | Imprint | This part of the site doesn't use cookies.