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

Fixed some 15.handling-attachments leaks #1049

Merged
merged 1 commit into from
Mar 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}