Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
786051ddbf | ||
|
|
d9ff4ae422 | ||
|
|
986b4e74e6 |
@@ -1,19 +1,23 @@
|
|||||||
using System;
|
#nullable enable
|
||||||
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Minio;
|
using Minio;
|
||||||
|
using Minio.DataModel.Args;
|
||||||
using SimApi.Configurations;
|
using SimApi.Configurations;
|
||||||
|
|
||||||
namespace SimApi.Helpers;
|
namespace SimApi.Helpers;
|
||||||
|
|
||||||
public class SimApiStorage
|
public class SimApiStorage
|
||||||
{
|
{
|
||||||
private MinioClient Mc { get; }
|
private IMinioClient Mc { get; }
|
||||||
|
|
||||||
public MinioClient Client => Mc;
|
public IMinioClient Client => Mc;
|
||||||
|
|
||||||
private string ServeUrl { get; }
|
private string ServeUrl { get; }
|
||||||
|
|
||||||
|
private string Endpoint { get; }
|
||||||
|
|
||||||
public string Bucket { get; }
|
public string Bucket { get; }
|
||||||
|
|
||||||
private IHttpContextAccessor HttpContextAccessor { get; }
|
private IHttpContextAccessor HttpContextAccessor { get; }
|
||||||
@@ -22,6 +26,7 @@ public class SimApiStorage
|
|||||||
{
|
{
|
||||||
var options = apiOptions.SimApiStorageOptions;
|
var options = apiOptions.SimApiStorageOptions;
|
||||||
HttpContextAccessor = httpContextAccessor;
|
HttpContextAccessor = httpContextAccessor;
|
||||||
|
Endpoint = options.Endpoint;
|
||||||
var useSsl = false;
|
var useSsl = false;
|
||||||
string endpoint;
|
string endpoint;
|
||||||
if (options.Endpoint.StartsWith("http://"))
|
if (options.Endpoint.StartsWith("http://"))
|
||||||
@@ -39,6 +44,7 @@ public class SimApiStorage
|
|||||||
}
|
}
|
||||||
|
|
||||||
ServeUrl = options.ServeUrl;
|
ServeUrl = options.ServeUrl;
|
||||||
|
if (ServeUrl.EndsWith('/')) throw new Exception("SimApiStorage: ServeUrl must not end with /");
|
||||||
Bucket = options.Bucket;
|
Bucket = options.Bucket;
|
||||||
var mcb = new MinioClient().WithEndpoint(endpoint)
|
var mcb = new MinioClient().WithEndpoint(endpoint)
|
||||||
.WithCredentials(options.AccessKey, options.SecretKey);
|
.WithCredentials(options.AccessKey, options.SecretKey);
|
||||||
@@ -62,10 +68,13 @@ public class SimApiStorage
|
|||||||
/// <param name="path"></param>
|
/// <param name="path"></param>
|
||||||
/// <param name="expire"></param>
|
/// <param name="expire"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public string GetUploadUrl(string path, int expire = 7200)
|
public GetUploadUrlResponse GetUploadUrl(string path, int expire = 7200)
|
||||||
{
|
{
|
||||||
return Mc.PresignedPutObjectAsync(new PresignedPutObjectArgs().WithBucket(Bucket)
|
CheckPath(path);
|
||||||
.WithObject(path).WithExpiry(expire)).Result;
|
var obj = path.TrimStart('/');
|
||||||
|
var uploadUrl = Mc.PresignedPutObjectAsync(new PresignedPutObjectArgs().WithBucket(Bucket)
|
||||||
|
.WithObject(obj).WithExpiry(expire)).Result;
|
||||||
|
return new GetUploadUrlResponse(uploadUrl, $"{ServeUrl}{path}", path);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -76,20 +85,37 @@ public class SimApiStorage
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public string GetDownloadUrl(string path, int expire = 600)
|
public string GetDownloadUrl(string path, int expire = 600)
|
||||||
{
|
{
|
||||||
|
CheckPath(path);
|
||||||
|
path = path.TrimStart('/');
|
||||||
return Mc.PresignedGetObjectAsync(new PresignedGetObjectArgs().WithBucket(Bucket).WithObject(path)
|
return Mc.PresignedGetObjectAsync(new PresignedGetObjectArgs().WithBucket(Bucket).WithObject(path)
|
||||||
.WithExpiry(expire)).Result;
|
.WithExpiry(expire)).Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public string UploadFile(string path, Stream stream, string contentType = "image/png")
|
/// <summary>
|
||||||
|
/// 直接上传文件
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path"></param>
|
||||||
|
/// <param name="stream"></param>
|
||||||
|
/// <param name="contentType"></param>
|
||||||
|
public void UploadFile(string path, Stream stream, string contentType = "image/png")
|
||||||
{
|
{
|
||||||
|
CheckPath(path);
|
||||||
|
path = path.TrimStart('/');
|
||||||
Mc.PutObjectAsync(new PutObjectArgs().WithBucket(Bucket).WithObject(path).WithObjectSize(stream.Length)
|
Mc.PutObjectAsync(new PutObjectArgs().WithBucket(Bucket).WithObject(path).WithObjectSize(stream.Length)
|
||||||
.WithStreamData(stream).WithContentType(contentType)).Wait();
|
.WithStreamData(stream).WithContentType(contentType)).Wait();
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 使用path获取完整的访问URL
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="Exception"></exception>
|
||||||
public string FullUrl(string path)
|
public string FullUrl(string path)
|
||||||
{
|
{
|
||||||
|
if(path.StartsWith("http://") || path.StartsWith("https://")) return path;
|
||||||
|
if (!(path.StartsWith('/') || path.StartsWith("~/"))) throw new Exception("path must start with / or ~/");
|
||||||
var httpRequest = HttpContextAccessor.HttpContext?.Request;
|
var httpRequest = HttpContextAccessor.HttpContext?.Request;
|
||||||
var url = $"{httpRequest?.Scheme}://{httpRequest?.Host}";
|
var url = $"{httpRequest?.Scheme}://{httpRequest?.Host}";
|
||||||
if (string.IsNullOrEmpty(path))
|
if (string.IsNullOrEmpty(path))
|
||||||
@@ -99,4 +125,21 @@ public class SimApiStorage
|
|||||||
|
|
||||||
return path.StartsWith('~') ? string.Concat(url, path.AsSpan(1, path.Length - 1)) : $"{ServeUrl}{path}";
|
return path.StartsWith('~') ? string.Concat(url, path.AsSpan(1, path.Length - 1)) : $"{ServeUrl}{path}";
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
/// <summary>
|
||||||
|
/// 从URL中获取相对路径 (如果url不是当前服务器的url,则原样返回)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="url"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public string? GetPath(string? url)
|
||||||
|
{
|
||||||
|
return url?.Replace($"{Endpoint}/{Bucket}", string.Empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CheckPath(string path)
|
||||||
|
{
|
||||||
|
if (!path.StartsWith('/')) throw new Exception("path must start with /");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public record GetUploadUrlResponse(string UploadUrl, string DownloadUrl, string Path);
|
||||||
+4
-4
@@ -25,10 +25,10 @@
|
|||||||
<Folder Include="Exceptions\"/>
|
<Folder Include="Exceptions\"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Minio" Version="4.0.7"/>
|
<PackageReference Include="Minio" Version="6.0.3" />
|
||||||
<PackageReference Include="RabbitMQ.Client" Version="6.6.0"/>
|
<PackageReference Include="RabbitMQ.Client" Version="6.8.1" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0"/>
|
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.6.2" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.5.0"/>
|
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.6.2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ProjectExtensions>
|
<ProjectExtensions>
|
||||||
<MonoDevelop>
|
<MonoDevelop>
|
||||||
|
|||||||
@@ -1,11 +1,8 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Encodings.Web;
|
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.Unicode;
|
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Newtonsoft.Json.Serialization;
|
|
||||||
using RabbitMQ.Client.Events;
|
using RabbitMQ.Client.Events;
|
||||||
using SimApi.Communications;
|
using SimApi.Communications;
|
||||||
using SimApi.Helpers;
|
using SimApi.Helpers;
|
||||||
|
|||||||
Reference in New Issue
Block a user