code: protected static function findExistingFile($fileName)
      {
          $file = null;
          $relativePath = substr(PathUtility::dirname($fileName), strlen(PATH_site));
          $resourceFactory = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
          $targetFolder = $resourceFactory->retrieveFileOrFolderObject($relativePath);
  
          $storageConfiguration = $targetFolder->getStorage()->getConfiguration();
          if (isset($storageConfiguration['basePath'])) {
              $basePath = rtrim($storageConfiguration['basePath'], '/') . '/';
              $basePath = GeneralUtility::getFileAbsFileName($basePath);
              $identifier = substr($fileName, strlen($basePath) - 1);
  
              $row = GeneralUtility::makeInstance(ConnectionPool::class)
                  ->getConnectionForTable('sys_file')
                  ->select(
                      ['uid'],
                      'sys_file',
                      [
                          'storage' => $targetFolder->getStorage()->getUid(),
                          'identifier' => $identifier,
                      ]
                  )
                  ->fetch();
  
              if (!empty($row['uid'])) {
                  /** @var \TYPO3\CMS\Core\Resource\FileRepository $fileRepository */
                  $fileRepository = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\FileRepository::class);
                  $file = $fileRepository->findByUid($row['uid']);
              }
          }
  
          return $file;
      }
docs: Finds an existing file.
  
  @param string $fileName
  @return \TYPO3\CMS\Core\Resource\AbstractFile|null