Skip to content

Commit

Permalink
[ZkSGN6PO] apoc.export.graphml.all doesn't accept absolute Windows pa…
Browse files Browse the repository at this point in the history
…ths (neo4j/apoc#381) (#3577)

* [ZkSGN6PO] apoc.export.graphml.all doesn't accept absolute Windows paths

* [ZkSGN6PO] changed wording of the win error
  • Loading branch information
vga91 authored May 16, 2023
1 parent 18aaf50 commit e852bd3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
33 changes: 23 additions & 10 deletions core/src/main/java/apoc/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@
*/
public class FileUtils {

public static String getFileUrl(String fileName) throws MalformedURLException {
try {
return new URL(fileName).getFile();
} catch (MalformedURLException e) {
if (e.getMessage().contains("no protocol")) {
return fileName;
}
throw e;
}
}

public enum SupportedProtocols {
http(true, null),
https(true, null),
Expand Down Expand Up @@ -95,14 +106,7 @@ public StreamConnection getStreamConnection(String urlAddress, Map<String, Objec
try {
return new StreamConnection.FileStreamConnection(URI.create(urlAddress));
} catch (IllegalArgumentException iae) {
try {
return new StreamConnection.FileStreamConnection(new URL(urlAddress).getFile());
} catch (MalformedURLException mue) {
if (mue.getMessage().contains("no protocol")) {
return new StreamConnection.FileStreamConnection(urlAddress);
}
throw mue;
}
return new StreamConnection.FileStreamConnection(getFileUrl(urlAddress));
}
}
}
Expand All @@ -120,8 +124,10 @@ public OutputStream getOutputStream(String fileName, ExportConfig config) {
outputStream = HDFSUtils.writeFile(fileName);
break;
default:
final Path path = resolvePath(fileName);
outputStream = new FileOutputStream(path.toFile());
final File file = isImportUsingNeo4jConfig()
? resolvePath(fileName).toFile()
: new File(getFileUrl(fileName));
outputStream = new FileOutputStream(file);
}
return new BufferedOutputStream(compressionAlgo.getOutputStream(outputStream));
} catch (Exception e) {
Expand Down Expand Up @@ -152,6 +158,13 @@ public static SupportedProtocols from(String source) {
// otherwise we return unknown protocol: yyyyy
return SupportedProtocols.valueOf(new URI(source).getScheme());
} catch (Exception ignored) {}

// in case a Windows user write an url like `C:/User/...`
if (e.getMessage().contains("unknown protocol") && Util.isWindows()) {
throw new RuntimeException(e.getMessage() +
"\n Please note that for Windows absolute paths they have to be explicit by prepending `file:` or supplied without the drive, " +
"\n e.g. `file:C:/my/path/file` or `/my/path/file`, instead of `C:/my/path/file`");
}
throw new RuntimeException(e);
}
return SupportedProtocols.file;
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/apoc/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -1196,4 +1196,10 @@ public static <T extends Entity> T withTransactionAndRebind(GraphDatabaseService
T result = retryInTx(NullLog.getInstance(), db, action, 0, 0, r -> {});
return rebind(transaction, result);
}

public static boolean isWindows() {
return System.getProperty("os.name")
.toLowerCase()
.contains("win");
}
}

0 comments on commit e852bd3

Please sign in to comment.