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
|
/*
* Copyright (c) 2024 Vaughn Nugent
*
* Library: CMNext
* Package: Content.Publishing.Blog.Admin
* File: ManagedStorage.cs
*
* CMNext is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* CMNext 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using VNLib.Utils.IO;
using VNLib.Plugins;
using VNLib.Plugins.Extensions.Loading;
namespace Content.Publishing.Blog.Admin.Storage
{
[ConfigurationName("storage")]
internal sealed class ManagedStorage : ISimpleFilesystem
{
private readonly ISimpleFilesystem _backingStorage;
public ManagedStorage(PluginBase plugin, IConfigScope config)
{
string type = config.GetRequiredProperty("type", p => p.GetString()!);
//try to get custom storage assembly
if (config.TryGetProperty("custom_storage_assembly", p => p.GetString(), out string? storageAssembly)
&& !string.IsNullOrWhiteSpace(storageAssembly))
{
_backingStorage = plugin.CreateServiceExternal<ISimpleFilesystem>(storageAssembly!);
}
else if (string.Equals(type, "s3", StringComparison.OrdinalIgnoreCase))
{
//Use minio storage
_backingStorage = plugin.GetOrCreateSingleton<MinioClientManager>();
}
else if (string.Equals(type, "ftp", StringComparison.OrdinalIgnoreCase))
{
//Use ftp storage
_backingStorage = plugin.GetOrCreateSingleton<FtpStorageManager>();
}
else
{
throw new ArgumentException("No storage providers were found, cannot continue!");
}
}
///<inheritdoc/>
public Task DeleteFileAsync(string filePath, CancellationToken cancellation)
{
return _backingStorage.DeleteFileAsync(filePath, cancellation);
}
///<inheritdoc/>
public string GetExternalFilePath(string filePath)
{
return _backingStorage.GetExternalFilePath(filePath);
}
///<inheritdoc/>
public async Task<long> ReadFileAsync(string filePath, Stream output, CancellationToken cancellation)
{
//Read the file from backing storage
long result = await _backingStorage.ReadFileAsync(filePath, output, cancellation);
//Try to reset the stream if allowed
if (output.CanSeek)
{
//Reset stream
output.Seek(0, SeekOrigin.Begin);
}
return result;
}
///<inheritdoc/>
public Task WriteFileAsync(string filePath, Stream data, string ct, CancellationToken cancellation)
{
//Try to reset the stream if allowed
if (data.CanSeek)
{
//Reset stream
data.Seek(0, SeekOrigin.Begin);
}
return _backingStorage.WriteFileAsync(filePath, data, ct, cancellation);
}
}
}
|