diff --git a/src/main/java/org/kde/kdeconnect/Helpers/FilesHelper.java b/src/main/java/org/kde/kdeconnect/Helpers/FilesHelper.java index 6854c108..9d87f38a 100644 --- a/src/main/java/org/kde/kdeconnect/Helpers/FilesHelper.java +++ b/src/main/java/org/kde/kdeconnect/Helpers/FilesHelper.java @@ -15,4 +15,42 @@ public class FilesHelper { return mime; } + //Following code from http://activemq.apache.org/maven/5.7.0/kahadb/apidocs/src-html/org/apache/kahadb/util/IOHelper.html + /** + * Converts any string into a string that is safe to use as a file name. + * The result will only include ascii characters and numbers, and the "-","_", and "." characters. + * + * @param name + * @param dirSeparators + * @param maxFileLength + * @return the resulting string + */ + public static String toFileSystemSafeName(String name, boolean dirSeparators, int maxFileLength) { + int size = name.length(); + StringBuffer rc = new StringBuffer(size * 2); + for (int i = 0; i < size; i++) { + char c = name.charAt(i); + boolean valid = c >= 'a' && c <= 'z'; + valid = valid || (c >= 'A' && c <= 'Z'); + valid = valid || (c >= '0' && c <= '9'); + valid = valid || (c == '_') || (c == '-') || (c == '.'); + valid = valid || (dirSeparators && ( (c == '/') || (c == '\\'))); + + if (valid) { + rc.append(c); + } + } + String result = rc.toString(); + if (result.length() > maxFileLength) { + result = result.substring(result.length()-maxFileLength,result.length()); + } + return result; + } + public static String toFileSystemSafeName(String name, boolean dirSeparators) { + return toFileSystemSafeName(name, dirSeparators, 255); + } + public static String toFileSystemSafeName(String name) { + return toFileSystemSafeName(name, true, 255); + } + } diff --git a/src/main/java/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java b/src/main/java/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java index 9b7b9171..f23a01ff 100644 --- a/src/main/java/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java +++ b/src/main/java/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java @@ -86,10 +86,11 @@ public class SharePlugin extends Plugin { final int fileLength = np.getPayloadSize(); final String filename = np.getString("filename", new Long(System.currentTimeMillis()).toString()); + String deviceDir = FilesHelper.toFileSystemSafeName(device.getName()); //Get the external storage and append "/kdeconnect/DEVICE_NAME/" String destinationDir = Environment.getExternalStorageDirectory().getPath(); destinationDir = new File(destinationDir, "kdeconnect").getPath(); - destinationDir = new File(destinationDir, device.getName()).getPath(); + destinationDir = new File(destinationDir, deviceDir).getPath(); //Create directories if needed new File(destinationDir).mkdirs();