Compare commits

...
4 Commits
Author SHA1 Message Date
xrain 786051ddbf update pkg 2024-07-10 03:30:09 +08:00
xrain d9ff4ae422 fix upload url 2024-07-10 01:18:18 +08:00
xrain 986b4e74e6 new upload 2024-07-10 00:52:51 +08:00
xrain 2257ba5b2a new feature 2024-06-27 04:38:04 +08:00
6 changed files with 90 additions and 23 deletions
+4 -2
View File
@@ -1,6 +1,8 @@
namespace SimApi.Communications;
using System.Collections.Generic;
namespace SimApi.Communications;
/// <summary>
/// 登录信息中间件
/// </summary>
public record SimApiLoginItem(string Id, string[] Type);
public record SimApiLoginItem(string Id, string[] Type,Dictionary<string,string> Meta = null);
+6
View File
@@ -51,4 +51,10 @@ public class SimApiCommonController(SimApiAuth auth) : SimApiBaseController
auth.Logout(token!);
return new SimApiBaseResponse();
}
[HttpPost("/logined"),SimApiAuth]
public SimApiBaseResponse<SimApiLoginItem> UserInfo()
{
return new SimApiBaseResponse<SimApiLoginItem>(LoginInfo);
}
}
+23 -4
View File
@@ -1,5 +1,6 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Text.Json;
using Microsoft.Extensions.Caching.Distributed;
using SimApi.Communications;
@@ -16,11 +17,12 @@ public class SimApiAuth(IDistributedCache cache)
/// </summary>
/// <param name="id"></param>
/// <param name="type"></param>
/// <param name="meta"></param>
/// <param name="token"></param>
/// <returns></returns>
public string Login(string id, string type = "user", string token = null)
public string Login(string id, Dictionary<string, string>? meta = null, string type = "user", string? token = null)
{
return Login(id, new[] { type }, token);
return Login(id, meta, new[] { type }, token);
}
/// <summary>
@@ -28,16 +30,33 @@ public class SimApiAuth(IDistributedCache cache)
/// </summary>
/// <param name="id"></param>
/// <param name="type"></param>
/// <param name="meta"></param>
/// <param name="uuid"></param>
/// <returns></returns>
public string Login(string id, string[] type, string uuid = null)
// ReSharper disable once MemberCanBePrivate.Global
public string Login(string id, Dictionary<string, string>? meta, string[] type, string? uuid = null)
{
uuid ??= Guid.NewGuid().ToString();
var loginItem = new SimApiLoginItem(id, type);
var loginItem = new SimApiLoginItem(id, type, meta);
cache.SetString(uuid, JsonSerializer.Serialize(loginItem));
return uuid;
}
/// <summary>
/// 设置登录的Meta信息
/// </summary>
/// <param name="token"></param>
/// <param name="meta"></param>
/// <returns></returns>
public bool SetMeta(string token, Dictionary<string, string> meta)
{
var login = GetLogin(token);
if (login == null) { return false; }
var newLogin = new SimApiLoginItem(login.Id, login.Type, login.Meta);
cache.SetString(token,JsonSerializer.Serialize(newLogin));
return true;
}
/// <summary>
/// 获取登陆信息
/// </summary>
+53 -10
View File
@@ -1,20 +1,24 @@
using System;
#nullable enable
using System;
using System.IO;
using Microsoft.AspNetCore.Http;
using Minio;
using Minio.DataModel.Args;
using SimApi.Configurations;
namespace SimApi.Helpers;
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 Bucket { get; }
private string Endpoint { get; }
public string Bucket { get; }
private IHttpContextAccessor HttpContextAccessor { get; }
@@ -22,6 +26,7 @@ public class SimApiStorage
{
var options = apiOptions.SimApiStorageOptions;
HttpContextAccessor = httpContextAccessor;
Endpoint = options.Endpoint;
var useSsl = false;
string endpoint;
if (options.Endpoint.StartsWith("http://"))
@@ -39,6 +44,7 @@ public class SimApiStorage
}
ServeUrl = options.ServeUrl;
if (ServeUrl.EndsWith('/')) throw new Exception("SimApiStorage: ServeUrl must not end with /");
Bucket = options.Bucket;
var mcb = new MinioClient().WithEndpoint(endpoint)
.WithCredentials(options.AccessKey, options.SecretKey);
@@ -62,10 +68,13 @@ public class SimApiStorage
/// <param name="path"></param>
/// <param name="expire"></param>
/// <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)
.WithObject(path).WithExpiry(expire)).Result;
CheckPath(path);
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>
@@ -76,20 +85,37 @@ public class SimApiStorage
/// <returns></returns>
public string GetDownloadUrl(string path, int expire = 600)
{
CheckPath(path);
path = path.TrimStart('/');
return Mc.PresignedGetObjectAsync(new PresignedGetObjectArgs().WithBucket(Bucket).WithObject(path)
.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)
.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)
{
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 url = $"{httpRequest?.Scheme}://{httpRequest?.Host}";
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}";
}
}
/// <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
View File
@@ -25,10 +25,10 @@
<Folder Include="Exceptions\"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Minio" Version="4.0.7"/>
<PackageReference Include="RabbitMQ.Client" Version="6.6.0"/>
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0"/>
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.5.0"/>
<PackageReference Include="Minio" Version="6.0.3" />
<PackageReference Include="RabbitMQ.Client" Version="6.8.1" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.6.2" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.6.2" />
</ItemGroup>
<ProjectExtensions>
<MonoDevelop>
-3
View File
@@ -1,11 +1,8 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Serialization;
using RabbitMQ.Client.Events;
using SimApi.Communications;
using SimApi.Helpers;