2024-12-09 21:11:46 +01:00
|
|
|
// Copyright (C) 2023-present sam/u1f320 (vulpine.solutions)
|
|
|
|
//
|
|
|
|
// This program 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.
|
|
|
|
//
|
|
|
|
// This program 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/>.
|
2024-09-03 16:29:51 +02:00
|
|
|
using Minio;
|
|
|
|
using Minio.DataModel.Args;
|
|
|
|
using Minio.Exceptions;
|
|
|
|
|
|
|
|
namespace Foxnouns.Backend.Services;
|
|
|
|
|
2024-09-09 14:50:00 +02:00
|
|
|
public class ObjectStorageService(ILogger logger, Config config, IMinioClient minioClient)
|
2024-09-03 16:29:51 +02:00
|
|
|
{
|
|
|
|
private readonly ILogger _logger = logger.ForContext<ObjectStorageService>();
|
2024-09-04 14:25:44 +02:00
|
|
|
|
2024-09-09 14:37:59 +02:00
|
|
|
public async Task RemoveObjectAsync(string path, CancellationToken ct = default)
|
2024-09-03 16:29:51 +02:00
|
|
|
{
|
2024-09-04 14:25:44 +02:00
|
|
|
_logger.Debug("Deleting object at path {Path}", path);
|
2024-09-03 16:29:51 +02:00
|
|
|
try
|
|
|
|
{
|
2024-09-09 14:50:00 +02:00
|
|
|
await minioClient.RemoveObjectAsync(
|
|
|
|
new RemoveObjectArgs().WithBucket(config.Storage.Bucket).WithObject(path),
|
2024-10-02 00:28:07 +02:00
|
|
|
ct
|
|
|
|
);
|
2024-09-03 16:29:51 +02:00
|
|
|
}
|
|
|
|
catch (InvalidObjectNameException)
|
|
|
|
{
|
2024-09-04 14:25:44 +02:00
|
|
|
// ignore non-existent objects
|
2024-09-03 16:29:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-02 00:28:07 +02:00
|
|
|
public async Task PutObjectAsync(
|
|
|
|
string path,
|
|
|
|
Stream data,
|
|
|
|
string contentType,
|
|
|
|
CancellationToken ct = default
|
|
|
|
)
|
2024-09-03 16:29:51 +02:00
|
|
|
{
|
2024-10-02 00:28:07 +02:00
|
|
|
_logger.Debug(
|
|
|
|
"Putting object at path {Path} with length {Length} and content type {ContentType}",
|
|
|
|
path,
|
|
|
|
data.Length,
|
|
|
|
contentType
|
|
|
|
);
|
2024-09-04 14:25:44 +02:00
|
|
|
|
2024-10-02 00:28:07 +02:00
|
|
|
await minioClient.PutObjectAsync(
|
|
|
|
new PutObjectArgs()
|
2024-09-09 14:37:59 +02:00
|
|
|
.WithBucket(config.Storage.Bucket)
|
|
|
|
.WithObject(path)
|
|
|
|
.WithObjectSize(data.Length)
|
|
|
|
.WithStreamData(data)
|
2024-10-02 00:28:07 +02:00
|
|
|
.WithContentType(contentType),
|
|
|
|
ct
|
2024-09-03 16:29:51 +02:00
|
|
|
);
|
|
|
|
}
|
2024-10-02 00:28:07 +02:00
|
|
|
}
|