Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
Fixed some 15.handling-attachments leaks (#1049)
Browse files Browse the repository at this point in the history
  • Loading branch information
tracyboehrer authored Mar 11, 2021
1 parent b16bdec commit 13b647a
Showing 1 changed file with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ private CompletableFuture<Activity> handleOutgoingAttachment(TurnContext turnCon
private Activity handleIncomingAttachment(Activity activity) {
String replyText = "";
for (Attachment file : activity.getAttachments()) {
ReadableByteChannel remoteChannel = null;
FileOutputStream fos = null;

try {
// Determine where the file is hosted.
URL remoteFileUrl = new URL(file.getContentUrl());
Expand All @@ -161,12 +164,18 @@ private Activity handleIncomingAttachment(Activity activity) {
String localFileName = file.getName();

// Download the actual attachment
ReadableByteChannel remoteChannel = Channels.newChannel(remoteFileUrl.openStream());
FileOutputStream fos = new FileOutputStream(localFileName);

remoteChannel = Channels.newChannel(remoteFileUrl.openStream());
fos = new FileOutputStream(localFileName);
fos.getChannel().transferFrom(remoteChannel, 0, Long.MAX_VALUE);
} catch (Throwable t) {
replyText += "Attachment \"" + file.getName() + "\" failed to download.\r\n";
} finally {
if (remoteChannel != null) {
try {remoteChannel.close(); } catch (Throwable ignored) {};
}
if (fos != null) {
try {fos.close(); } catch (Throwable ignored) {};
}
}
}

Expand Down Expand Up @@ -237,10 +246,10 @@ private CompletableFuture<String> getEncodedFileData(String filename) {
}

private CompletableFuture<byte[]> getFileData(String filename) {
return Async.wrapBlock(() -> {
InputStream inputStream = Thread.currentThread().
getContextClassLoader().getResourceAsStream(filename);
return IOUtils.toByteArray(inputStream);
});
try (InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename)) {
return CompletableFuture.completedFuture(IOUtils.toByteArray(inputStream));
} catch (Throwable t) {
return Async.completeExceptionally(t);
}
}
}

0 comments on commit 13b647a

Please sign in to comment.