private static Resource<@NonNull Path> copyToTempFile(@NonNull URL url) throws IOException { Path path = Files.createTempFile("tmp", Paths.get(url.getPath()).getFileName().toString()); //$NON-NLS-1$ try (InputStream stream = url.openStream()) { Files.copy(stream, path); } if( path == null ) { return null; } return Resource.createTempResource(path); } }
/** * Create a media instance from the given source * * @param url * the url the media is located at * @return media object created */ public static Optional<Media> createMedia(@NonNull URL url) { if (isSupported(url)) { return Optional.of(new Media(url.toExternalForm())); } Optional<URL> localURL = Util.getLocalURL(url); if (localURL.isPresent()) { if (isSupported(localURL.get())) { return Optional.of(new Media(localURL.get().toExternalForm())); } } Optional<Resource<@NonNull Path>> localPath = Util.getLocalPath(url, false); if( localPath.isPresent() ) { try { return Optional.of(new Media(localPath.get().getNativeResource().toUri().toURL().toExternalForm())); } catch (MalformedURLException e) { throw new RuntimeException(e); } } return Optional.empty(); }
/** * Convert an URL to a path on the local filesystem * * @param url * the url * @param copyIfNeeded * <code>true</code> if the url can not be converted to a local * the content is copied to the local filesystem * @return the path * @since 2.2.0 */ public static Optional<Resource<@NonNull Path>> getLocalPath(@NonNull URL url, boolean copyIfNeeded) { return lookupServiceList(URLResolver.class) .stream() .filter(r -> r.test(url)).findFirst() .map(r -> Optional.of(Resource.createResource(r.resolveToLocalPath(url)))) .orElseGet(() -> copyIfNeeded ? ExExecutor.executeSupplier( () -> Util.copyToTempFile(url), "Unable to copy resource") : Optional.empty()); //$NON-NLS-1$ }
if( localPath.isPresent() ) { try { return Optional.of(new AudioClip(localPath.get().getNativeResource().toUri().toURL().toExternalForm())); } catch (MalformedURLException e) { throw new RuntimeException(e);
/** * Create a media instance from the given source * * @param url * the url the media is located at * @return media object created */ public static Optional<Media> createMedia(@NonNull URL url) { if (isSupported(url)) { return Optional.of(new Media(url.toExternalForm())); } Optional<URL> localURL = Util.getLocalURL(url); if (localURL.isPresent()) { if (isSupported(localURL.get())) { return Optional.of(new Media(localURL.get().toExternalForm())); } } Optional<Resource<@NonNull Path>> localPath = Util.getLocalPath(url, false); if( localPath.isPresent() ) { try { return Optional.of(new Media(localPath.get().getNativeResource().toUri().toURL().toExternalForm())); } catch (MalformedURLException e) { throw new RuntimeException(e); } } return Optional.empty(); }
if( localPath.isPresent() ) { try { return Optional.of(new AudioClip(localPath.get().getNativeResource().toUri().toURL().toExternalForm())); } catch (MalformedURLException e) { throw new RuntimeException(e);