From b1ade214bc65dcbe3f16a0b06d2766749e20cd10 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 25 Feb 2017 21:20:40 +0800 Subject: [PATCH] Simplify GVFS support --- source/net/filebot/gio/PlatformGVFS.java | 110 +++++++++++++-------- test/net/filebot/gio/PlatformGVFSTest.java | 17 +--- 2 files changed, 69 insertions(+), 58 deletions(-) diff --git a/source/net/filebot/gio/PlatformGVFS.java b/source/net/filebot/gio/PlatformGVFS.java index 5ee5e318..11490d86 100644 --- a/source/net/filebot/gio/PlatformGVFS.java +++ b/source/net/filebot/gio/PlatformGVFS.java @@ -3,59 +3,85 @@ package net.filebot.gio; import java.io.File; import java.net.URI; -import java.util.Optional; public class PlatformGVFS implements GVFS { - private final File gvfs; + private File gvfs; public PlatformGVFS(File gvfs) { - if (gvfs.list() == null) { - throw new IllegalArgumentException(gvfs.getPath() + " is not a valid directory"); - } - this.gvfs = gvfs; } public File getPathForURI(URI uri) { - if ("file".equals(uri.getScheme())) { - return new File(uri); + return Protocol.valueOf(uri.getScheme().toUpperCase()).getFile(gvfs, uri); + } + + public static enum Protocol { + + FILE { + + @Override + public File getFile(File gvfs, URI uri) { + return new File(uri); + } + + @Override + public String getPath(URI uri) { + return new File(uri).getPath(); + } + }, + + SMB { + + @Override + public String getPath(URI uri) { + // e.g. smb://10.0.1.5/data/Movies/Avatar.mp4 -> /run/user/1000/gvfs/smb-share:server=10.0.1.5,share=data/Movies/Avatar.mp4 + StringBuilder s = new StringBuilder("smb-share:"); + s.append("server=").append(uri.getHost()); + if (uri.getUserInfo() != null) { + s.append(",user=").append(uri.getUserInfo()); + } + s.append(",share=").append(uri.getPath().substring(1)); + return s.toString(); + } + }, + + AFP { + + @Override + public String getPath(URI uri) { + // e.g. afp://reinhard@10.0.1.5/data/Movies/Avatar.mp4 -> /run/user/1000/gvfs/afp-volume:host=10.0.1.5,user=reinhard,volume=data/Movies/Avatar.mp4 + StringBuilder s = new StringBuilder("afp-volume:"); + s.append("host=").append(uri.getHost()); + if (uri.getUserInfo() != null) { + s.append(",user=").append(uri.getUserInfo()); + } + s.append(",volume=").append(uri.getPath().substring(1)); + return s.toString(); + } + }, + + SFTP { + + @Override + public String getPath(URI uri) { + // e.g. sftp://reinhard@10.0.1.5/home/Movies/Avatar.mp4 -> /run/user/1000/gvfs/sftp:host=10.0.1.5,user=reinhard/home/Movies/Avatar.mp4 + StringBuilder s = new StringBuilder("sftp:"); + s.append("host=").append(uri.getHost()); + if (uri.getUserInfo() != null) { + s.append(",user=").append(uri.getUserInfo()); + } + s.append(uri.getPath()); + return s.toString(); + } + }; + + public abstract String getPath(URI uri); + + public File getFile(File gvfs, URI uri) { + return new File(gvfs, getPath(uri)); } - // e.g. smb://10.0.1.5/data/Movies/Avatar.mp4 -> /run/user/1000/gvfs/smb-share:server=10.0.1.5,share=data/Movies/Avatar.mp4 - // e.g. afp://reinhard@10.0.1.5/data/Movies/Avatar.mp4 -> /run/user/1000/gvfs/afp-volume:host=10.0.1.5,user=reinhard,volume=data/Movies/Avatar.mp4 - // e.g. sftp://reinhard@10.0.1.5/home/Movies/Avatar.mp4 -> /run/user/1000/gvfs/sftp:host=10.0.1.5,user=reinhard/home/Movies/Avatar.mp4 - - String protocol = uri.getScheme(); - String host = uri.getHost(); - String user = uri.getUserInfo(); - String port = Optional.of(uri.getPort()).filter(i -> i > 0).map(Object::toString).orElse(null); - - String path = uri.getPath().substring(1); - String volume = null; - - if (protocol.equals("smb") || protocol.equals("afp")) { - volume = path.substring(0, path.indexOf('/')); - path = path.substring(volume.length()).substring(1); - } - - // guess GVFS folder based on keywords (see https://wiki.gnome.org/Projects/gvfs/doc) - for (String mount : gvfs.list()) { - if (!mount.startsWith(protocol)) - continue; - if (!mount.contains(host) && !(mount.endsWith("server=" + host) || mount.endsWith("host=" + host))) - continue; - if (volume != null && !(mount.endsWith("share=" + volume) || mount.endsWith("volume=" + volume))) - continue; - if (user != null && !mount.contains("user=" + user)) - continue; - if (port != null && !mount.contains("port=" + port)) - continue; - - return new File(new File(gvfs, mount), path); - } - - throw new IllegalArgumentException("Failed to locate local path: " + uri); } } diff --git a/test/net/filebot/gio/PlatformGVFSTest.java b/test/net/filebot/gio/PlatformGVFSTest.java index 2544eb2a..cc056bc3 100644 --- a/test/net/filebot/gio/PlatformGVFSTest.java +++ b/test/net/filebot/gio/PlatformGVFSTest.java @@ -1,30 +1,15 @@ package net.filebot.gio; -import static java.util.Arrays.*; import static org.junit.Assert.*; import java.io.File; import java.net.URI; -import org.junit.AfterClass; -import org.junit.BeforeClass; import org.junit.Test; public class PlatformGVFSTest { - static File gvfsRoot = new File("gvfs"); - static String[] shares = { "smb-share:server=10.0.1.5,share=data", "afp-volume:host=10.0.1.5,user=reinhard,volume=data", "sftp:host=myserver.org,user=nico", "smb-share:server=192.168.0.1,share=test" }; - - GVFS gvfs = new PlatformGVFS(gvfsRoot); - - @BeforeClass - public static void before() throws Exception { - stream(shares).forEach(f -> new File(gvfsRoot, f).mkdirs()); - } - - @AfterClass - public static void after() throws Exception { - } + GVFS gvfs = new PlatformGVFS(new File("gvfs")); @Test public void smb() throws Exception {