-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileDownloadManager.cs
More file actions
56 lines (46 loc) · 1.87 KB
/
FileDownloadManager.cs
File metadata and controls
56 lines (46 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using Minio.DataModel.Args;
using Minio;
using Services.Contracts;
using Entities.Exceptions.File;
using Microsoft.Extensions.Options;
using Entities.UtilityClasses.Minio;
namespace Services
{
public class FileDownloadManager(
ILoggerService logger,
IOptions<CustomMinioConfig> minioConfig,
IMinioClient minioClient) : IFileDownloadService
{
private readonly IMinioClient _minioClient = minioClient;
private readonly ILoggerService _logger = logger;
private readonly CustomMinioConfig minioConfig = minioConfig.Value;
public async Task<(MemoryStream fileStream, string contentType)> Download(string fileName, string filePath)
{
MemoryStream memoryStream = new()
{
Position = 0
};
try
{
var objectStatArgs = new StatObjectArgs()
.WithBucket(minioConfig.Bucket)
.WithObject(String.Concat(filePath, fileName));
var statObject = await _minioClient.StatObjectAsync(objectStatArgs).ConfigureAwait(false);
var args = new GetObjectArgs()
.WithBucket(minioConfig.Bucket)
.WithObject(String.Concat(filePath, fileName))
.WithCallbackStream((stream) =>
{
stream.CopyTo(memoryStream);
});
var s = await _minioClient.GetObjectAsync(args).ConfigureAwait(false);
return (memoryStream, s.ContentType);
}
catch (Exception ex)
{
_logger.LogError($"Method: {nameof(Download)}; An error occurred while download the media, {ex}");
throw new FileNameNotFoundException();
}
}
}
}