From d191f289d8cac14b45b40a9fd331141508eddde0 Mon Sep 17 00:00:00 2001 From: Sky Date: Mon, 29 Apr 2024 20:51:07 +0200 Subject: [PATCH] :bug: Fixes & enhancement for the `download attachment` effect --- .../properties/attachments/EffAttDownload.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/info/itsthesky/disky/elements/properties/attachments/EffAttDownload.java b/src/main/java/info/itsthesky/disky/elements/properties/attachments/EffAttDownload.java index 9b482a49..6d0ace0e 100644 --- a/src/main/java/info/itsthesky/disky/elements/properties/attachments/EffAttDownload.java +++ b/src/main/java/info/itsthesky/disky/elements/properties/attachments/EffAttDownload.java @@ -53,9 +53,19 @@ public void runEffect(@NotNull Event e) { if (file.isDirectory()) file.mkdirs(); - try (InputStream in = new URL(attachment.getUrl()).openStream()) { - Files.copy(in, file.isDirectory() ? Paths.get(file.getPath() + "/" + attachment.getFileName()) : Paths.get(file.getPath())); - } catch (IOException ex) { + if (!file.isDirectory()) { + File parent = file.getParentFile(); + while (parent != null && !parent.exists()) { + parent.mkdirs(); + parent = parent.getParentFile(); + } + } + + try { + attachment.getProxy().downloadToPath( + file.isDirectory() ? Paths.get(file.getPath() + "/" + attachment.getFileName()) : Paths.get(file.getPath()) + ); + } catch (Exception ex) { DiSky.getErrorHandler().exception(e, ex); } restart();