code: private String detectFileName(Resource referencedResource, Resource ntFileResource,
        Resource ntResourceResource) {
      // detect file name
      String fileName = null;
      // if referenced resource is not the nt:file node check for <nodename>Name property
      if (ntFileResource != null && !referencedResource.equals(ntFileResource)) {
        fileName = referencedResource.getValueMap().get(ntFileResource.getName() + "Name", String.class);
      }
      // if not nt:file node exists and the referenced resource is not the nt:resource node check for <nodename>Name property
      else if (ntFileResource == null && !referencedResource.equals(ntResourceResource)) {
        fileName = referencedResource.getValueMap().get(ntResourceResource.getName() + "Name", String.class);
      }
      // otherwise use node name of nt:file resource if it exists
      else if (ntFileResource != null) {
        fileName = ntFileResource.getName();
      }
      // make sure filename has an extension, otherwise build virtual file name
      if (!StringUtils.contains(fileName, ".")) {
        fileName = null;
      }
  
      // if no filename found detect extension from mime type and build virtual filename
      if (StringUtils.isBlank(fileName)) {
        String fileExtension = null;
        if (ntResourceResource != null) {
          String mimeType = ntResourceResource.getValueMap().get(JcrConstants.JCR_MIMETYPE, String.class);
          if (StringUtils.isNotEmpty(mimeType) && mimeTypeService != null) {
            fileExtension = mimeTypeService.getExtension(mimeType);
          }
        }
        if (StringUtils.isEmpty(fileExtension)) {
          fileExtension = "bin";
        }
        fileName = "file." + fileExtension;
      }
  
      return fileName;
    }
docs: Detect filename for inline binary.
  @param referencedResource Resource that was referenced in media reference and may contain file name property.
  @param ntFileResource nt:file resource (optional, null if not existent)
  @param ntResourceResource nt:resource resource
  @return Detected or virtual filename. Never null.