/** * Infers the content type contained in the provided {@code Path}. * * @param path path to check * @return string with content type */ public static String detectContentType(Path path) { String result = null; try { for (FileTypeDetector detector : FileTypeDetectors.INSTALLED_DETECTORS) { result = detector.probeContentType(path); if (result != null) { break; } } } catch (IOException e) { LOGGER.log(Level.FINEST, e, () -> "Failed to find content type for " + path); } return result; }
/** * Infers the content type contained in the provided {@code Path}. * * @param path path to check * @return string with content type */ public static String detectContentType(Path path) { String result = null; try { for (FileTypeDetector detector : FileTypeDetectors.INSTALLED_DETECTORS) { result = detector.probeContentType(path); if (result != null) { break; } } } catch (IOException e) { LOGGER.log(Level.FINEST, e, () -> "Failed to find content type for " + path); } return result; }
@Substitute private static String probeContentType(Path path) throws IOException { FilesSupport fs = ImageSingletons.lookup(FilesSupport.class); for (FileTypeDetector detector : fs.installedDetectors) { String result = detector.probeContentType(path); if (result != null) { return result; } } // fallback to default return fs.defaultFileTypeDetector.probeContentType(path); } }
static String findMimeType(final Path filePath, Set<String> validMimeTypes) throws IOException { if (!TruffleOptions.AOT) { Collection<ClassLoader> loaders = SourceAccessor.allLoaders(); for (ClassLoader l : loaders) { for (FileTypeDetector detector : ServiceLoader.load(FileTypeDetector.class, l)) { String mimeType = detector.probeContentType(filePath); if (mimeType != null && (validMimeTypes == null || validMimeTypes.contains(mimeType))) { return mimeType; } } } } String contentType = Files.probeContentType(filePath); if (contentType != null && (validMimeTypes == null || validMimeTypes.contains(contentType))) { return contentType; } return null; }
static String getMimeType(Path filePath) throws IOException { if (!TruffleOptions.AOT) { Collection<ClassLoader> loaders = SourceAccessor.allLoaders(); for (ClassLoader l : loaders) { for (FileTypeDetector detector : ServiceLoader.load(FileTypeDetector.class, l)) { String mimeType = detector.probeContentType(filePath); if (mimeType != null) { return mimeType; } } } } String found = Files.probeContentType(filePath); return found; }
static String findMimeType(final Path filePath) throws IOException { if (!AOT) { Collection<ClassLoader> loaders = SourceAccessor.allLoaders(); for (ClassLoader l : loaders) { for (FileTypeDetector detector : ServiceLoader.load(FileTypeDetector.class, l)) { String mimeType = detector.probeContentType(filePath); if (mimeType != null) { return mimeType; } } } } String found = Files.probeContentType(filePath); return found == null ? "content/unknown" : found; } }