Skip to content

Commit 8471367

Browse files
committed
Add new file handling features and update dependencies
Modified `using` directives in `FileResult.cs` to remove `Telegram.Bot` and `Telegram.Bot.Types`, and add `TelegramBot.Abstractions`. Added a new constructor to `FileResult` class in `FileResult.cs` to create an instance using a `Stream`, `fileName`, and `disposeStream` flag. Updated `Dispose` method in `FileResult` class to check `_disposeStream` flag before disposing of the file stream. Added `File` method in `BotControllerBase.cs` to send a file to the sender, with a `filePath` parameter and `ArgumentNullException` for null or empty `filePath`.
1 parent 18226d2 commit 8471367

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

Sources/TelegramBot/ActionResults/FileResult.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System;
22
using System.IO;
3-
using System.Threading.Tasks;
43
using Telegram.Bot;
54
using Telegram.Bot.Types;
5+
using System.Threading.Tasks;
66
using TelegramBot.Abstractions;
77

88
namespace TelegramBot.ActionResults
@@ -27,12 +27,25 @@ public FileResult(string filePath)
2727
_fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
2828
}
2929

30+
/// <summary>
31+
/// Creates a new instance of <see cref="FileResult"/>.
32+
/// </summary>
33+
/// <param name="stream">Stream with file content.</param>
34+
/// <param name="fileName">File name.</param>
35+
/// <param name="disposeStream">Dispose the stream after sending the file.</param>
36+
public FileResult(Stream stream, string fileName, bool disposeStream = true)
37+
{
38+
_fileName = fileName;
39+
_fileStream = stream;
40+
_disposeStream = disposeStream;
41+
}
42+
3043
/// <summary>
3144
/// Disposes the file stream if it is applicable.
3245
/// </summary>
3346
public void Dispose()
3447
{
35-
if (!_disposed)
48+
if (!_disposed && _disposeStream)
3649
{
3750
_fileStream.Dispose();
3851
_disposed = true;

Sources/TelegramBot/Controllers/BotControllerBase.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ public IActionResult Void()
9696
return new EmptyResult();
9797
}
9898

99+
/// <summary>
100+
/// Sends a file to the sender.
101+
/// </summary>
102+
/// <param name="filePath">Path to the file.</param>
103+
/// <returns>Result of the <see cref="IActionResult"/> action.</returns>
104+
/// <exception cref="ArgumentNullException">Thrown when <paramref name="filePath"/> is null or empty.</exception>
99105
public IActionResult File(string filePath)
100106
{
101107
if (string.IsNullOrWhiteSpace(filePath))

0 commit comments

Comments
 (0)