Simplify GVFS support

This commit is contained in:
Reinhard Pointner 2017-02-25 21:20:40 +08:00
parent e3f46c56b5
commit b1ade214bc
2 changed files with 69 additions and 58 deletions

View File

@ -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);
}
}

View File

@ -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 {