aboutsummaryrefslogtreecommitdiff
path: root/lib/Emails.Transactional.Plugin/src/MinioStorage.cs
blob: ab6164ccb9e40d14029f56aec234f8da07331485 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright (c) 2023 Vaughn Nugent
* 
* Library: VNLib
* Package: Emails.Transactional
* File: MinioStorage.cs 
*
* MinioStorage.cs is part of Emails.Transactional which is part of the larger 
* VNLib collection of libraries and utilities.
*
* Emails.Transactional is free software: you can redistribute it and/or modify 
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 2 of the License,
* or (at your option) any later version.
*
* Emails.Transactional is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License 
* along with Emails.Transactional. If not, see http://www.gnu.org/licenses/.
*/


using System.IO;
using System.Threading;
using System.Threading.Tasks;

using VNLib.Utils.IO;
using VNLib.Utils.Memory;
using VNLib.Utils.Logging;
using VNLib.Utils.Extensions;
using VNLib.Plugins;
using VNLib.Plugins.Extensions.Loading;


using Minio;
using Minio.Handlers;
using Minio.DataModel;
using Minio.DataModel.Args;
using Minio.DataModel.Tracing;

namespace Emails.Transactional
{

    [ConfigurationName("s3_config")]
    internal sealed class MinioStorage : ISimpleFilesystem, IAsyncConfigurable
    {
        private readonly MinioClient Client;
        private readonly S3Config Config;

        public MinioStorage(PluginBase plugin, IConfigScope s3Config)
        {
            //Deserialize the config
            Config = s3Config.Deserialze<S3Config>();
            Client = new();

            Client.WithEndpoint(Config.ServerAddress)
                .WithSSL(Config.UseSsl.HasValue && Config.UseSsl.Value)
                .WithTimeout(10 * 1000);

            //Accept optional region
            if (!string.IsNullOrWhiteSpace(Config.Region))
            {
                Client.WithRegion(Config.Region);
            }

            //Setup debug trace
            if (plugin.IsDebug())
            {
                Client.SetTraceOn(new ReqLogger(plugin.Log));
            }
        }


        public string GetExternalFilePath(string filePath)
        {
            string path = Path.Combine(Config.BaseBucket!, filePath);
            //force forward only slashes
            return path.Replace('\\', '/');
        }

        ///<inheritdoc/>
        public async Task ConfigureServiceAsync(PluginBase plugin)
        {
            using ISecretResult? secret = await plugin.GetSecretAsync("s3_secret");

            //Build client
            Client.WithCredentials(Config.ClientId, secret.Result.ToString())
                .Build();
        }

        ///<inheritdoc/>
        public Task DeleteFileAsync(string filePath, CancellationToken cancellation)
        {
            string fullPath = GetExternalFilePath(filePath);

            RemoveObjectArgs args = new();
            args.WithBucket(Config.BaseBucket)
                .WithObject(fullPath);

            //Remove the object
            return Client.RemoveObjectAsync(args, cancellation);
        }

        ///<inheritdoc/>
        public Task WriteFileAsync(string filePath, Stream data, string ct, CancellationToken cancellation)
        {
            string fullPath = GetExternalFilePath(filePath);

            PutObjectArgs args = new();
            args.WithBucket(Config.BaseBucket)
                .WithContentType(ct)
                .WithObject(fullPath)
                .WithObjectSize(data.Length)
                .WithStreamData(data);

            //Upload the object
            return Client.PutObjectAsync(args, cancellation);
        }

        ///<inheritdoc/>
        public async Task<long> ReadFileAsync(string filePath, Stream output, CancellationToken cancellation)
        {
            string fullPath = GetExternalFilePath(filePath);

            //Get the item
            GetObjectArgs args = new();
            args.WithBucket(Config.BaseBucket)
            .WithObject(fullPath)
            .WithCallbackStream(async (stream, cancellation) =>
            {
                //Read the objec to memory
                await stream.CopyToAsync(output, 4096, MemoryUtil.Shared, cancellation);
            });
            try
            {
                //Get the post content file 
                ObjectStat stat = await Client.GetObjectAsync(args, cancellation);
            }
            catch (Minio.Exceptions.ObjectNotFoundException)
            {
                //File not found
                return -1;
            }
            return output.Position;
        }


        internal class ReqLogger : IRequestLogger
        {
            private readonly ILogProvider logProvider;

            public ReqLogger(ILogProvider log)
            {
                logProvider = log;
            }

            public void LogRequest(RequestToLog requestToLog, ResponseToLog responseToLog, double durationMs)
            {
                logProvider.Debug("S3 result\n{method} {uri} HTTP {ms}ms\nHTTP {status} {message}\n{content}",
                    requestToLog.Method, requestToLog.Resource, durationMs,
                    responseToLog.StatusCode, responseToLog.ErrorMessage, responseToLog.Content
                    );
            }
        }
    }
}